我试图写一个mex函数来调用另一个函数F. F使用opencv库,所以我需要将mxarray转换为cv :: mat。 我使用嵌套for循环来将每个像素值或向量的值分配给两个创建的cv :: mat。 该代码发布如下。
但是,运行for-loop一段时间后,程序崩溃。 我试图使用mexPrintf打印出如A(i,j,2)的值,并且值是正确的(无符号字符)。 来自Matlab的输入是uint8类型的矩阵,这就是我在cpp代码中将东西转换为无符号字符的原因。
我不知道它为什么会崩溃。 我也尝试使用visual studio的attach过程进行调试,但它似乎不起作用,并且每次我在Matlab中附加过程并调用mex函数时,我都会收到访问冲突消息。
void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]) { #define A_IN prhs[0] #define B_IN prhs[1] #define Y_out plhs[0] #define A(i,j,k) A[i+j*R+k*(R*C)] #define B(i,j) B[i+j*R] #define Y(i,j) Y[i+j*R] const int *d_pr = mxGetDimensions(A_IN); int C = d_pr[1]; int dims[2] = { R,C }; Y_out = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL); unsigned char *A, *B,*Y; A = (unsigned char *)mxGetData(A_IN); B = (unsigned char *)mxGetData(B_IN); Y = (unsigned char *)mxGetData(Y_out); cv::Mat image(R, C, CV_8UC3); cv::Mat trimap(R, C, CV_8UC1); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j); } } //some other stuff}
I'm trying to write a mex function to call another function F. F uses the opencv library, so I need to convert the mxarray into a cv::mat. I use a nested for loop to assign each pixel value or vector of values to the two created cv::mat. The code is posted below.
However, the program crashes after running the for-loop for a while. I tried to use mexPrintf to print out values such as A(i, j, 2) and the values are correct (unsigned char). The input from Matlab are matrices of type uint8, and this is the reason I cast things into unsigned char in the cpp code.
I have no idea why it is crashing. I also tried to use visual studio's attach process to debug, but it doesn't seem to work, and every time I attached the process and call the mex function in Matlab I just got a access violation message.
void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]) { #define A_IN prhs[0] #define B_IN prhs[1] #define Y_out plhs[0] #define A(i,j,k) A[i+j*R+k*(R*C)] #define B(i,j) B[i+j*R] #define Y(i,j) Y[i+j*R] const int *d_pr = mxGetDimensions(A_IN); int C = d_pr[1]; int dims[2] = { R,C }; Y_out = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL); unsigned char *A, *B,*Y; A = (unsigned char *)mxGetData(A_IN); B = (unsigned char *)mxGetData(B_IN); Y = (unsigned char *)mxGetData(Y_out); cv::Mat image(R, C, CV_8UC3); cv::Mat trimap(R, C, CV_8UC1); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j); } } //some other stuff}
最满意答案
更改for循环中的4行代码:
image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j);成:
image.row(i).col(j).data[0] = A(i, j, 2); image.row(i).col(j).data[1] = A(i, j, 1); image.row(i).col(j).data[2] = A(i, j, 0); trimap.row(i).col(j).data[0] = B(i, j);为我工作。
Changing the 4 lines of code inside the for-loop:
image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j);into:
image.row(i).col(j).data[0] = A(i, j, 2); image.row(i).col(j).data[1] = A(i, j, 1); image.row(i).col(j).data[2] = A(i, j, 0); trimap.row(i).col(j).data[0] = B(i, j);works for me.
将mxarray转换为cv :: mat时,程序崩溃(Program crashes when converting mxarray to cv::mat)我试图写一个mex函数来调用另一个函数F. F使用opencv库,所以我需要将mxarray转换为cv :: mat。 我使用嵌套for循环来将每个像素值或向量的值分配给两个创建的cv :: mat。 该代码发布如下。
但是,运行for-loop一段时间后,程序崩溃。 我试图使用mexPrintf打印出如A(i,j,2)的值,并且值是正确的(无符号字符)。 来自Matlab的输入是uint8类型的矩阵,这就是我在cpp代码中将东西转换为无符号字符的原因。
我不知道它为什么会崩溃。 我也尝试使用visual studio的attach过程进行调试,但它似乎不起作用,并且每次我在Matlab中附加过程并调用mex函数时,我都会收到访问冲突消息。
void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]) { #define A_IN prhs[0] #define B_IN prhs[1] #define Y_out plhs[0] #define A(i,j,k) A[i+j*R+k*(R*C)] #define B(i,j) B[i+j*R] #define Y(i,j) Y[i+j*R] const int *d_pr = mxGetDimensions(A_IN); int C = d_pr[1]; int dims[2] = { R,C }; Y_out = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL); unsigned char *A, *B,*Y; A = (unsigned char *)mxGetData(A_IN); B = (unsigned char *)mxGetData(B_IN); Y = (unsigned char *)mxGetData(Y_out); cv::Mat image(R, C, CV_8UC3); cv::Mat trimap(R, C, CV_8UC1); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j); } } //some other stuff}
I'm trying to write a mex function to call another function F. F uses the opencv library, so I need to convert the mxarray into a cv::mat. I use a nested for loop to assign each pixel value or vector of values to the two created cv::mat. The code is posted below.
However, the program crashes after running the for-loop for a while. I tried to use mexPrintf to print out values such as A(i, j, 2) and the values are correct (unsigned char). The input from Matlab are matrices of type uint8, and this is the reason I cast things into unsigned char in the cpp code.
I have no idea why it is crashing. I also tried to use visual studio's attach process to debug, but it doesn't seem to work, and every time I attached the process and call the mex function in Matlab I just got a access violation message.
void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]) { #define A_IN prhs[0] #define B_IN prhs[1] #define Y_out plhs[0] #define A(i,j,k) A[i+j*R+k*(R*C)] #define B(i,j) B[i+j*R] #define Y(i,j) Y[i+j*R] const int *d_pr = mxGetDimensions(A_IN); int C = d_pr[1]; int dims[2] = { R,C }; Y_out = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL); unsigned char *A, *B,*Y; A = (unsigned char *)mxGetData(A_IN); B = (unsigned char *)mxGetData(B_IN); Y = (unsigned char *)mxGetData(Y_out); cv::Mat image(R, C, CV_8UC3); cv::Mat trimap(R, C, CV_8UC1); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j); } } //some other stuff}
最满意答案
更改for循环中的4行代码:
image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j);成:
image.row(i).col(j).data[0] = A(i, j, 2); image.row(i).col(j).data[1] = A(i, j, 1); image.row(i).col(j).data[2] = A(i, j, 0); trimap.row(i).col(j).data[0] = B(i, j);为我工作。
Changing the 4 lines of code inside the for-loop:
image.at<cv::Vec3b>(j, i)[0] = A(i, j, 2); image.at<cv::Vec3b>(j, i)[1] = A(i, j, 1); image.at<cv::Vec3b>(j, i)[2] = A(i, j, 0); trimap.at<unsigned char>(j,i) = B(i, j);into:
image.row(i).col(j).data[0] = A(i, j, 2); image.row(i).col(j).data[1] = A(i, j, 1); image.row(i).col(j).data[2] = A(i, j, 0); trimap.row(i).col(j).data[0] = B(i, j);works for me.
发布评论