Matlab C
Da Bioingegneria Elettronica e Informatica.
Versione del 19 dic 2019 alle 13:16 di Profbevilacqua (Discussione | contributi)
Irio De Feudis irio.defeudis@poliba.it
Vitoantonio Bevilacqua vitoantonio.bevilacqua@poliba.it
MATLAB C/C++ interface in Visual Studio
Input Image
[[1]]
Code
#include "engine.h"
#include <iostream>
using namespace std;
unsigned char** getRawGrayScaleImage(mxArray *);
void putRawGrayScaleImageInMatlab(unsigned char ** mat, int n);
void countAboveBelowThreshold(unsigned char ** mat, int n, int th);
void invertImage(unsigned char ** mat, int n);
//declare pointer to engine
Engine *eng;
int main() {
//initialize it with the handle to a new session of MATLAB engine
eng = engOpen(NULL);
engSetVisible(eng, 0);
//check status
if (!(eng)) {
exit(-1);
}
//open image in MATLAB engine (using image path)
engEvalString(eng, "I = imread('C:\\Users\\Irio\\Desktop\\\Esercizio_05122019\\MATLAB&VISUAL STUDIO\\codes\\Heatmap.jpg');");
//show image I
engEvalString(eng, "imshow(I)");
//wait engine
system("PAUSE");
//....manage your matrix in MATLAB (crop and convert)
// MATLAB functions:
// imcrop - creates a crop of the image
engEvalString(eng, "I = imcrop(I,[276,279,844,844]);");
engEvalString(eng, "imshow(I)");
system("PAUSE");
// rgb2gray - convert RGB image or colormap to grayscale.
engEvalString(eng, "I = rgb2gray(I);");
engEvalString(eng, "imshow(I)");
system("PAUSE");
//get mxArray of the image (C++ style)
mxArray *imageML = engGetVariable(eng, "I");
unsigned char **rawimage = getRawGrayScaleImage(imageML);
//....manage your matrix in C++
// count and print elements above and below a threshold value
countAboveBelowThreshold(rawimage, 845, 127);
system("pause");
// invert image
invertImage(rawimage, 845);
system("pause");
//put image back in MATLAB
putRawGrayScaleImageInMatlab(rawimage, 845);
//show image
engEvalString(eng, "imshow(I)");
system("pause");
//close the session
engClose(eng);
return 0;
}
//extract image from mxArray pointer
unsigned char** getRawGrayScaleImage(mxArray *imagePtr) {
int n = (int)mxGetN(imagePtr);
unsigned char **mat = new unsigned char*[n];
for (int i = 0; i < n; i++)
mat[i] = new unsigned char[n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
mat[i][j] = *((unsigned char*)mxGetData(imagePtr) + i + j*n);
}
return mat;
}
//create mxArray from image
void putRawGrayScaleImageInMatlab(unsigned char ** mat, int n) {
mxArray *matrix = mxCreateNumericMatrix(n, n, mxUINT8_CLASS, mxREAL);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
unsigned char *data = (unsigned char*)mxGetData(matrix) + i + j*n;
*data = mat[i][j];
}
engPutVariable(eng, "I", matrix);
}
// count elements over and under threshold
void countAboveBelowThreshold(unsigned char ** mat, int n, int th) {
int above = 0;
int below = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (mat[i][j] <= th)
below++;
else
above++;
}
cout << "Threshold: " << th << endl;
cout << "Elements above the threshold: " << above << endl;
cout << "Elements below the threshold: "<< below << endl;
}
// invert image
void invertImage(unsigned char ** mat, int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
mat[i][j] = 255 - mat[i][j];
}
}