gpt4 book ai didi

c++ - OpenCV clone() 和 copyTo() 方法不会生成与原始类型相同的 Mat?

转载 作者:行者123 更新时间:2023-11-28 00:14:35 26 4
gpt4 key购买 nike

我是 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 帖子中使用 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 channel 图像。
  • markers – 输入/输出标记的 32 位单 channel 图像( map )。它应该与图像大小相同。

所以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).

关于c++ - OpenCV clone() 和 copyTo() 方法不会生成与原始类型相同的 Mat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com