c++ - 二进制 cv::mat 的 cv::Erode 错误

标签 c++ opencv

所以我试图侵 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;
        }
    }

}   

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

http://snag.gy/ZZzhw.jpg

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

http://snag.gy/lhuR7.jpg

所以在我尝试使用此代码侵 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/

相关文章:

PHP 或 CGI - 将 .ico 转换为 gif、png 或 jpg?

c++ - 在 Android NDK 上使用 STL_tree.h 结果为 "No such file or directory"

c++ - 在 Visual Studio 6 中从 VB 调用 VS2010 C++ dll

android - Android 中的眼动追踪

animation - 如何为视频中的图像制作动画

c++ - 调用 "SetDllDirectory"时表达式: string iterators incompatiable,

c++ - OpenGL - "ultra smooth"简单水平移动对象的动画

c++ - 关于如何将线条图像转换为圆形图像的问题

opencv - 行,列,_ = frame.shape AttributeError: 'tuple'对象没有属性 'shape'

opencv - 用R和t计算新的PixelCoordinates点