Hi!
Got an annoying problem.
I have:
- A binary image containing the result of a canny edge detection.
- A Vector containing all points on a circle.
I want:
To draw a little dot on all points where the circle intersects a white line on the canny image.
The problem:
The points get scattered along the circle. I am accessing the wrong pixels.
The code:
//Return a vector of all the points on the given circle
vector<Point> points_on_circle( Point center, int radius )
{
vector<Point> ret;
int l = (int)I_CIRCLE_RADIUS * cos( M_PI / 4 );
//Top right quadrant
for( int x = -l; x < l; x++ )
{
int y = (int)sqrt( (double)(I_CIRCLE_RADIUS * I_CIRCLE_RADIUS) - (x * x) );
ret.push_back( Point( center.x - x, center.y - y ) );
}
//Top left quadrant
for( int x = -l; x < l; x++ )
{
int y = (int)sqrt( (double)(I_CIRCLE_RADIUS * I_CIRCLE_RADIUS) - (x * x) );
ret.push_back( Point( center.x - y, center.y - (-x) ) );
}
//Bottom left quadrant
for( int x = -l; x < l; x++ )
{
int y = (int)sqrt( (double)(I_CIRCLE_RADIUS * I_CIRCLE_RADIUS) - (x * x) );
ret.push_back( Point( center.x - (-x), center.y - (-y) ) );
}
//Bottom right quadrant
for( int x = -l; x < l; x++ )
{
int y = (int)sqrt( (double)(I_CIRCLE_RADIUS * I_CIRCLE_RADIUS) - (x * x) );
ret.push_back( Point( center.x - (-y), center.y - x ) );
}
return ret;
}
int main()
{
namedWindow( "Original", CV_WINDOW_AUTOSIZE );
namedWindow( "Binary", CV_WINDOW_AUTOSIZE );
moveWindow( "Binary", 570, 100 );
moveWindow( "Original", 0, 100 );
Point needle_center( NEEDLE_CENTER_X, NEEDLE_CENTER_Y );
vector<Point> i_points = points_on_circle( needle_center, I_CIRCLE_RADIUS );
//Step through images
for( int i = 1; i <= 21; i++ )
{
stringstream file;
file << "img/" << setw( 2 ) << setfill( '0' ) << i << ".png";
//Read image
Mat original = imread( file.str(), CV_LOAD_IMAGE_UNCHANGED );
circle( original, needle_center, 2, CV_RGB( 0, 255, 0 ), 1, CV_AA, 1 );
//Make a grayscale copy
Mat gray( original.size(), original.type() );
cvtColor( original, gray, CV_BGR2GRAY );
//Find edges in grayscale image
Mat edges( original.size(), CV_8UC1 );
Canny( gray, edges, CANNY_THRESHOLD, CANNY_THRESHOLD * 3, 3 );
imshow( "Binary", edges );
//Iterate all points of the circle in edges image
for( auto i_point : i_points )
{
if( (edges.at<uchar>( i_point )) != 0 )
circle( original, i_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
imshow( "Original", original );
}
if( (waitKey( 0 )) == 1048603 )
break;
}
destroyAllWindows();
return 0;
}
Any help is appreciated.
//John