所以我试图侵 eclipse 二进制矩阵。 我使用以下代码创建矩阵:
cv::Mat tmp = cv::Mat::zeros( IMG->width, IMG->height, CV_8U );
for( auto i = 0 ; i < IMG->width ; i++)
{
for ( auto j = 0 ; j < IMG->height ; j++)
{
if( cv::pointPolygonTest(cv::Mat(contour),cv::Point(i,j),true) < 0 )
{
tmp.at<double>(i,j) = 255;
}
}
}
这是我正在使用的源图片:
这就是我通过循环得到的(它是 tmp 矩阵):
所以在我尝试使用此代码侵 eclipse 图片之后:
int erosion_elem = 1;
int erosion_size = 8;
int erosion_type;
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
/// Apply the erosion operation
erode( binary, erosion_dst, element );`
所以它编译得很好,但我在这一行遇到异常:
erode( binary, erosion_dst, element );`
它说这是一种不受支持的数据类型。 有谁知道为什么我会收到此异常?
我尝试更改矩阵 tmp 的数据类型,但出现相同的错误。
感谢您的帮助!
最佳答案
您的二进制图像像素存储为无符号字符
(CV_8U -> 8位 -> 1字节),
您也应该将像素值存储为unsigned char
cv::Mat tmp = cv::Mat::zeros( IMG->width, IMG->height, CV_8U );
for( auto i = 0 ; i < IMG->width ; i++)
{
for ( auto j = 0 ; j < IMG->height ; j++)
{
if( cv::pointPolygonTest(cv::Mat(contour),cv::Point(i,j),true) < 0 )
{
tmp.at<unsigned char>(i,j) = 255;
}
}
}
(根据评论做出回答)
关于c++ - 二进制 cv::mat 的 cv::Erode 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17083719/