# Calculate length between 2 points in c++

I need to calculate length of lines(number of pixels along the path) from a point until a terminal point. terminal point is a point which either ends or which has more than one line starting from that point. For example, in below image I need to calculate length between A->B, A->C, A->D.

I have attempted but then it goes into forever loop and I am not understanding why. I am new to work with images. so to do that I want to implement a logic in which I select neighbouring 8 pixels of a point and if its 1 then it should do a depth first till terminal and store that point in a hashmap. Please give me any suggestions. I am stuck on this. I can also share the original image if thats required. below image is only to explain problem

```
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <opencv2\highgui.hpp>
#include <opencv2\nonfree\features2d.hpp>
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2\legacy\legacy.hpp>
using namespace cv;
using namespace std;
#define ERR(msg) printf("%s : %d", (msg), __LINE__)
struct point_hash {
inline std::size_t operator()(const cv::Point & v) const {
return v.x * 61 + v.y;
}
};
unordered_multimap<Point,int, point_hash> lengths;
bool is_terminal(Mat &image,Point grid, unordered_set<Point, point_hash>& visited) {
cout << "This point is 1:Terminal:" << endl;
int count = 0;
for (int i = grid.x-1; i < grid.x+2; i++) {
for (int j = grid.y - 1; j < grid.y + 2; j++) {
if ((int) image.at<char>(j,i) > 0 && visited.find(Point(i,j)) == visited.end()) {
count++;
}
}
}
if (count > 1) {
return true;
}else
return false;
}
int check_length(Mat &image, Point kp,Point start, int length, unordered_set<Point, point_hash> visited) {
cout << "Get Neighbouring points" << kp.x << " " << kp.y << endl;
visited.insert({ kp });
if (start!=kp && is_terminal(image, kp, visited)) {
lengths.insert({ start,length });
return 0;
}
else {
if ((int)image.at<uchar>(kp.y - 1, kp.x - 1) > 0 && visited.find(Point(kp.x - 1, kp.y - 1)) == visited.end()) {
check_length(image, Point(kp.x - 1, kp.y - 1), start, length + 1, visited);
}
if ((int)image.at<uchar>(kp.y, kp.x - 1) > 0 && visited.find(Point(kp.x - 1, kp.y)) == visited.end()) {
check_length(image, Point(kp.x - 1, kp.y), start, length + 1, visited);
}
if ((int)image.at<uchar>(kp.y + 1, kp.x - 1) > 0 && visited.find(Point(kp.x - 1, kp.y + 1)) == visited.end()) {
check_length(image, Point(kp.x - 1, kp.y + 1), start, length + 1, visited);
}
if ((int)image.at<uchar>(kp.y - 1, kp.x) > 0 && visited.find(Point(kp.x, kp.y - 1)) == visited.end()) {
check_length(image, Point(kp.x, kp.y - 1), start, length + 1, visited);
}
if ((int)image.at<uchar>(kp.y + 1, kp.x) > 0 && visited.find(Point(kp.x, kp.y + 1)) == visited.end()) {
check_length(image, Point(kp.x, kp ...
```

you're reading img2, but use (undeclared) img1. - this can't be your

realcode.this is real code I was trying something so I took initial image in img2 and then was after checking if its binary I was passing it to check length function. I removed binary check step so this typo.

still, atm you're reading a

colourimage.please make it so, that folks can just c/p your code and try it !

I am not able to get it in binary form using that code so I removed it. I am getting this image from octave which is in terms of 1 and 0 but dint find any way to directly store an image in binary format. I know its histogram from 0-255 and when trying to threshold based on value(50). I end up losing important points. any suggestions on how I can do it?

imread(filename, 0); will read in a 8bit uchar img

this does not give me binary image because looking at max and min values it still gives min as 0 and max as 255.

this is correct, that's how binary images look like in opencv.

you have to be

verycareful, porting matlab/octave ideas to opencv, there are alotof subtle differences (indexing, interleaving, row/col majorness, etc).any suggestions about the logic or if there is some error in that implementation. because I have considered as if(pixel val > 0) condition.

maybe you need 2 passes:

sidenote: opencv uses white pixels for "on", and black ones for "off". maybe you have to invert your binary image.