Sumanth
Sumanth

Reputation: 109

OpenCV in C - drawing a circle that should appear only in upper half of the image?

I have to draw a circle for several images. For each image to the radius of curvature is different with a constant center.

The problem is : no matter how big the circle is it shouldn't cross to upper half of the image. It's OK if it becomes invisible or only a part of it is visible in the lower half.

I am using OpenCV 2.4.4 in C lang. The values for the circle is found by:

 for(angle1 = 0; angle1<360; angle1++)
  {
    x [angle1]= r * sin(angle1) + axis_x;
    y [angle1]= r * cos(angle1) + axis_y;
  }

FYI:

 cvCircle( img,center_circle, r,cvScalar( 0, 0, 255,0 ),2,8,0);

Draws circle in the entire image. Which I don't want to happen.

How can I do it? Rem: no part of the circle should appear in upper half of the image. And the code should be in OpenCV's C lang.

In MALTAB is pretty easy. I only have to select the pixels and map them on the image. I am new to OpenCV and operations like img->data.i/f/s/db[50] =50; is showing error.

Upvotes: 0

Views: 367

Answers (1)

sgarizvi
sgarizvi

Reputation: 16796

A pretty naive approach is to create a copy of the upper half of image, draw the complete circle, and then copy back the upper half to original image. This may not be the best approach but it works. Here is how it can be achieved:

void drawCircleLowerHalf(IplImage* image, CvPoint center, int radius, CvScalar color, int thickness, int line_type, int shift)
{
    CvRect roi = cvRect(0,0,image->width, image->height/2);
    IplImage* upperHalf = cvCreateImage(cvSize(image->width, image->height/2), image->depth, image->nChannels);
    cvSetImageROI(image, roi);
    cvCopy(image,upperHalf);
    cvResetImageROI(image);

    cvCircle(image, center, radius, color, thickness, line_type, shift);

    cvSetImageROI(image, roi);
    cvCopy(upperHalf, image);
    cvResetImageROI(image);

    cvReleaseImage(&upperHalf);
}

Just call this function with the same arguments as of cvCircle.

Upvotes: 2

Related Questions