Schnigges
Schnigges

Reputation: 1326

Weird behaviour of cv::circle

I'm trying to draw a couple of circles with OpenCV 3 onto an image which is obtained from the Kinect v2 sensor.

There seems to be a strange bug with cv::circle or I don't understand how the function works. Let's look at some code:

if(kinectDataManager.IsColorStreamEnabled())
{
    cv::Mat colorFrameMat = kinectDataManager.GetFrame().GetColorFrame()  

    cv::imshow("Color", colorFrameMat)
}

This code works perfectly fine, and using the ImageWatch Visual Studio Plugin for inspecting OpenCV images, I can see that the colorFrameMat matrix is not corrupted.

Let's look at some more code:

if(kinectDataManager.IsColorStreamEnabled())
{
    cv::Mat colorFrameMat = kinectDataManager.GetFrame().GetColorFrame()  

    int radius = 2;
    int y = 1068;

    for (int x = 0; x < 1920; ++x)
    {
        cv::circle(colorFrameMat, cv::Point(x,y), radius, cv::Scalar(255, 0, 0), -1, CV_AA);
    }

    cv::imshow("Color", colorFrameMat)
}

After the loop execution has finished, the ImageWatch plugin reveals that the last rows of the image are missing. Strangely, the program still executes. However, for different values of y, the program crashes due to access violations, e.g. for y = 1067, the program crashes for x = 1917. For y = 1069, it crashes at x = 988.

Does anyone have an idea what the issue might be?

EDIT: The ImageWatch plugin of course reveals that the last rows are missing, as circles are drawn at these positions from left to right, sorry for the mistake!!

EDIT2: After storing one frame and reading it in, the cv::circle method with the identical code works fine:

cv::Mat test = cv::imread("test.jpg", CV_LOAD_IMAGE_COLOR);
cv::namedWindow("test", CV_WINDOW_NORMAL);
int radius = 10;
int y = 1067;

for (int x = 0; x < 1920; ++x)
{
    cv::circle(test, cv::Point(x, y), radius, cv::Scalar(0, 0, 255, 255), -1, CV_AA);
}

cv::imshow("test", test);
cv::waitKey(0);

Upvotes: 0

Views: 471

Answers (2)

Schnigges
Schnigges

Reputation: 1326

The Kinect SDK provides only functionality to read a 4-channel image (i.e. RGBA), however, the cv::circle functions seems to crash in a strange way for these kind of images. By dropping the alpha channel with a call to cvtImage, I could resolve the issue.

Upvotes: 3

boaz001
boaz001

Reputation: 397

Since you say the image looks fine before the cv::circle call it is possible that GetColorFrame() returns data that changes while the loop is running. Try:

  • a GetColorFrame().clone() to see if this fixes the issue, or
  • change the way GetColorFrame works.

Upvotes: 0

Related Questions