我做了一些图像处理,并应用蒙版,我得到了我想要的。我可以清楚地看到,在"裁剪"的不显示结果中,图像中间有灰色像素。我试图获得最大像素值的位置。我检查了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;
返回max..4.25593 e-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:我知道也有类似的问题。但这是一个特殊的问题我不知道我是怎么解决的。很多时间过去了。但是我现在的代码是这样的
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;
}
}
}
}
您确定规范化Mat会自动将其从uchar转换为double吗?很有可能数据仍然以字符的形式存储,而你从中读取了错误的数字。
尝试dist. convertto (dist, CV_64F);将数字以双精度输出
试试下面的代码:
cout << "X........"
<< x
<< " Y......"
<< y
<< " = "
<< (double) crop.at< unsigned char>(x,y) <<endl;