First time here? Check out the FAQ!

Ask Your Question
0

Please tell me what's wrong with my code! New OpenCV User

asked Jul 10 '14

brycemh gravatar image

Below is my code. It is supposed to draw lines from the center of the circle to the edge of the circle, but some of the lines are too short while others are too long. Does anyone have a fix for this?

cvNamedWindow("Shapes",CV_WINDOW_AUTOSIZE);
//Image structure
IplImage* shape=cvLoadImage("white2.jpg");

//Drawing a Rectangle
int x = shape->width;
int y = shape->height;
int m = 0;
int l = 2;
int q = 300;
int z = shape->width / l;
int a = shape->height / l;
int b = 10;
int f = -255;
int g = 255;

cvRectangle(shape,cvPoint(x,m),cvPoint(m,y),CV_RGB(m,g,m),f,g);

//Drawing a Circle
cvCircle(shape,cvPoint(z,a),q,CV_RGB(m,m,g),f);
cvCircle(shape,cvPoint(z,a),b,CV_RGB(m,g,m),f);




for(int o = 2; o < 800; o+=45){
for(int n = 2; n < 800; n +=45){
    cvLine(shape, cvPoint(z,a), cvPoint(o,n), CV_RGB(0,255,0),1,8);     
    }
}

//Showing the image
cvShowImage("Shapes",shape);
//Escape Sequence
cvWaitKey(0);
//CleanUp
cvReleaseImage(&shape);
cvDestroyAllWindows();

}

Preview: (hide)

Comments

1

The dimensions of the image are 600 x 600.

brycemh gravatar imagebrycemh (Jul 10 '14)edit
2

Think about converting to C++ interface instead of legacy interface that you are using.

unxnut gravatar imageunxnut (Jul 10 '14)edit
1

the other answer showed you, how to get a point for a certain angle. look at that again, and make 1 loop over angles, not 2 for x and y

berak gravatar imageberak (Jul 10 '14)edit

1 answer

Sort by » oldest newest most voted
1

answered Jul 10 '14

unxnut gravatar image

In the following loop, you are drawing lines from circle center to arbitrary points and not to the edge of circle.

for(int o = 2; o < 800; o+=45){
for(int n = 2; n < 800; n +=45){
    cvLine(shape, cvPoint(z,a), cvPoint(o,n), CV_RGB(0,255,0),1,8);     
    }

The distance between points (z,a) and (o,n) is not q or b. Once you fix that, you should be good.

Preview: (hide)

Comments

@unxnut how do I find the distance between these two points?

brycemh gravatar imagebrycemh (Jul 10 '14)edit

A simple way will be to get Euclidean distance as sqrt ( sqr ( x1 - x2 ) + sqr ( y1 - y2 ) ). Please also look at the answer pointed to by @berak in the comment above.

unxnut gravatar imageunxnut (Jul 11 '14)edit

Question Tools

Stats

Asked: Jul 10 '14

Seen: 229 times

Last updated: Jul 10 '14