图像中滑动窗口的方差

时间:2017-02-19 12:13:11

标签: c++ opencv variance

我从事交通标志检测工作,首先我对RGB图像进行了分割,以获得红色通道图像,如图1所示:

enter image description here

Secondely我试图通过计算图像上方滑动窗口的方差来找到均匀区域以消除不感兴趣的区域(不是交通标志)

我使用此代码,但我总是异常

int main(int argc, char** argv)
{
    IplImage *image1;

    if ((image1 = cvLoadImage("segmenter1/00051.jpg", 0)) == 0)
        return NULL;

    int rows = image1->width;
    int cols = image1->height;

    Mat image = Mat::zeros(cols, rows, CV_32FC1);
    double x = 0;
    double temp = 0;

    for (int i = 0; i < rows; i++){
        for (int j = 0; j < cols; j++){
            temp = cvGet2D(image1, j, i).val[0];

            x = temp / 255;

            image.at<float>(j, i) = x;
            x = image.at<float>(j, i);
        }
    }

    int k = 16;
    double seuil = 0.0013;
    CvScalar blanc;//pixel blanc
    blanc.val[0] = 255;
    cv::Scalar mean, stddev; //0:1st channel, 1:2nd channel and 2:3rd channel

    for (int j = 0; j < rows - k; j++)
    {
        for (int i = 0; i < cols - k; i++)
        {
            double som = 0;
            double var = 0;
            double t = 0;

            for (int jj = j; jj < k+j; jj++)
            {
                for (int ii = i; ii < k+i; ii++)
                {
                    t = image.at<float>(jj, ii);
                    som = som + t;
                    t = t*t;
                    var =var+ t;
                }
            }

            som = som / (k*k);
            if (som>0.18){
                var = (var / (k*k)) - (som*som);
                if (var < seuil)
                    cvSet2D(image1, j, i, blanc);
            }
        }
    }

    char stsave[80];
    cvSaveImage("variance/00051.jpg", image1);
    cv::waitKey(0);                                         
    return 0;
}

1 个答案:

答案 0 :(得分:0)

没有特定的例外情况,我只能猜测它是out_of_range。根据opencv文档,cvGet2DcvSet2D参数为image, y, x,有效转换为image, rows, cols。您已经翻转了rows, cols的定义,并且在两个循环之间存在冲突的用法。也许解决这些问题再试一次。