1

我在 openCV 代码中做一个简单的 kmean 聚类。它运行成功,但是当我使用 valgrind 检查它有内​​存问题时:

==6982== 条件跳转或移动取决于未初始化的值
==6982== 未初始化的值是由堆分配创建的

这是简单的代码,其中输入图像'obs'被堆叠到大矩阵'samples'中,然后我调用kmean内置函数

/*GETINITKMEANS: calculate initial kmeans segmentation (lev,img) 
obs:    input image
    levels: array holding intensity levels
    hh:     height
    ww:     width of image
    nlevels:number of levels

    lev:    holds segmentation labels
    img:    holds segmentation reconstruction
 */

void getinitkmeans(Mat &obs,Mat &lev,Mat &img,vector<UINT8>& levels,int hh,int ww,int nlevels)
{
    int dim=hh*ww;

    // data for kmeans
    Mat samples(dim,3,CV_32F,Scalar(0));
    Mat labels(dim,1,CV_8U,Scalar(0));
    Mat centers(nlevels,3,CV_32F,Scalar(0));

    // extract bgr pixel values into samples matrix
    for (int i=0;i<hh;i++)
    {
        for (int j=0;j<ww;j++)
        {
            samples.at<float>(i*ww+j,0) = (float)obs.at<Vec3b>(i,j)[0];
            samples.at<float>(i*ww+j,1) = (float)obs.at<Vec3b>(i,j)[1];
            samples.at<float>(i*ww+j,2) = (float)obs.at<Vec3b>(i,j)[2];
        }
    }

    // run kmeans algorithm
    kmeans(samples,nlevels,labels,cv::TermCriteria(CV_TERMCRIT_ITER,30,0),1,KMEANS_RANDOM_CENTERS,centers);

    // construct lev, img
    for (int i=0;i<hh;i++)
    {
        for (int j=0;j<ww;j++)
        {
            lev.at<UINT8>(i,j) = labels.at<int>(i*ww+j,0);
            img.at<UINT8>(i,j) = levels[labels.at<int>(i*ww+j,0)];
        }
    }
}
4

0 回答 0