Ask Your Question
1

Convert Mat.type and convert Mat to vector

asked 2016-03-22 07:26:09 -0600

Nbb gravatar image

updated 2016-03-22 07:29:33 -0600

Hello

1) Given a Mat M and vector< double > v, how do I extract the 1st column and store it in a vector ? I have tried M.col(0).copyTo(v) but I am getting an error "Heap Corruption Detected". I am however, able to do so if v is a Mat itself. I just want to know why I cant convert it to a vector.

2) Do i have to take note of anything when using convertTo(OutputArray m, int rtype, double alpha=1, double beta=0 ) ? Like if i were to convert 64F to 8U for display purposes, do I have to perform the scaling operation or does the function takes care of it i.e. I do not have to worry about anything.

3) Is it more efficient to process vectors or Matrices ? Eg a vector< vector< int >> vs a 2D Mat and a vector< int > vs a 1D Mat (row or column Mat). I know http://docs.opencv.org/2.4/doc/tutori... covers how to process matrices efficiently but I also want to know if it is faster to process a vector or a matrix.

Thanks !

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-03-22 10:16:28 -0600

1 - Not sure why that is happening, but here are a few possibilities:

  • OpenCV's Mat deals automatically with memory allocation so you don't have to do it yourself. The reason why it is working when you replace vector for Mat is because of that automatic memory handling that is going on. Make sure your Mat M is of type CV_64F to match your vector<double>.

  • OpenCV stores pixels in memory in form of a 1D array (not 2D). So, a cv::Mat is actually a very big row that is later handled in a way to mimic a 2-D array. It is likely that Mat::col() operator returns a non-continuous Mat, so the behaviour when trying to copy it into a std::vector is undefined. Verify if M.isContinuous() returns true. If it doesn't, you can make it continuous by calling M = M.clone() (which creates a new space on memory for Mat).

2 - Yes, if you want to convert mat to make it "visualizable" you must normalize it before converting. In a Mat of CV_64F type, any value above 255 will stay 255 when you convert it to 8-Bit. So, you need to call cv::normalize(Source, Destination, 0, 255, CV_MINMAX). This will transform Mat so that all values are between 0 and 255. Then you can transform it into 8-bit and visualize it.

3 - I've never compared, but I believe there is no difference in processing a vector or a Mat. As I wrote before, a cv::Mat is just a big array. Nothing like making the real tests though.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-22 07:26:09 -0600

Seen: 1,687 times

Last updated: Mar 22 '16