First time here? Check out the FAQ!

Ask Your Question
0

imwrite in opencv gives a black image

asked Jan 12 '16

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.

Preview: (hide)

Comments

no luck :(

avpai gravatar imageavpai (Jan 12 '16)edit

1 answer

Sort by » oldest newest most voted
0

answered Jan 12 '16

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.

Preview: (hide)

Comments

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

avpai gravatar imageavpai (Jan 12 '16)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 (Jan 12 '16)edit

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

avpai gravatar imageavpai (Jan 12 '16)edit

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

thdrksdfthmn gravatar imagethdrksdfthmn (Jan 12 '16)edit

Question Tools

1 follower

Stats

Asked: Jan 12 '16

Seen: 6,704 times

Last updated: Jan 12 '16