在我进行一些图像处理并应用蒙版之后,我得到了我想要的东西。我可以在“crop”的imshow结果中清楚地看到图像中间有灰色像素。 我正在尝试获取最大像素值位置。我检查了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;
返回最大值.. 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;
}
}
}
输出结果为:
X........604 Y......479 = ¿
X........607 Y......479 =
X........610 Y......479 = ¿
请帮帮我
PD:我知道有类似的问题。但这是具体问题。答案 0 :(得分:1)
我不确定我是怎么解决的。很多时间过去了。但是目前我拥有的代码是有效的:
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;
}
}
}
}
答案 1 :(得分:0)
你确定规范化Mat会自动将它从uchar转换为double吗?很可能数据仍然存储为uchars而你正在从中读取错误的数字。
尝试dist.convertTo(dist,CV_64F); 打印数字到处都是双打
或只与uchars合作。
答案 2 :(得分:0)
试试这段代码:
cout << "X........"
<< x
<< " Y......"
<< y
<< " = "
<< (double) crop.at< unsigned char>(x,y) <<endl;