Ask Your Question
2

how can I find the coordinates with the given step?

asked 2015-11-20 05:46:05 -0600

MValeriy gravatar image

updated 2020-10-28 02:40:36 -0600

I have the following picture from camera: http://www.pictureshack.ru/images/100...

I need: http://www.pictureshack.ru/images/643...

I tried first to find the lines with HoughLines , but results were not good races : http://www.pictureshack.ru/done_66361...

then, I considered myself as I can see the laser line . I've tried using Canny , there are too many other unnecessary lines . After that I read on the internet about HSV . as firstly I made trackable:

int iLowH = 0;
 int iHighH = 255;

 int iLowS = 0; 
 int iHighS = 255;

 int iLowV = 0;
 int iHighV = 255;

 //Create trackbars
 cvCreateTrackbar("LowH", "Control", &iLowH, 255); 
 cvCreateTrackbar("HighH", "Control", &iHighH, 255);

 cvCreateTrackbar("LowS", "Control", &iLowS, 255); 
 cvCreateTrackbar("HighS", "Control", &iHighS, 255);

 cvCreateTrackbar("LowV", "Control", &iLowV, 255); 
 cvCreateTrackbar("HighV", "Control", &iHighV, 255);

as the next I have with findcountors that Countor have found:

vector<vector<Point> > contours;
vector<Point> contour;
findContours( imgThresholded, contours, CV_RETR_TREE, CV_CHAIN_APPROX_NONE );
//findContours( edges, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
for ( size_t i = 0; i < contours.size(); i++)
{drawContours(imgOriginal,contours,i,color);}

und I have :

http://www.pictureshack.ru/images/750...

How do I find the coordinates at the intersection of the lines drawn by me and " lines " in the picture?

edit retag flag offensive close merge delete

Comments

Hi, I have one more question. today I have tried to work with an object that has many oblique. And that was also to be expected that I get such a result. You've probably even a piece of advice for me on how I can get good results here.

Img: http://www.pictureshack.ru/images/209...

result: http://www.pictureshack.ru/images/672...

MValeriy gravatar imageMValeriy ( 2015-11-25 09:33:34 -0600 )edit

You know how I can find the overall width of the object? My idea was that we can measure in steps 1 and where y> 388 (388 is the middle), and start counting again at the end where y = 388 finish. but this technique is too complex to calculations, the computer downloads and video shows even more. maybe try again with findсontours? or there may be other suitable function?

MValeriy gravatar imageMValeriy ( 2015-12-02 03:20:46 -0600 )edit

3 answers

Sort by » oldest newest most voted
4

answered 2015-11-20 20:12:31 -0600

updated 2015-11-25 16:58:20 -0600

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 ...
(more)
edit flag offensive delete link more

Comments

Hello, Thank you for your help. I tried to start your code. but comes error, if I ignore the error, I have an image, but it shows something wrong. X coordinate could still be ok, but all the same Y. Contour of the laser line is not detected. and program will only overlap between lines

http://www.pictureshack.ru/images/165...

MValeriy gravatar imageMValeriy ( 2015-11-23 04:38:12 -0600 )edit

what is your OpenCV version? what is the error message ?

sturkmen gravatar imagesturkmen ( 2015-11-23 05:15:07 -0600 )edit

I have 2.3.1(VS10) and 3.0.0(VS12), I have the errors in old version. with 3.0.0 there are problems, but I try to solve the

MValeriy gravatar imageMValeriy ( 2015-11-23 05:26:23 -0600 )edit

in version 3, there are problems even with cvtColor (src, CDST, COLOR_GRAY2BGR); I do not know why, have not worked so much with this.

MValeriy gravatar imageMValeriy ( 2015-11-23 05:29:31 -0600 )edit

please post all your code . let's check what is wrong.

sturkmen gravatar imagesturkmen ( 2015-11-23 05:38:40 -0600 )edit

I have taken only your code, and wanted to test. it runs in two versions to Rect _boundingRect; and then an error message: Unhandled exception at at 0x7595812F in Sample1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0026F540.

MValeriy gravatar imageMValeriy ( 2015-11-23 05:47:16 -0600 )edit

please check your compiler configuration. there is no problem with my code

sturkmen gravatar imagesturkmen ( 2015-11-23 05:51:03 -0600 )edit
2

answered 2015-11-20 06:11:05 -0600

thdrksdfthmn gravatar image

You have the coordinates of your lines (as p01, p02) and the coordinates of the found lines (as p11, p12). Now, all you need to do is to compute the intersection of the two (that is solving a pair of equations)

But you can also do as following:

  • draw the detected lines (you can use threshold + findContours and then fillPoly)
  • now, that you have the detected lines, you can draw yours (using line function with a larger thick)
  • after that you can do an erode with a cross kernel that will give you the intersection points/area
  • you can use moments (or you can do once more find contours and compute the mean of the contour's points, that is its center)
edit flag offensive delete link more
0

answered 2015-11-23 05:48:22 -0600

MValeriy gravatar image

updated 2015-11-25 16:59:45 -0600

Second alternative code:

#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;
}
edit flag offensive delete link more

Comments

cvtColor(src, cdst, COLOR_GRAY2BGR); must be cvtColor(src, cdst, COLOR_BGR2GRAY);

sturkmen gravatar imagesturkmen ( 2015-11-23 05:52:28 -0600 )edit

cdst = cdst > 127; // you must have a binary image for findContours

sturkmen gravatar imagesturkmen ( 2015-11-23 05:54:25 -0600 )edit

when I write so comes error: Unhandled exception at at 0x7595812F in Sample1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0033D584.

MValeriy gravatar imageMValeriy ( 2015-11-23 05:55:19 -0600 )edit

try the code above. i edited but can't run a test now.

sturkmen gravatar imagesturkmen ( 2015-11-23 05:59:46 -0600 )edit

cdst = cdst > 127; I have supplemented, but still problem with cvtColor

MValeriy gravatar imageMValeriy ( 2015-11-23 06:00:32 -0600 )edit

i am sorry i can't run the code now. and could not understand what is the problem. i will test your code in some hours . keep following.

sturkmen gravatar imagesturkmen ( 2015-11-23 06:05:45 -0600 )edit

Thanks for your help, I try something

MValeriy gravatar imageMValeriy ( 2015-11-23 06:09:17 -0600 )edit

again many thanks for your help. Code now runs flawlessly in old version. I have beautiful pictures and coordinates are displayed. in new OpenCV there are other mistakes to find contours

MValeriy gravatar imageMValeriy ( 2015-11-23 07:06:15 -0600 )edit

you could mark my answer as correct then

sturkmen gravatar imagesturkmen ( 2015-11-23 07:10:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-20 05:46:05 -0600

Seen: 1,046 times

Last updated: Nov 25 '15