Ask Your Question
0

Multiple circles with a single call to cv::circle function

asked 2015-05-26 10:08:00 -0500

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:

image description

[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?

edit retag flag offensive close merge delete

Comments

Have you read the doc of circle function (it is a drawing function)? Please use hough transform for detection and draw them with circle

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-05-26 10:33:24 -0500 )edit

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?

Noxalus gravatar imageNoxalus ( 2015-05-26 10:47:13 -0500 )edit
1

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

LorenaGdL gravatar imageLorenaGdL ( 2015-05-26 10:51:19 -0500 )edit

Those circles are drawn by your code?! Interesting... Do you call proccessImage multiple time?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-05-26 10:52:08 -0500 )edit

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), try CV_RGB(0, 0, 255) instead and see if it is the same

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-05-26 10:55:18 -0500 )edit

@thdrksdfthmn: in fact I don't think that will work either, because it's trying to draw color over a binary image

LorenaGdL gravatar imageLorenaGdL ( 2015-05-26 10:58:38 -0500 )edit

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.

Noxalus gravatar imageNoxalus ( 2015-05-26 11:11:50 -0500 )edit

Where are you getting your output image from? I mean, are you using imshow with your finalmatrix? If not, I would suggest doing so, cause if that works, then the problem may be in the further drawing steps

LorenaGdL gravatar imageLorenaGdL ( 2015-05-26 11:25:12 -0500 )edit

@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:

for(auto i = 0; i < (width * height) * 4; i += 4)
{
    auto x = (i / 4) % width;
    auto y = (i / 4) / width;

    if ((x >= (width / 2.f) - 100 && x <= (width / 2.f) + 100) ||
      (y >= (height / 2.f) - 100 && y <= (height / 2.f) + 100))
    {
      src.data[i] = (char)255;
    }
}

And the square is drawn properly. This is why it lets me think that my output is correct.

Noxalus gravatar imageNoxalus ( 2015-05-26 11:44:01 -0500 )edit

@Noxalus: You're right. I don't really see the problem. The only thing I see is the radius of circle must be an int and you're forcing it to be a float. That shouldn't be the problem, but you'd better try just in case

LorenaGdL gravatar imageLorenaGdL ( 2015-05-26 11:57:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-28 04:52:09 -0500

Thanks for your help, I finally figured out what happen, and it's my entire fault...

When you create a new Mat you need to pass it the image's height as first argument, and not width. The circle is drawn properly if I switch the arguments.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-05-26 10:08:00 -0500

Seen: 859 times

Last updated: May 26 '15