Neeraj
Neeraj

Reputation: 1680

Converting PNG to Bitmap with transparency using opencv

I have a transparent png image. I read that file using opencv. Then I am converting it to bitmap as follows.

Mat image = imread("fruit.png", -1);
BYTE * rawdata = reinterpret_cast<BYTE*>(image.data);
m_pBitmap->CopyFromMemory(NULL, rawdata, image.cols * 4);
m_pRenderTarget->DrawBitmap(m_pBitmap);

But after converting some images show some transparency problems... Same as in this link and the image is not showing full. But when we save that Mat image using imwrite there is no problem. So the problems I am facing are

  1. Transparency of some images are lost.
  2. The full image data is not showing. input image output image

Upvotes: 3

Views: 11583

Answers (1)

John Hany
John Hany

Reputation: 630

You should convert the image format within OpenCV, since different libraries have their own decoding and representing methods.

Try the following codes:

Mat image = imread("fruit.png", -1);
Mat image_bmp;
image.convertTo(image_bmp, CV_8UC3);
imwrite("fruit.bmp", image_bmp);

You can specify in which format you want to save your image by .bmp or .jpg in the filename.

The result looks like this (after resize of course):

enter image description here

Upvotes: 3

Related Questions