Ask Your Question

Imread returns a blank image for a certain file

asked 2018-11-20 06:57:52 -0500

Letar gravatar image

version: 4.0.0-alpha

I try to display an image with this code:

cv::Mat image1 = cv::imread(path);
std::ifstream ifile(path);
if (!ifile.is_open()) {
    std::cerr << "There was a problem opening the input file" << std::endl;
Mat prev = image1;
namedWindow("Display", WINDOW_AUTOSIZE);
imshow("Display", image1);

Where path is absolute path to a file. When I try to run with this file: C:\fakepath\5a2e6ac832420.png, i got only a black window without any image.

At the same time it works well with images from another dataset. Obviously the problem lays in file. But I just can't figure it out. It's a common png, what's wrong? I tried to use different flags in cv::imread, but it was useless.

Any ideas?

edit retag flag offensive close merge delete



You can see letter because there is transparency plane (4 planes) in your png file. opencv does not manage transparency and shows only plane 0 to 2 You must split plane and process yourself plane[3]

LBerger gravatar imageLBerger ( 2018-11-20 07:03:13 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-20 07:12:46 -0500

berak gravatar image

it's one of those png's where a silly webdev put ALL of the information into the alpha channel ONLY. the bgr channels are all black, indeed.

what you have to do is:

  • read the image with alpha channel
  • extract alpha (and throw anything else away...)

Mat m = imread("badalpha.png", IMREAD_UNCHANGED);
Mat bw;
cout << m.type() << " " << m.channels() << endl;
extractChannel(m, bw, 3);
imshow("good", bw);


but, just saying, if you have images like that in your dataset, you have a problem ...

edit flag offensive delete link more


Many thanks! Okey, i really have a dataset of such images, but it seems all usefull data is contained in alpha channel only)

Letar gravatar imageLetar ( 2018-11-20 09:09:04 -0500 )edit

as long as it is consistently so -- problem ;)

berak gravatar imageberak ( 2018-11-20 09:10:56 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-11-20 06:57:52 -0500

Seen: 30 times

Last updated: Nov 20