简体   繁体   English

将图像输出回Matlab Mex

[英]outputting image back to matlab Mex

I am trying to output an image from my mex file back to my matlab file, but when i open it in matlab it is not correct. 我正在尝试将图像从我的mex文件输出回我的matlab文件,但是当我在matlab中打开它时,它是不正确的。

The output image withing the mex file is correct 带有mex文件的输出图像是正确的

I have tried switching the orientation of the mwSize as well as swapping i and j in new_img.at<int>(j, i) ; 我试图切换mwSize的方向以及在new_img.at<int>(j, i)交换ij ;

Mat image = imread(mxArrayToString(prhs[0]));
Mat new_img(H,W, image.type(), Scalar(0));
// some operations on new_img
imshow( "gmm image", image ); //shows the original image
imshow( "gmm1 image", new_img ); //shows the output image
waitKey( 200 );  //both images are the same size as desired

mwSize nd = 2;
mwSize dims[] = {W, H};

plhs[0] = mxCreateNumericArray(nd, dims, mxUINT8_CLASS, mxREAL);
if(plhs == NULL) {
  mexErrMsgTxt("Could not create mxArray.\n");     
}     
char* outMat = (char*) mxGetData( plhs[0]);

for (int i= 0; i < H; i++)
{
  for (int j = 0; j < W; j++)
  {       
    outMat[i +j*image.rows] = new_img.at<int>(j, i);       
  }
}

this is in the mat file 这在垫子文件中

gmmMask = GmmMex2(imgName,rect);
imshow(gmmMask); % not the same as the output image. somewhat resembles it, but not correct.

Because you have alluded to this being a colour image , this means that you have three slices of the matrix to consider. 因为您已经暗示这是彩色图像 ,所以这意味着您要考虑矩阵的三个部分。 Your code only considers one slice. 您的代码仅考虑一片。 First off you need to make sure that you declare the right size of the image. 首先,您需要确保声明正确的图像尺寸。 In MATLAB, the first dimension is always the number of rows while the second dimension is the number of columns. 在MATLAB中,第一维始终是行数,而第二维是列数。 Now you have to add the number of channels too on top of this. 现在,您还必须在此之上添加通道数。 I'm assuming this is an RGB image so there are three channels. 我假设这是RGB图像,所以有三个通道。

Therefore, change your dims to: 因此,将您的dims更改为:

mwSize nd = 3;
mwSize dims[] = {H, W, nd};

Changing nd to 3 is important as this will allow you to create a 3D matrix. nd更改为3很重要,因为这将允许您创建3D矩阵。 You only have a 2D matrix. 您只有2D矩阵。 Next, make sure that you are accessing the image pixels at the right location in the cv::Mat object. 接下来,确保要访问cv::Mat对象中正确位置的图像像素。 The way you are accessing the image pixels in the nested pair of for loops assumes a row-major fashion (iterating over the columns first, then the rows). 在嵌套的for循环对中访问图像像素的方式采用行为主的方式(先在列上迭代,然后在行上迭代)。 As such, you need to interchange i and j as i accesses the rows and j accesses the columns. 这样,您需要在i访问行和j访问列时交换ij You will also need to access the channel of the colour image so you'll need another for loop to compensate. 需要访问彩色图像的通道,因此需要另一个for循环来进行补偿。 For the grayscale case, you have properly compensated for the column-major memory configuration for the MATLAB MEX matrix though. 对于灰度情况,尽管如此,您已经适当地补偿了MATLAB MEX矩阵的列主内存配置。 This is verified because j accesses the columns and you need to skip over by rows amount in order to access the next column. 这已得到验证,因为j访问列,并且您需要按行数跳过才能访问下一列。 However, to accommodate for a colour image, you must also skip over by image.rows*image.cols to go to the next layer of pixels. 但是,要适应彩色图像,还必须按image.rows*image.cols跳过以转到下一层像素。

Therefore your for loop should now be: 因此,您的for循环现在应为:

for (int k = 0; k < nd; k++) {
    for (int i = 0; i < H; i++) { 
        for (int j = 0; j < W; j++) {
            outMat[k*image.rows*image.cols + i + j*image.rows] = new_img.at<uchar>(i, j, k);
        }
    }
}

Take note that the container of pixels is most likely 8-bit unsigned character, and so you must change the template to uchar not int . 请注意,像素容器很可能是8位无符号字符,因此必须将模板更改为uchar not int This may also explain why your program is crashing. 这也可以解释您的程序为什么崩溃的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM