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