0

我正在尝试显示霍夫变换的输出图像但失败了。

这是原始代码的一部分:

int main(int argc, char** argv) {

    theta;      // parametro di angolo di inclinazione nel sistema di coordinate polari
    double rho;     // parametro di distanza (rho) nel sistema di coordinate polari

    Mat source, edges, output;

    
    if (argc < 3) {
        printf("USAGE: hough [fileName] [treshold]\n");
        return EXIT_FAILURE;
    }

    int lineTreshold = atoi(argv[2]);

    deque<pair<int, int>> edgePoints;  // <row, col>

    source = imread(argv[1], IMREAD_GRAYSCALE);
    namedWindow("source image", WINDOW_NORMAL | WINDOW_KEEPRATIO);

    output = source.clone();
    namedWindow("output image", WINDOW_NORMAL | WINDOW_KEEPRATIO);

    int maxDistance = hypot(source.rows, source.cols);

    cout << maxDistance << "\n";

    // matrice di voti
    vector<vector<int>> votes(2 * maxDistance, vector<int>(NUM_BINS, 0));

    cout << votes.size() << ", " << votes[0].size() << endl;

    // downsample: tengo solo i bordi dell'immagine
    detectEdge(source, edges);
    namedWindow("edge detection result", WINDOW_NORMAL | WINDOW_KEEPRATIO);

    // vote

    for (i = 0; i < edges.rows; ++i) {
        for (j = 0; j < edges.cols; ++j) {

            if (edges.at<uchar>(i, j) == 255) {  // se incontro edge point

                // guarda gli angoli [-90°, +90°]
                for (theta = 0; theta <= 180; theta += BIN_WIDTH) {

                    rho = round(j * cos(theta - 90) + i * sin(theta - 90)) + maxDistance;

                    //cout<< rho <<endl;

                    votes[rho][theta]++;
                }
            }
        }
    }

    //cout<< votes <<"\n";

    // find peaks
    for (i = 0; i < votes.size(); ++i) {
        for (j = 0; j < votes[i].size(); ++j) {

            if (votes[i][j] >= lineTreshold) {

                rho = i - maxDistance;
                theta = j - 90;

                cout << "found line with rho = " << rho << " and theta = " << theta << "\n";

                // converti linea da polare a cartesiana

                Point p1, p2;   // 2 punti che descrivono la linea
                polarToCartesian(rho, theta, p1, p2);

                //cout<< p1 << ", " << p2 <<"\n";

                line(output, p1, p2, Scalar(0, 0, 255), 2, LINE_AA);

            }
        }
    }

    Point dummy1(10, 10), dummy2(100, 100);

    line(output, dummy1, dummy2, Scalar(0, 0, 255), 1, LINE_AA);

    imshow("source image", source);
    imshow("output image", output);
    imshow("edge detection result", edges);

    waitKey();


    return 0;
}


It will always enter into the if (argc < 3) statement, so I commented the if statement part, and changed the lineThreshold and the source:


int main(int argc, char** argv) {

    ......

    /*
    if (argc < 3) {
        printf("USAGE: hough [fileName] [treshold]\n");
        return EXIT_FAILURE;
    }*/

    int lineTreshold = 1;

    deque<pair<int, int>> edgePoints;  // <row, col>

    source = imread("C:\\Users\\JyoHong\\Desktop\\OpenCV\\OpenCV-ImageProcessing\\OpenCVHoughTransform\\rec.jpeg", IMREAD_GRAYSCALE);
    namedWindow("source image", WINDOW_NORMAL | WINDOW_KEEPRATIO);

    ......

它应该在执行时显示源图像,输出图像,边缘检测结果窗口和图像,但输出图像窗口的结果不显示任何内容。

下面是程序执行时的输出:

在此处输入图像描述

我能做些什么来显示输出图像窗口吗?

这是完整的代码链接: https ://github.com/eToTheEcs/hough-transform/blob/master/hough.cpp

4

0 回答 0