cv ::与二进制cv :: mat的Erode错误

时间:2013-06-13 09:32:52

标签: c++ opencv

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

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您的二进制图像像素存储为unsigned char(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;
        }
    }

}

(通过评论回答)