简体   繁体   English

在opencv中获取灰度图像中的最大像素值

[英]Getting maximum pixel value in grayscale image in opencv

After I do some image manipulation, and apply mask, I get what I want. 在进行一些图像处理并应用遮罩后,我得到了想要的东西。 I can clearly see on imshow result of "crop" that there's gray pixels in the middle of image. 我可以在“裁剪”的imshow结果上清楚地看到图像中间有灰色像素。 I'm trying to get the maximum pixel value location. 我正在尝试获取最大像素值位置。 I've checked the crop.channels(), which returns 1. 我检查了crop.channels(),它返回1。

    Mat mask = drawing2;
    drawContours(mask, contours, -1, Scalar(255), CV_FILLED);
    Mat dist;
    distanceTransform( cannyInv, dist, CV_DIST_L2, 3 );
    normalize(dist,dist,0.0,1.0,NORM_MINMAX);
    Mat crop;
    dist.copyTo(crop, mask);
    cout << "max.. "<< *std::max_element(crop.begin<double>(),crop.end<double>()) <<endl;

which returns max.. 4.25593e-08 最多返回4.25593e-08

    for(int y = 0; y < crop.rows; y++)
    {
        for(int x = 0; x < crop.cols; x++)
        {
            if (crop.at<unsigned char>(x,y) > 0){      
                cout << "X........"<<x<<" Y......"<<y<< " = "<<crop.at<unsigned char>(x,y) <<endl;
            }
        }
    }

The output is: 输出为:

X........604 Y......479 = ¿
X........607 Y......479 =   
X........610 Y......479 = ¿

Help me please 请帮帮我

PD: I know that there's similar question. PD:我知道也有类似的问题。 But this is specific problem. 但这是特定的问题。

I'm not sure how I solved it. 我不确定如何解决。 A lot of time has passed. 很多时间过去了。 But the code that currently I have and it works is this: 但是我目前拥有的并且可以正常工作的代码是这样的:

    Mat dist=Mat::zeros(480,640, CV_8UC1);;
    distanceTransform( cannyInv, dist, CV_DIST_L2, 3 );
    Mat distNorm;
    dist.convertTo(distNorm, CV_8UC1,1,0);
    Mat result= Mat::zeros(480,640, CV_8UC1);
    distNorm.copyTo(result, mask);
    Mat tmp=Mat::zeros(480,640, CV_8UC1);
    Mat fik=Mat::zeros(480,640, CV_8UC3);
    for(int i = 0; i < result.rows; i++)
    {
        for(int j = 0; j < result.cols; j++)
        {
            if ( result.at< uchar >( i,j ) > 0){
                uchar val = result.at< uchar >( i,j  );
                if(val>maxVal){
                    if(val>0){
                        cv::circle(tmp,cvPoint(j,i),2,255,-1);
                    }
                    maxVal=val;
                    maxX = j;
                    maxY = i;
                }
            }
        }
    }

Are you sure that normalizing the Mat automatically converts it from uchar to double? 您确定归一化Mat会自动将其从uchar转换为double吗? It's very likely the data is still stored as uchars and you're reading wrong numbers from it. 数据很可能仍以uchar形式存储,并且您正在从中读取错误的数字。

Try dist.convertTo(dist, CV_64F); 尝试dist.convertTo(dist,CV_64F); Print the numbers as doubles everywhere 将数字打印成双倍

OR work only with uchars. 或仅与uchars一起使用。

Try this code: 试试这个代码:

cout << "X........"
     << x
     << " Y......"
     << y
     << " = "
     << (double) crop.at< unsigned char>(x,y) <<endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM