Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

image description

#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.y + 1), 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, 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);
        }
    }
    return 0;
}

int main() {
    Mat img2 = imread("image1.jpg");
    vector<Point> keypoints;
    fstream myfile("row1.txt");// These files give me my interest points like A
    fstream myfile1("col1.txt");// not necessarily B,C, D.
    unordered_set<Point, point_hash> 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;
}

image description

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

image description

#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.y + 1), 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, 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);
        }
    }
    return 0;
}

int main() {
    Mat img2 img1 = imread("image1.jpg");
    vector<Point> keypoints;
    fstream myfile("row1.txt");// These files give me my interest points like A
    fstream myfile1("col1.txt");// not necessarily B,C, D.
    unordered_set<Point, point_hash> 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;
}

image description

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

image description

#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.y + 1), 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, 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);
        }
    }
    return 0;
}

int main() {
    Mat img1 = imread("image1.jpg");
imread("image1.jpg",0);
    vector<Point> 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<Point, point_hash> 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;
}

image description

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

image description

#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.y + 1), 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, 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);
        }
    }
    return 0;
}

int main() {
    Mat img1 = imread("image1.jpg",0);
    vector<Point> 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<Point, point_hash> 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;
}

image description

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

image description

#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.y + 1), 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, 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);
        }
    }
    return 0;
}

int main() {
    Mat img1 = imread("image1.jpg",0);
    vector<Point> 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<Point, point_hash> 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;
}