Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually :

Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);

Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );

for (int i=0; i<360; i+=20)
{
    double s = sin(i*CV_PI/180);
    double c = cos(i*CV_PI/180);
    Point p2(cen.x+s*150, cen.y+c*150);

    LineIterator it(ocv, cen, p2, 8);
    Rect bounds(0, 0, ocv.cols, ocv.rows);
    while(bounds.contains(it.pos()))
    {
        Vec3b & pixel = ocv.at<Vec3b>(it.pos());
        // if you stare really hard, you'll see the cheat ;)
        if (pixel[0] > 50) // non dark(it's not really black in the image!)
           pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
        else
           break;
        it++;
    }
}
imshow("lines", ocv);

image description

it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually :

Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);

Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );

for (int i=0; i<360; i+=20)
{
    double s = sin(i*CV_PI/180);
    double c = cos(i*CV_PI/180);
    Point p2(cen.x+s*150, cen.y+c*150);
cen.y+c*150); // any radius will do, we just want the direction

    LineIterator it(ocv, cen, p2, 8);
    Rect bounds(0, 0, ocv.cols, ocv.rows);
    while(bounds.contains(it.pos()))
    {
        Vec3b & pixel = ocv.at<Vec3b>(it.pos());
        // if you stare really hard, you'll see the cheat ;)
        if (pixel[0] > 50) // non dark(it's not really black in the image!)
           pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
        else
           break;
        it++;
    }
}
imshow("lines", ocv);

image description

it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :

Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);

Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );

for (int i=0; i<360; i+=20)
{
    double s = sin(i*CV_PI/180);
    double c = cos(i*CV_PI/180);
    Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction

    LineIterator it(ocv, cen, p2, 8);
    Rect bounds(0, 0, ocv.cols, ocv.rows);
    while(bounds.contains(it.pos()))
    {
        Vec3b & pixel = ocv.at<Vec3b>(it.pos());
        // if you stare really hard, you'll see the cheat ;)
        if (pixel[0] > 50) // non dark(it's not really black in the image!)
           pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
        else
           break;
        it++;
    }
}
imshow("lines", ocv);

image description