How should I use cvReshape?

I am trying to use cvReshape to have 2 versions of the same matrix data. For example, gray_img is a 100x100 matrix, and gray_line is a 10000x1 matrix, pointing to the same data, but with a different header. This is what I do in OpenCV following the documentation:

CvMat *  gray_img;
CvMat  gray_line_header;
CvMat * gray_line;
gray_img = cvCreateImage(100, 100, IPL_DEPTH_32F, 1);
gray_line = cvReshape(gray_img, &gray_line_header, 0, 10000);

It works as intentional, but I feel that it is difficult to read, not elegant. If I understand correctly, gray_line will point to gray_line_header, so I feel that there is an additional variable here. Is it possible to do what I want without declaring the matrix header or only with ads from 2 (instead of 3)?

thank

0
source share
2 answers

Are you committed to the open interface of OpenCV? Using the C ++ interface you can do this:

cv::Mat grayImg(100, 100, CV_32FC1);
cv::Mat grayLine(grayImg);
grayLine.reshape(1,10000); //1 column, 10000 rows

, .

+3

.

gray_line gray_line_header.

, , ......

:

CvMat *gray_img, *gray_line;
gray_img = cvCreateMat(20, 20, CV_8U);
gray_line = cvReshape(gray_img, gray_line, 0, 40);
cvShowImage("after reshape", gray_line);
cvWaitKey(0);

, , ...

, 2

  • ,

    CvMat *gray_img, gray_line_data;
    gray_img = cvCreateImage(100, 100, IPL_DEPTH_32F, 1);
    cvReshape(gray_img, &gray_line_data, 0, 10000);
    CvMat *gray_line = &gray_line_data;
    
  • cvCreateMatHeader cvReshape

    CvMat *gray_img, *gray_line;
    gray_img  = cvCreateMat(20, 20, CV_8U);
    // initialize gray_img (omitted)
    gray_line = cvCreateMatHeader(40, 1, gray_img->type);
    gray_line = cvReshape(gray_img, gray_line, 0, 40); // maybe just data pointer reassigning?
    
+1

All Articles