Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 19555

Calculate length between 2 points in c++

$
0
0
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 ![image description](/upfiles/14756541229443297.png) #include "stdafx.h" #include #include #include #include #include #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include #include #include "opencv2/calib3d/calib3d.hpp" #include "opencv2/imgproc/imgproc.hpp" #include 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 lengths; bool is_terminal(Mat &image,Point grid, unordered_set& 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(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 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(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(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(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(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(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(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(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(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); } } return 0; } int main() { Mat img1 = imread("image1.jpg",0); vector keypoints; fstream myfile("row1.txt");// These files give me my interest points like A fstream myfile1("col1.txt");// not necessarily B,C, D. int a,b; while (myfile >> a && myfile1 >> b) { keypoints.push_back(Point(b,a)); } unordered_set visited; for (int i = 0; i < keypoints.size(); i++) { int length = check_length(img1, keypoints[i],keypoints[i],0,visited); visited.clear(); } imshow("image 1", img1); waitKey(); return 0; }

Viewing all articles
Browse latest Browse all 19555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>