Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Accessing pixels along a circle

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

Accessing pixels along a circle

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.pixels. image description

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

Accessing pixels along a circle

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. image description

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> i_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, 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() CV_8UC1 );
        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 pixels in edges image
circle
        for( auto i_point : i_points )
        {
            cout << i_point << endl;
            if( (edges.at<uchar>( i_point )) != 0 )
                circle( original, i_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
         }

        imshow( "Binary", edges );
        imshow( "Original", original );
        }

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. image description

The code:

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> i_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, i_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto i_point : i_points )
        {
            cout << i_point << endl;
            if( (edges.at<uchar>( i_point )) != 0 )
                circle( original, i_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );
        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. image description

The code:

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> i_points;
circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, i_points circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto i_point current_point : i_points circle_points )
        {
            cout << i_point << endl;
            if( (edges.at<uchar>( i_point current_point )) != 0 )
                circle( original, i_point, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );
         if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. image description

The code:

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. I noticed that the pixel values varies. Even if I use the normalize function on my edges image I still get a couple of pixels with strange values. image description

The code:

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. I noticed that the pixel values varies. Even if I use the normalize function on my edges image to 0 and 255, I still get a couple of pixels with strange values. image description

The code:

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. I noticed that the pixel values varies. Even if I normalize my edges image to 0 and 255, I still get a couple of pixels with strange values. image description

The code: #include <iostream> #include <iomanip> #include <sstream> #include <opencv2 highgui="" highgui.hpp=""> #include <opencv2 imgproc="" imgproc.hpp="">

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. I noticed that the pixel values varies. Even if I normalize my edges image to 0 and 255, I still get a couple of pixels with strange values. image description

The code:

#include <iostream>
 #include <iomanip>
 #include <sstream>
 #include <opencv2 highgui="" highgui.hpp="">
    <opencv2/highgui/highgui.hpp>
#include <opencv2 imgproc="" imgproc.hpp="">

<opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. I noticed that the pixel values varies. Even if I normalize my edges image to 0 and 255, I still get a couple of pixels with strange values. image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Update

Changed loop to this:

for( int y = 0; y < edges.size().height; y++ )
{
    for( int x = 0; x < edges.size().width; x++ )
    {
        int pixel_value = edges.at<uchar>( y, x ) > 0 ? 1 : 0;
    if( pixel_value == 1 )
        circle( original, Point( x, y ), 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
}
}

Which resulted in this: image description

Accessing pixels along a circle

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 looking at the wrong pixels. Loading the image with different flags doesn't help. I noticed that the pixel values varies. Even if I normalize my edges image to 0 and 255, I still get a couple of pixels with strange values. image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Update

Changed loop to this:

for( int y = 0; y < edges.size().height; y++ )
{
    for( int x = 0; x < edges.size().width; x++ )
    {
        int pixel_value = edges.at<uchar>( y, x ) > 0 ? 1 : 0;
     if( pixel_value == 1 )
         circle( original, Point( x, y ), 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
 }
}

Which resulted in this: image description

Accessing pixels along a circle

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 I can use the points get scattered along with draw functions such as circle(). But when I use them with Mat::at() I end up accessing pixels outside the circle. I am looking at the wrong pixels. Loading the image with image. It is as if they are different flags doesn't help. I noticed that the pixel values varies. Even if I normalize my edges image to 0 and 255, I still get a couple of pixels with strange values. coordinates. image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Update

Changed loop to this:

for( int y = 0; y < edges.size().height; y++ )
{
    for( int x = 0; x < edges.size().width; x++ )
    {
        int pixel_value = edges.at<uchar>( y, x ) > 0 ? 1 : 0;
        if( pixel_value == 1 )
            circle( original, Point( x, y ), 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
    }
}

Which resulted in this: image description

Accessing pixels along a circle

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:

I can use the points with draw functions such as circle(). But when I use them with Mat::at() I end up accessing pixels outside the image. It is as if they are use different coordinates. image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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:

I can use the points with draw functions such as circle(). But when I use them with Mat::at() I end up accessing pixels outside the image. It is as if they use different coordinates. I need to translate the points before using at. But to what? image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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:

I can use the points with draw functions such as circle(). But when I use them with Mat::at() I end up accessing pixels outside the image. It is as if I need to translate the points before using at. But to what? image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John

Accessing pixels along a circle

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:

I can use the points with draw functions such as circle(). But when I use them with Mat::at() I end up accessing pixels outside the image. It is as if I need to translate the points before using at. But to what? image description

The code:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define NEEDLE_CENTER_X 565
#define NEEDLE_CENTER_Y 602
#define I_CIRCLE_RADIUS 100
#define CANNY_THRESHOLD 70

using namespace std;
using namespace cv;

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

    //Get points in circle
    Size axes( I_CIRCLE_RADIUS, I_CIRCLE_RADIUS );
    vector<Point> circle_points;
    ellipse2Poly( needle_center, axes, 0, 0, 360, 1, circle_points );

    //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(), CV_8UC1 );
        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 );

        //Iterate pixels in circle
        for( auto current_point : circle_points )
        {
            if( (edges.at<uchar>( current_point )) != 0 )
                circle( original, current_point, 1, CV_RGB( 255, 0, 0 ), 1, CV_AA, 1 );
        }

        imshow( "Binary", edges );
        imshow( "Original", original );

        if( (waitKey( 0 )) == 1048603 )
            break;
    }

    destroyAllWindows();

    return 0;
}

Any help is appreciated.

//John