【问题标题】:Initializing a contour in the mask of an image初始化图像掩码中的轮廓
【发布时间】:2016-09-28 17:50:01
【问题描述】:

背景:我有一张图片是width-256 x height-1024,我正在尝试为我的图片蒙版初始化一个“轮廓”。蒙版与我的图像尺寸相同,所有像素值都设置为0。实际图像是灰度图像。

我正在尝试根据图像每一列中第一个“足够亮”的像素值来初始化掩码中的轮廓。也就是说,对于每一列,我遍历所有行,并找到第一个超过阈值的像素(比如图像中所有像素值的平均值)。一旦找到第一个“足够亮”的像素,我就会移动到下一列,然后重复这个过程。

代码:这是我到目前为止所做的,逻辑对我来说似乎是正确的。但是,每当有一列中的所有行都没有超过阈值的值时,我都会遇到一个无限循环。一旦达到图像中的总列数,我就会破坏我的代码。 我做错了什么?

// prior mem_allocation of host_iData and mask
// host_iData contains image data 

int iterate = 0;
int c =0;
int r = 0;
while( c<output.cols ) {
    while( r<output.rows ) {
        int val = host_iData[r*output.cols + c];

        if( val > sum) {
            mask[r*output.cols + c] = 255;
            c++; 
            r = 0;
            itr++; 
        }
        else { r++; }

        if( itr == output.cols) { break; }
}}

【问题讨论】:

    标签: c++ opencv image-processing


    【解决方案1】:

    “我做错了什么?”

    while ( c < output.cols ) {
        while ( r < output.rows ) {
    
            if ( value of pixel is higher than some threshold) {
                start processing the following column starting the first row
            }
            else { 
                process next row
            }
        }
    }
    

    条件r &lt; output.rows 完全破坏了这个逻辑。条件的主体继续将r 设置为0,使第一个循环无限。我想你的意思是这样的:

            if ( value of pixel is higher than some threshold) {
                start processing the following column starting the first row
                break; // <-- THIS
            }
    

    这将完全停止内循环,导致外循环继续。

    【讨论】:

    • 如果我不设置r=0,那么我会移动到下一列的同一行。我没有得到你写的代码的第二部分?
    猜你喜欢
    • 2021-03-08
    • 1970-01-01
    • 2021-01-05
    • 2015-11-30
    • 1970-01-01
    • 2012-04-05
    • 2013-06-11
    • 1970-01-01
    • 2017-10-07
    相关资源
    最近更新 更多