Matlab C

Da Bioingegneria Elettronica e Informatica.
Versione del 19 dic 2019 alle 13:16 di Profbevilacqua (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Irio De Feudis irio.defeudis@poliba.it

Vitoantonio Bevilacqua vitoantonio.bevilacqua@poliba.it

Slides

MATLAB C/C++ interface in Visual Studio

Input Image

[[1]]

Code

  1. #include "engine.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. unsigned char** getRawGrayScaleImage(mxArray *);
  6. void putRawGrayScaleImageInMatlab(unsigned char ** mat, int n);
  7. void countAboveBelowThreshold(unsigned char ** mat, int n, int th);
  8. void invertImage(unsigned char ** mat, int n);
  9.  
  10. //declare pointer to engine
  11. Engine *eng;
  12.  
  13. int main() {
  14.  
  15.  
  16. 	//initialize it with the handle to a new session of MATLAB engine
  17. 	eng = engOpen(NULL);
  18. 	engSetVisible(eng, 0);
  19. 	//check status 
  20. 	if (!(eng)) {
  21. 		exit(-1);
  22. 	}
  23.  
  24.  
  25. 	//open image in MATLAB engine (using image path)
  26. 	engEvalString(eng, "I = imread('C:\\Users\\Irio\\Desktop\\\Esercizio_05122019\\MATLAB&VISUAL STUDIO\\codes\\Heatmap.jpg');");
  27.  
  28. 	//show image I
  29. 	engEvalString(eng, "imshow(I)");
  30.  
  31. 	//wait engine
  32. 	system("PAUSE");
  33.  
  34. 	//....manage your matrix in MATLAB (crop and convert)
  35. 	// MATLAB functions:
  36. 	// imcrop - creates a crop of the image
  37. 	engEvalString(eng, "I = imcrop(I,[276,279,844,844]);");
  38. 	engEvalString(eng, "imshow(I)");
  39.  
  40. 	system("PAUSE");
  41.  
  42. 	// rgb2gray - convert RGB image or colormap to grayscale.
  43. 	engEvalString(eng, "I = rgb2gray(I);");
  44. 	engEvalString(eng, "imshow(I)");
  45.  
  46. 	system("PAUSE");
  47.  
  48. 	//get mxArray of the image (C++ style)
  49. 	mxArray *imageML = engGetVariable(eng, "I");
  50. 	unsigned char **rawimage = getRawGrayScaleImage(imageML);
  51.  
  52. 	//....manage your matrix in C++ 
  53.  
  54. 	// count and print elements above and below a threshold value
  55. 	countAboveBelowThreshold(rawimage, 845, 127);
  56. 	system("pause");
  57.  
  58. 	// invert image
  59. 	invertImage(rawimage, 845);
  60. 	system("pause");
  61.  
  62. 	//put image back in MATLAB
  63. 	putRawGrayScaleImageInMatlab(rawimage, 845);
  64. 	//show image
  65. 	engEvalString(eng, "imshow(I)");
  66. 	system("pause");
  67.  
  68. 	//close the session
  69. 	engClose(eng);
  70.  
  71. 	return 0;
  72. }
  73.  
  74. //extract image from mxArray pointer
  75. unsigned char** getRawGrayScaleImage(mxArray *imagePtr) {
  76.  
  77. 	int n = (int)mxGetN(imagePtr);
  78.  
  79. 	unsigned char **mat = new unsigned char*[n];
  80. 	for (int i = 0; i < n; i++)
  81. 		mat[i] = new unsigned char[n];
  82.  
  83. 	for (int i = 0; i < n; i++)
  84. 		for (int j = 0; j < n; j++) {
  85. 			mat[i][j] = *((unsigned char*)mxGetData(imagePtr) + i + j*n);
  86. 		}
  87.  
  88. 	return mat;
  89. }
  90.  
  91. //create mxArray from image
  92. void putRawGrayScaleImageInMatlab(unsigned char ** mat, int n) {
  93. 	mxArray *matrix = mxCreateNumericMatrix(n, n, mxUINT8_CLASS, mxREAL);
  94.  
  95. 	for (int i = 0; i < n; i++)
  96. 		for (int j = 0; j < n; j++) {
  97. 			unsigned char *data = (unsigned char*)mxGetData(matrix) + i + j*n;
  98. 			*data = mat[i][j];
  99. 		}
  100.  
  101. 	engPutVariable(eng, "I", matrix);
  102. }
  103.  
  104. // count elements over and under threshold
  105. void countAboveBelowThreshold(unsigned char ** mat, int n, int th) {
  106. 	int above = 0;
  107. 	int below = 0;
  108.  
  109. 	for (int i = 0; i < n; i++)
  110. 		for (int j = 0; j < n; j++) {
  111. 			if (mat[i][j] <= th)
  112. 				below++;
  113. 			else
  114. 				above++;
  115. 		}
  116.  
  117. 	cout << "Threshold: " << th << endl;
  118. 	cout << "Elements above the threshold: " <<  above << endl;
  119. 	cout << "Elements below the threshold: "<< below << endl;
  120. }
  121.  
  122. // invert image
  123. void invertImage(unsigned char ** mat, int n) {
  124.  
  125. 	for (int i = 0; i < n; i++)
  126. 		for (int j = 0; j < n; j++) {
  127. 			mat[i][j] = 255 - mat[i][j];
  128. 		}
  129. }