Ask Your Question

Revision history [back]

another approach :

you can define each line as a cv::Rect and you can find intersections of boundingRect of each contour by & operator.

the code below shows the way:

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src,gray;
    src = imread(argv[1]);
    if(src.empty())
        return -1;

    cvtColor( src, gray, COLOR_BGR2GRAY );
    gray = gray > 127;

    vector<Rect> rects_of_lines;

    Rect rect_of_line;

    rect_of_line.x = src.cols /4;
    rect_of_line.y = 0;
    rect_of_line.width = 1;
    rect_of_line.height = src.rows;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = (src.cols /2) + src.cols /4;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = src.cols /2;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = 0;
    rect_of_line.y = src.rows / 2;
    rect_of_line.width = src.cols;
    rect_of_line.height = 1;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    vector<vector<Point> > contours;

    findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    Rect _boundingRect;

    for (size_t i = 0; i < contours.size(); ++i)
    {
        if( contourArea( contours[i] ) > 20 )
        {
            _boundingRect = boundingRect( Mat(contours[i]) );

            for( int j = 0; j < rects_of_lines.size(); j++ )
            {
                Rect intersection = _boundingRect & rects_of_lines[j];
                if(intersection.height > 0)
                {
                // to centralize intersection rect
                intersection.x = intersection.x + (intersection.width / 2 );
                intersection.y = intersection.y + (intersection.height / 2 );
                intersection.width = 1;
                intersection.height = 1;
                rectangle( src, intersection, Scalar(255,0,0),2);
                putText(src, format("x = %d , y = %d",intersection.x,intersection.y),Point(intersection.x,intersection.y),CV_FONT_HERSHEY_COMPLEX,0.5,Scalar(0,255,0));
                }
            }
        }
    }
    imshow("result", src);
    waitKey(0);
    return 0;
}

part of result image :

image description

another approach :

you can define each line as a cv::Rect and you can find intersections of boundingRect of each contour by and operator ( & ).

& operator(note: you don't need to draw lines to find intersections).

the code below shows the way:

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src,gray;
    src = imread(argv[1]);
    if(src.empty())
        return -1;

    cvtColor( src, gray, COLOR_BGR2GRAY );
    gray = gray > 127;

    vector<Rect> rects_of_lines;

    Rect rect_of_line;

    rect_of_line.x = src.cols /4;
    rect_of_line.y = 0;
    rect_of_line.width = 1;
    rect_of_line.height = src.rows;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = (src.cols /2) + src.cols /4;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = src.cols /2;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = 0;
    rect_of_line.y = src.rows / 2;
    rect_of_line.width = src.cols;
    rect_of_line.height = 1;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    vector<vector<Point> > contours;

    findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    Rect _boundingRect;

    for (size_t i = 0; i < contours.size(); ++i)
    {
        if( contourArea( contours[i] ) > 20 )
        {
            _boundingRect = boundingRect( Mat(contours[i]) );

            for( int j = 0; j < rects_of_lines.size(); j++ )
            {
                Rect intersection = _boundingRect & rects_of_lines[j];
                if(intersection.height > 0)
                {
                // to centralize intersection rect
                intersection.x = intersection.x + (intersection.width / 2 );
                intersection.y = intersection.y + (intersection.height / 2 );
                intersection.width = 1;
                intersection.height = 1;
                rectangle( src, intersection, Scalar(255,0,0),2);
                putText(src, format("x = %d , y = %d",intersection.x,intersection.y),Point(intersection.x,intersection.y),CV_FONT_HERSHEY_COMPLEX,0.5,Scalar(0,255,0));
                }
            }
        }
    }
    imshow("result", src);
    waitKey(0);
    return 0;
}

part of result image :

image description

another approach :

you can define each line as a cv::Rect and you can find intersections of boundingRect of each contour by and operator ( & ).

(note: you don't need to draw lines to find intersections)

the code below shows the way:

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src,gray;
    src = imread(argv[1]);
    if(src.empty())
        return -1;

    cvtColor( src, gray, COLOR_BGR2GRAY );
    gray = gray > 127;

    vector<Rect> rects_of_lines;

    Rect rect_of_line;

    rect_of_line.x = src.cols /4;
    rect_of_line.y = 0;
    rect_of_line.width = 1;
    rect_of_line.height = src.rows;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = (src.cols /2) + src.cols /4;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = src.cols /2;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = 0;
    rect_of_line.y = src.rows / 2;
    rect_of_line.width = src.cols;
    rect_of_line.height = 1;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    vector<vector<Point> > contours;

    findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    Rect _boundingRect;

    for (size_t i = 0; i < contours.size(); ++i)
    {
        if( contourArea( contours[i] ) > 20 )
        {
            _boundingRect = boundingRect( Mat(contours[i]) );

            for( int j = 0; j < rects_of_lines.size(); j++ )
            {
                Rect intersection = _boundingRect & rects_of_lines[j];
                if(intersection.height > 0)
                {
                // to centralize intersection rect
                intersection.x = intersection.x + (intersection.width / 2 );
                intersection.y = intersection.y + (intersection.height / 2 );
                intersection.width = 1;
                intersection.height = 1;
                rectangle( src, intersection, Scalar(255,0,0),2);
                putText(src, format("x = %d , y = %d",intersection.x,intersection.y),Point(intersection.x,intersection.y),CV_FONT_HERSHEY_COMPLEX,0.5,Scalar(0,255,0));
                }
            }
        }
    }
    imshow("result", src);
    waitKey(0);
    return 0;
}

part of result image :

image description

EDIT :

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src,gray;
    src = imread(argv[1]);
    if(src.empty())
        return -1;

    cvtColor( src, gray, COLOR_BGR2GRAY );
    gray = gray > 127;
    vector<Rect> rects_of_lines;

    Rect rect_of_line;

    rect_of_line.x = src.cols /4;
    rect_of_line.y = 0;
    rect_of_line.width = 1;
    rect_of_line.height = src.rows;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = (src.cols /2) + src.cols /4;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = src.cols /2;
    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    rect_of_line.x = 0;
    rect_of_line.y = src.rows / 2;
    rect_of_line.width = src.cols;
    rect_of_line.height = 1;

    rects_of_lines.push_back( rect_of_line );
    rectangle( src, rect_of_line, Scalar(0,0,255));

    vector<vector<Point> > contours;

    findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    Rect _boundingRect;
    gray = 0;
    for (size_t i = 0; i < contours.size(); ++i)
    {
        if( contourArea( contours[i] ) > 20 )
        {
            drawContours(gray,contours,i,Scalar(255),-1);
            _boundingRect = boundingRect( Mat(contours[i]) );

            for( int j = 0; j < rects_of_lines.size(); j++ )
            {
                Mat l = gray(rects_of_lines[j]);
                vector<Point> pts;
                findNonZero(l,pts);

                _boundingRect = boundingRect( pts );
                _boundingRect.x = rects_of_lines[j].x;
                Rect intersection = _boundingRect & rects_of_lines[j];
                if(intersection.height > 0)
                {
                    putText(src, format("x = %d , y = %d",intersection.x,intersection.y),Point(intersection.x,intersection.y),CV_FONT_HERSHEY_COMPLEX,1,Scalar(255,0,0));
                    //rectangle( src, intersection, Scalar(0,255,0),5);
                }

            }
        }
    }
    imshow("gray", gray);
    imshow("result", src);
    waitKey(0);
    return 0;
}