Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp"

include <iostream>

using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src; src = imread("1.jpg", 0); if(src.empty()) { return -1; } Mat cdst;

cvtColor(src, cdst, COLOR_GRAY2BGR);

                                    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(cdst.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(255,255,255));
                                                    }
                                                }
                                            }
                                        }

imshow("source", src); imshow("d", cdst);

waitKey();

return 0; }

include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp"

include <iostream>

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
 Mat src;
src = imread("1.jpg", 0);
 if(src.empty())
 {
        return -1;
 }
  Mat cdst;

cdst;

cvtColor(src, cdst, COLOR_GRAY2BGR);

COLOR_GRAY2BGR);
                                     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(cdst.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(255,255,255));
                                                     }
                                                 }
                                             }
                                            }

 imshow("source", src);
 imshow("d", cdst);

 waitKey();

 return 0;
}

imshow("source", src); imshow("d", cdst);

waitKey();

return 0; }

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
 Mat src;
src = imread("1.jpg", 0);
imread("1.jpg");
 if(src.empty())
 {
        return -1;
 }
  Mat cdst;

 cvtColor(src, cdst, COLOR_GRAY2BGR);

                                        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(cdst.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(255,255,255));
                                                        }
                                                    }
                                                }
                                            }

 imshow("source", src);
 imshow("d", cdst);

 waitKey();

 return 0;
}
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
 Mat src;
src = imread("1.jpg");
 if(src.empty())
 {
        return -1;
 }
  Mat cdst;

 cvtColor(src, cdst, COLOR_GRAY2BGR);

     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(cdst.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(255,255,255));
                                                        }
                                                    }
                                                }
                                            }

 imshow("source", src);
 imshow("d", cdst);

 waitKey();

 return 0;
}
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
 Mat src;
src = imread("1.jpg");
 if(src.empty())
 {
        return -1;
 }
  Mat cdst;

 cvtColor(src, cdst, COLOR_GRAY2BGR);
COLOR_BGR2GRAY); // edited
 cdst = cdst > 127;                                 // added

    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(cdst.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(255,255,255));
                                                        }
                                                    }
                                                }
                                            }

 imshow("source", src);
 imshow("d", cdst);

 waitKey();

 return 0;
}

Second alternative code:

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

using namespace cv;
using namespace std;
 int main(int main( int argc, char** argv)
argv )
{
  Mat src;
src,gray;
    src = imread("1.jpg");
imread(argv[1]);
    if(src.empty())
 {
        return -1;
 }
  Mat cdst;

 cvtColor(src, cdst, COLOR_BGR2GRAY); // edited
 cdst = cdst 
    cvtColor( src, gray, COLOR_BGR2GRAY );
    gray = gray > 127;                                 // added

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(cdst.clone(), 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)
             {
            // 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(255,255,255));
                                        %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("source",     imshow("gray", gray);
    imshow("result", src);
 imshow("d", cdst);

 waitKey();

   waitKey(0);
    return 0;
}