简体   繁体   English

在opencv中写入可产生黑白图像

[英]imwrite in opencv gives a black/white image

I wrote a code for watershed segmentation in C API. 我用C API编写了分水岭分割的代码。 Now I am converting all those into C++. 现在,我将所有这些都转换为C ++。 so, cvsaveimage becomes imwrite. 因此,cvsaveimage会被写入。 But when I use imwrite ,all i get is a black image. 但是当我使用imwrite时,我得到的只是一张黑色图像。

this is the code:- 这是代码:-

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);

The original image that I want to save is in wshed. 我要保存的原始图像处于乱码状态。 I saw suggestions from the net that we need to convert it to 16 bit or higher so that the imwrite saves it right. 我从网上看到了一些建议,我们需要将其转换为16位或更高版本,以使写入内容可以正确保存。 Like you see,I tried that. 如你所见,我尝试过。 But the wshed image is being displayed correctly when using imshow.The img0 is grey image/black and white while the wshed image is coloured. 但是,使用imshow时,波纹图像可以正确显示。img0为灰色图像/黑白,而波纹图像为彩色。 any help on this? 有什么帮助吗?

Edit- I changed the 4th line to 编辑-我将第四行更改为

Mat wshed(img0.size(), CV_32FC3);

When calling Mat::convertTo() with a scalar ( 255 in your case), the values of every matrix item will be multiplied by this scalar value. 当使用标量(在您的情况下为255 Mat::convertTo()调用Mat::convertTo()时,每个矩阵项的值都将乘以该标量值。 This will cause all most every result pixel values exceed 255 (ie white pixels) except those of 0s where they remain 0 (ie black pixels). 这将导致几乎所有结果像素值都超过255(即白色像素),但0的结果仍保持0(即黑色像素)。 This is why you will get the black-white pixel in the end. 这就是为什么最终会得到黑白像素的原因。

To make it work, simply change it to: 要使其工作,只需将其更改为:

wshed.convertTo(img8bit, CV_32FC3);

You said: 你说:

The original image that I want to save is in wshed. 我要保存的原始图像处于乱码状态。 I saw suggestions from the net that we need to convert it to 16 bit or higher so that the imwrite saves it right. 我从网上看到了一些建议,我们需要将其转换为16位或更高版本,以使写入内容可以正确保存。

If saving the image does not work you should keep in mind that the image data has to be either 8-Bits or 16-Bit unsigned when using the imwrite Function, not 16-Bits or higher. 如果保存图像不起作用,则应记住使用imwrite函数时,图像数据必须是8位或16位无符号的 ,而不是16位或更高。

This is stated in the documentation : 这在文档中说明

The function imwrite saves the image to the specified file. imwrite函数将图像保存到指定文件。 The image format is chosen based on the filename extension (see imread() for the list of extensions). 根据文件扩展名选择图像格式(有关扩展名列表,请参见imread())。 Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function . 使用此功能只能保存8位(在PNG,JPEG 2000和TIFF情况下为16位无符号(CV_16U))单通道或3通道(具有“ BGR”通道顺序)图像 If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving. 如果格式,深度或通道顺序不同,请在保存之前使用Mat :: convertTo()和cvtColor()进行转换。 Or, use the universal FileStorage I/O functions to save the image to XML or YAML format. 或者,使用通用的FileStorage I / O功能将图像保存为XML或YAML格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM