Ask Your Question

# 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

## 1 answer

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

## Comments

For distance

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


after may be you can use this

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

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

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

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

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 10,584 times

Last updated: Oct 28 '15