1

I am using opencv's matlab library to use matlab images and perform the HoughCircles algorithm on it.

I want to dynamically create matlab images instead of imread-ing them.

With that image I want to plot an array of x,y coordinates I have collected.

I can 'declare' an image by saying

Mat img

But that's pretty much as far as I got. I cannot find a function to plot points into it.

If anyone has any insight, I'd be much obliged!!

Vigrond
  • 7,820
  • 4
  • 24
  • 43

1 Answers1

2

Check cv::Mat docs and take a look at the several constructors that it offers:

To initialize a cv::Mat from a 2D array, you could do:

float data[2][2] = { {1,3,5,7,9}, {2,4,6,8,10} }; 
cv::Mat img = cv::Mat(2, 5, CV_32FC1, &data);

Or, if you need to access the pixels individually:

You might also be interested at reading:

Community
  • 1
  • 1
karlphillip
  • 87,606
  • 33
  • 227
  • 395
  • I think my head is going to explode from trying to learn all this in one day. But just to clarify: This line: cv::Mat img = cv::Mat(2, 5, CV_32FC1, &data); Means a 2x5 Image of single 32f values, loaded with the data above. I am trying to understand how that data array fits into that image. Where do the values in data go to in the image matrix? – Vigrond Oct 01 '12 at 00:02
  • 1
    The *values* of the image are stored in the `data` field (which is a pointer), and can be accessed with: `img.data` – karlphillip Oct 01 '12 at 00:07
  • I guess to specify my issue. I have a std::vector coordinates. I also have a Mat img = cv::Mat::zeros(1280,720, CV_8UC1); I am trying to figure out how to easily initialize the coordinates in my vector to 1 in the img. My first idea was iterating through and doing .at() =, but I figured there is an easier way – Vigrond Oct 01 '12 at 00:07
  • 1
    That is probably the most efficient way. If there are other solutions, they will involve calling CV methods which will do what you did manually, plus they'll have the overhead of adding function calls to the stack. – karlphillip Oct 01 '12 at 00:14
  • Awesome. Thanks for your help, really appreciate it! – Vigrond Oct 01 '12 at 00:15
  • I'm getting a "Error: no operator '=' matches these operands" when I do img.at(point) = 1; Do you know what causes that? – Vigrond Oct 01 '12 at 00:57
  • Yeah. If your mat is single-channel, then you should be doing something like: `img.at(point.x, point.y) = 1`. – karlphillip Oct 01 '12 at 01:02