The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to pass an image to a 2D array using Matlab MEX
Replies: 3   Last Post: Feb 13, 2014 11:37 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 110
Registered: 6/2/11
How to pass an image to a 2D array using Matlab MEX
Posted: Feb 13, 2014 8:23 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

I want to read some image to run a C++ MEX function to process this image. The image is originally stored as a Matlab 3D matrix, and then converted to a 2D matrix and each slice of the image is stored as a 1D vector. Here is some example codes below.

1. The MEX function:

include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

unsigned int** ubuff;

size_t col_ubuff = mxGetN(prhs[0]);
size_t row_ubuff = mxGetM(prhs[0]);

ubuff = (unsigned int **)mxCalloc(col_ubuff, row_ubuff);

for(int x = 0; x < col_ubuff; x++) {
ubuff[x] = (unsigned int *) mxCalloc(row_ubuff, sizeof(unsigned int));

for (int col=0; col < col_ubuff; col++) {
for (int row=0; row < row_ubuff; row++) {
ubuff[col][row] = mxGetPr(prhs[0])[row+col*row_ubuff];

unsigned int the_pixel_i_want = ubuff[16][2*32 + 2];
printf ("Debug: %d\n\n", the_pixel_i_want);


2. The program can be compiled; however, the results of running the program is strange; for example, I have some test codes and test image with all pixels to be 188,

%% Test input image as 2D array clc; clf; clear all; close all;

volImage = 188.*ones(32,32,32);
volImageVec = reshape(volImage, 32*32, 32)';
volImageVec = uint32(volImageVec);


But the output sometimes be 188 and sometimes be 0. Is there a memory leak somewhere in my code? Thanks very much for your help.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.