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

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

edit retag reopen merge delete

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

Sort by ยป oldest newest most voted

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:

#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;
}

more

For distance

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


after may be you can use this

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

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

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

and how to get the coordinates of my lines ?

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

coordinates of points?

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

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

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

the picture only just better

( 2015-10-27 09:32:00 -0500 )edit
( 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 ?

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

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

( 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

( 2015-10-27 10:51:31 -0500 )edit

Official site

GitHub

Wiki

Documentation