Ask Your Question
0

calculate the distance (pixel) between the two edges ( lines) ? [closed]

asked 2015-10-27 02:24:19 -0500

MValeriy gravatar image

updated 2015-10-27 09:27:52 -0500

Good day! I have a question. I get the picture from the camera using OPENCV, and using functions Canny ROI and get the following result ( Picture ) . as much as possible , or whether it is possible to calculate the distance (pixel) between the two edges ( lines) ? I will be very grateful for the help image description

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by MValeriy
close date 2015-11-25 00:51:07.659989

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-10-27 02:45:16 -0500

updated 2015-10-28 09:57:54 -0500

to calculate distance between two points you can use the function below

static double distanceBtwPoints(const cv::Point a, const cv::Point b)
{
    double xDiff = a.x - b.x;
    double yDiff = a.y - b.y;

    return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}

also you can see an example code using this function here

EDIT : i learned from LBerger that norm function do it.

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

using namespace cv;
using namespace std;

// calculates distance between two points
static double distanceBtwPoints(const cv::Point a, const cv::Point b)
{
    double xDiff = a.x - b.x;
    double yDiff = a.y - b.y;

    return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}

int main( int argc, char** argv )
{
Point pt0(200,200);
Point pt1(100,100);

cout << "distanceBtwPoints result : " << distanceBtwPoints(pt0,pt1) << endl;

cout << "norm result " << norm(pt0-pt1);

}

EDIT 2,3 :

look at the code below. maybe it give you another idea using findContours and boundingRect you can get widht and heigth of square objects like result image:

image description

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 50, 3);

        vector<vector<Point> > contours;
        findContours(edges, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);

        for( size_t i = 0; i < contours.size(); i++ )
        {
            Rect minRect = boundingRect(contours[i]);

            if(minRect.width > 150 & minRect.height > 150 )
            {
            rectangle(frame,minRect,Scalar(0,0,255));
            putText(frame,format("width = %d , height = %d",minRect.width,minRect.height), Point(minRect.x,minRect.y),
                    FONT_HERSHEY_PLAIN, 1, Scalar(0,255,0));
            }

        }
        imshow("edges", frame);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
edit flag offensive delete link more

Comments

For distance

Point a,  b;
cout<<"Distance "<<norm(a-b);

after may be you can use this

LBerger gravatar imageLBerger ( 2015-10-27 02:49:14 -0500 )edit

@LBerger i learned new thing. your comment is better i think. convert it as an answer.

sturkmen gravatar imagesturkmen ( 2015-10-27 03:08:35 -0500 )edit

and how to get the coordinates of my lines ?

MValeriy gravatar imageMValeriy ( 2015-10-27 08:23:37 -0500 )edit

coordinates of points?

MValeriy gravatar imageMValeriy ( 2015-10-27 08:50:39 -0500 )edit

for some reason i can't see the image. try findContours to get points.

sturkmen gravatar imagesturkmen ( 2015-10-27 08:57:54 -0500 )edit

the picture only just better

MValeriy gravatar imageMValeriy ( 2015-10-27 09:32:00 -0500 )edit
MValeriy gravatar imageMValeriy ( 2015-10-27 09:32:45 -0500 )edit

as I understand it , the operator "Canny" is looking for the edge . I can measure the distance between the edges ?

MValeriy gravatar imageMValeriy ( 2015-10-27 09:37:02 -0500 )edit

I am working with OpenCV231, can I use Operator findContours?

MValeriy gravatar imageMValeriy ( 2015-10-27 09:56:41 -0500 )edit

i think the steps that you applied to get the result you want are not the best. if you give the original image i can suggest some methods

sturkmen gravatar imagesturkmen ( 2015-10-27 10:51:31 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-27 02:24:19 -0500

Seen: 8,648 times

Last updated: Oct 28 '15