【问题标题】:How to print the elements inside the vector如何打印向量内的元素
【发布时间】:2017-02-17 12:13:30
【问题描述】:

我正在尝试计算下面骨架化图像中的端点数。我在这里使用向量。我需要的是打印向量,而不仅仅是计数。我尝试了很多方法。但是没有用。我是打开简历的新手,我从互联网上找到了以下代码。谁能帮我打印出这段代码实际得到的向量。代码如下。

// get the end points
    // Declare variable to count neighbourhood pixels
    int count, numberOFEndpoints;

    // To store a pixel intensity
    uchar pix;
    numberOFEndpoints = 0;
    // To store the ending co-ordinates
    std::vector<int> coords;

    // For each pixel in our image...
    for (int i = 1; i < CopyofSkeletionize.rows - 1; i++) {
        for (int j = 1; j < CopyofSkeletionize.cols - 1; j++) {

            // See what the pixel is at this location
            pix = CopyofSkeletionize.at<uchar>(i, j);

            // If not a skeleton point, skip
            if (pix == 0)
                continue;

            // Reset counter
            count = 0;

            // For each pixel in the neighbourhood
            // centered at this skeleton location...
            for (int y = -1; y <= 1; y++) {
                for (int x = -1; x <= 1; x++) {

                    // Get the pixel in the neighbourhood
                    pix = CopyofSkeletionize.at<uchar>(i + y, j + x);


                    // Count if non-zero
                    if (pix != 0)
                        count++;
                }
            }

            // If count is exactly 2, add co-ordinates to vector
            if (count == 2) {
                coords.push_back(i);
                coords.push_back(j);
                numberOFEndpoints = numberOFEndpoints + 1;
            }

        }
    }
     printf("numberOFEndpoints : %d \n", numberOFEndpoints);

我在互联网上使用了这段代码,但它不起作用。 for (int i = 0; i

https://i.stack.imgur.com/CMZtu.png

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    首先...您想以更好的方式存储坐标。也许像两个向量:

    std::vector<int> coordx;
    std::vector<int> coordy;
    

    然后你只需打印它:

    for (int i = 0; i < coordx.size(); ++i)
        std::cout << coordx[i] << " x " << coordy[i] << std::endl;
    

    或者使用pair(可能需要#include &lt;pair&gt;)或创建自定义point结构:

    std::vector< std::pair<int, int> > coords;
    

    添加到坐标向量将类似于:

    coords.push_back(std::make_pair(x, y));
    

    然后你只需打印它:

    for (int i = 0; i < coords.size(); ++i)
        std::cout << coords[i].first << " x " << coords[i].second << std::endl;
    

    其次...如果您坚持使用单个vector&lt;int&gt;,请尝试:

    int i = 0;
    while (i < coord.size())
        std::cout << coords[i++] << " x " << coords[i++] << std::endl;
    

    【讨论】:

    • 感谢您的解释。我尝试了您的第一个选项和最后一个选项。但是我仍然没有在命令提示符中看到向量结果。 :( 代码如下。
    • 零。因为在图像中没有字符“8”的端点
    • 好的,那就跟我一起想想吧……你有 0 个端点,但你想打印它们的坐标……对吧?现在控制台屏幕是空的......为什么会这样?
    • 天哪。现在我明白了。这并没有引起我的注意。谢谢你。我试过另一个。有效。 :)
    • 如何修改此代码以找到此图像中的关节。你能帮我解决这个问题吗?
    【解决方案2】:
    // get the end points
        // Declare variable to count neighbourhood pixels
        int count, numberOFEndpoints;
    
        // To store a pixel intensity
        uchar pix;
        numberOFEndpoints = 0;
        // To store the ending co-ordinates
        /*std::vector<int> coords;*/
        std::vector<int> coordx;
        std::vector<int> coordy;
    
        // For each pixel in our image...
        for (int i = 1; i < CopyofSkeletionize.rows - 1; i++) {
            for (int j = 1; j < CopyofSkeletionize.cols - 1; j++) {
    
                // See what the pixel is at this location
                pix = CopyofSkeletionize.at<uchar>(i, j);
    
                // If not a skeleton point, skip
                if (pix == 0)
                    continue;
    
                // Reset counter
                count = 0;
    
                // For each pixel in the neighbourhood
                // centered at this skeleton location...
                for (int y = -1; y <= 1; y++) {
                    for (int x = -1; x <= 1; x++) {
    
                        // Get the pixel in the neighbourhood
                        pix = CopyofSkeletionize.at<uchar>(i + y, j + x);
    
    
                        // Count if non-zero
                        if (pix != 0)
                            count++;
                    }
                }
    
                // If count is exactly 2, add co-ordinates to vector
                if (count == 2) {
                    coordx.push_back(i);
                    coordy.push_back(j);
    
    
                    /*numberOFEndpoints = numberOFEndpoints + 1;*/
                }
    
            }
    
    
        }
        for (int i = 0; i < coordx.size(); ++i) 
            std::cout << coordx[i] << " x " << coordy[i] << std::endl;} 
    

    我什至也没有从这段代码中得到矢量结果

    【讨论】:

    • 这将打印出 iff coordx.size() &gt; 0 的内容,这意味着有一些值要打印...
    猜你喜欢
    • 1970-01-01
    • 2017-11-19
    • 2010-09-20
    • 2018-06-26
    • 2021-12-30
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多