初始化图像蒙版中的轮廓

时间:2014-03-17 21:25:38

标签: c++ opencv image-processing

背景:我有一张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; }
}}

1 个答案:

答案 0 :(得分:0)

&#34;我做错了什么?&#34;

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 < output.rows完全破坏了这个逻辑。条件的主体继续将r设置为0,使第一个循环无限。我想你的意思是:

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

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

相关问题