我用 C API 写了一个分水岭分割的代码。现在我将所有这些转换为 C++。所以,cvsaveimage 变成了 imwrite。但是当我使用 imwrite 时,我得到的只是一个黑色图像。
这是代码:-
Mat img8bit;
Mat img0;
img0 = imread("source.png", 1);
Mat wshed(img0.size(), CV_32S);
wshed.setTo(cv::Scalar::all(0));
////after performing watershed segmentation and
// displaying the watershed image from wshed//
wshed.convertTo(img8bit, CV_32FC3, 255.0);
imwrite("Watershed.png", img8bit);
我要保存的原始图像在 wshed 中。我从网上看到建议我们需要将其转换为 16 位或更高位,以便 imwrite 正确保存它。如你所见,我试过了。但是使用 imshow 时 wshed 图像可以正确显示。img0 是灰色图像/黑白,而 wshed 图像是彩色的。有什么帮助吗?
编辑-我将第 4 行更改为
Mat wshed(img0.size(), CV_32FC3);