OpenCV clone() 和 copyTo() 方法不会生成与原始类型相同的 Mat?
OpenCV clone() and copyTo() methods don't produce Mat of same type as original?
我是 OpenCV 的新手,正在阅读一本书。我正在尝试使用以下内容提取指示组件区域的二进制图像:
cv::Mat result;
result = image.clone();
cv::watershed(image, result);
执行时,会产生以下错误:
segmentation.cpp:159: error: (-215) src.type() == CV_8UC3 && dst.type() == CV_32SC1
in function watershed
这个错误当然是正确的,正如我在这个 SO post 中用 type2str
函数验证的那样: How to find out what type of a Mat object is with Mat::type() in OpenCV
我也尝试使用 image.copyTo(result)
而不是 clone()
,但这会产生相同的错误消息。 我做错了什么,我怎样才能复制一个Mat
来得到相同的类型?
我猜这个 hacky 解决方案是转换结果的颜色以匹配图像的颜色,就像这里所做的那样:OpenCV: How to convert CV_8UC1 mat to CV_8UC3 但这似乎是错误的不是吗?
正如此处提到的:Difference Clone CopyTo,在这种情况下,clone()
和 copyTo()
之间没有区别。
其实clone()
的源码如下:
inline Mat Mat::clone() const
{
Mat m;
copyTo(m);
return m;
}
copyTo
然而可以与掩码结合使用,并且通常将数据复制到另一个矩阵,因此可以用于将子图像绘制到另一个图像中。
关于 watershed
的代码,文档指出,
- image – 输入 8 位 3 通道图像。
- markers – Input/output 标记的 32 位单通道图像(地图)。它应该与图像大小相同。
所以image
(你的image
)和markers
(你的result
)不应该相同。
Before passing the image to the function, you have to roughly outline the desired regions in the image markers with positive (>0) indices. So, every region is represented as one or more connected components with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from a binary mask using findContours() and drawContours() (see the watershed.cpp demo). The markers are “seeds” of the future image regions. All the other pixels in markers , whose relation to the outlined regions is not known and should be defined by the algorithm, should be set to 0’s. In the function output, each pixel in markers is set to a value of the “seed” components or to -1 at boundaries between the regions.
Visual demonstration and usage example of the function can be found in the OpenCV samples directory (see the watershed.cpp demo).
我是 OpenCV 的新手,正在阅读一本书。我正在尝试使用以下内容提取指示组件区域的二进制图像:
cv::Mat result;
result = image.clone();
cv::watershed(image, result);
执行时,会产生以下错误:
segmentation.cpp:159: error: (-215) src.type() == CV_8UC3 && dst.type() == CV_32SC1
in function watershed
这个错误当然是正确的,正如我在这个 SO post 中用 type2str
函数验证的那样: How to find out what type of a Mat object is with Mat::type() in OpenCV
我也尝试使用 image.copyTo(result)
而不是 clone()
,但这会产生相同的错误消息。 我做错了什么,我怎样才能复制一个Mat
来得到相同的类型?
我猜这个 hacky 解决方案是转换结果的颜色以匹配图像的颜色,就像这里所做的那样:OpenCV: How to convert CV_8UC1 mat to CV_8UC3 但这似乎是错误的不是吗?
正如此处提到的:Difference Clone CopyTo,在这种情况下,clone()
和 copyTo()
之间没有区别。
其实clone()
的源码如下:
inline Mat Mat::clone() const
{
Mat m;
copyTo(m);
return m;
}
copyTo
然而可以与掩码结合使用,并且通常将数据复制到另一个矩阵,因此可以用于将子图像绘制到另一个图像中。
关于 watershed
的代码,文档指出,
- image – 输入 8 位 3 通道图像。
- markers – Input/output 标记的 32 位单通道图像(地图)。它应该与图像大小相同。
所以image
(你的image
)和markers
(你的result
)不应该相同。
Before passing the image to the function, you have to roughly outline the desired regions in the image markers with positive (>0) indices. So, every region is represented as one or more connected components with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from a binary mask using findContours() and drawContours() (see the watershed.cpp demo). The markers are “seeds” of the future image regions. All the other pixels in markers , whose relation to the outlined regions is not known and should be defined by the algorithm, should be set to 0’s. In the function output, each pixel in markers is set to a value of the “seed” components or to -1 at boundaries between the regions.
Visual demonstration and usage example of the function can be found in the OpenCV samples directory (see the watershed.cpp demo).