Ask Your Question
0

imwrite in opencv gives a black image

asked 2016-01-12 05:23:19 -0500

avpai gravatar image

I wrote a code for watershed segmentation in C API. Now I am converting all those into C++. so, cvsaveimage becomes imwrite. But when I use imwrite ,all i get is a black image.

this is the code:-

    Mat img8bit;
    Mat img0;
    img0 = imread("source.png", 1);           
    Mat wshed(img0.size(), CV_32F);
    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. 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. any help on this? I used wshed.type() to get what kind of image the output is,it was 16bit.

edit retag flag offensive close merge delete

Comments

no luck :(

avpai gravatar imageavpai ( 2016-01-12 06:00:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-12 05:52:35 -0500

img8bit and CV_32FC3 ? 8 bit image is CV_8UC1 or CV_8UC3 (afair imwrite writes 8 bit 3 channel or 8 bit 1 channel images only). You have CV_32F (one channel) so try CV_8UC1:

wshed.convertTo(img8bit, CV_8UC1, 255.0);

Sure you will lose some information, but if you want to have just visualization it is ok. If you want store and then read all information use FileStorage class.

edit flag offensive delete link more

Comments

I want to save the image. is there no other way other than filestorage?

avpai gravatar imageavpai ( 2016-01-12 05:57:29 -0500 )edit

You can save 32 bits as CV_8UC4 image, but it is a little tricky. Use FileStorage , it is simple and can handle even large images.

If you want "just segmented image" then 4 colors are enough for any segmentation :)

Vit gravatar imageVit ( 2016-01-12 06:18:23 -0500 )edit

tried it..no luck..guess I have to look into filestorage..

avpai gravatar imageavpai ( 2016-01-12 06:32:39 -0500 )edit

I would do a CV_32F to CV_8UC3 conversion and there is no lost information

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-01-12 07:14:31 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-01-12 05:23:19 -0500

Seen: 3,254 times

Last updated: Jan 12 '16