Ask Your Question
0

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

asked 2014-07-10 11:17:56 -0500

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();

}

edit retag flag offensive close merge delete

Comments

1

The dimensions of the image are 600 x 600.

brycemh gravatar imagebrycemh ( 2014-07-10 11:18:32 -0500 )edit
2

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

unxnut gravatar imageunxnut ( 2014-07-10 12:08:33 -0500 )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 ( 2014-07-10 12:24:52 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-07-10 12:21:15 -0500

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.

edit flag offensive delete link more

Comments

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

brycemh gravatar imagebrycemh ( 2014-07-10 13:21:36 -0500 )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 ( 2014-07-11 08:53:52 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-07-10 11:17:56 -0500

Seen: 112 times

Last updated: Jul 10 '14