2

所以我试图侵蚀二进制矩阵。我使用以下代码创建矩阵:

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;
        }
    }

}   

这是我正在使用的源图片:

http://snag.gy/ZZzhw.jpg

这就是我的循环得到的(它是 tmp 矩阵):

http://snag.gy/lhuR7.jpg

因此,在我尝试使用此代码侵蚀图片之后:

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 的数据类型,但我有同样的错误。

谢谢你的帮助 !

4

1 回答 1

2

您的二进制图像像素存储为unsigned char(CV_8U -> on 8bits -> 1 byte),您也应该存储像素的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;
        }
    }

}

(从评论中回答)

于 2013-06-13T11:01:21.917 回答