Multiple circles with a single call to cv::circle function
Hello everyone.
I'm new with OpenCV library, and I would like to use it to detect circle in a video stream captured from an iPad back camera. I figured out how to do it and with OpenCV, it can be done in less than 10 lines of code. But it doesn't work for me, and I think I missed something because of some weird behaviours I obtain.
Here is a very simple code that doesn't work:
void proccessImage(std::vector<unsigned char>& imageData, int width, int height)
{
// Create cv::Mat from std::vector<unsigned char>
Mat src(width, height, CV_8UC4, const_cast<unsigned char*>(imageData.data()));
Mat src_gray;
Mat final;
// Convert the original image from BGRA to gray
cvtColor(src, src_gray, CV_BGRA2GRAY);
// Draw a circle at position (300, 200) with a radius of 30
cv::Point center(300, 200);
circle(src_gray, center, 30.f, Scalar(0,0,255, 255), 3, 8, 0);
// Convert the gray image to RGBA
cvtColor(src_gray, final, CV_GRAY2RGBA);
// Reform the std::vector from cv::Mat data
std::vector<unsigned char> array;
array.assign((unsigned char*)final.datastart, (unsigned char*)final.dataend);
// Send final image data to GPU and draw it
}
For me, this should convert the image in gray and draw a simple circle at the specified position. But instead of that, I get this:
[the data from the iPad's back camera is correct and is gray, but the circle is drawn multiple times with weird stripes]
Do you know what happened?
Have you read the doc of circle function (it is a drawing function)? Please use hough transform for detection and draw them with
circle
Thanks for your answer thdrksdfthmn. I already read the documentation of circle function => "The function circle draws a simple or filled circle with a given center and radius."
So why do I have many circles drawn whereas I only called circle function once?
Have you tried to draw the circle over the original src image and see if the problem persists? I don't understand why your convert src to src_gray if you're going to convert it back later
Those circles are drawn by your code?! Interesting... Do you call proccessImage multiple time?
Sorry! It seems that the conversion is bad... if you look in detail, you'll notice that the lines of the circle are in different places... I suppose that the problem is here:
Scalar(0,0,255, 255)
, tryCV_RGB(0, 0, 255)
instead and see if it is the same@thdrksdfthmn: in fact I don't think that will work either, because it's trying to draw color over a binary image
Thank you very well for your help. To answer to your questions, I tried a simple version of the code above => here for the code
What I got is not better :'( (image)
PS: proccessImage is called each time we receive a new frame from the iPad's camera.
Where are you getting your output image from? I mean, are you using
imshow
with yourfinal
matrix? If not, I would suggest doing so, cause if that works, then the problem may be in the further drawing steps@Lorena GdL: I think I can't use
imshow
because I'm on iPad and it doesn't seem to be implemented for this platform.But if it's a problem further, why the image captured from the iPad's camera is correct? I also tried to draw a blue square on my image doing something like that:
And the square is drawn properly. This is why it lets me think that my output is correct.
@Noxalus: You're right. I don't really see the problem. The only thing I see is the radius of
circle
must be anint
and you're forcing it to be afloat
. That shouldn't be the problem, but you'd better try just in case