Ask Your Question
2

Imread returns a blank image for a certain file

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

Letar gravatar image

updated 2018-12-29 15:29:43 -0600

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);
waitKey(0);

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

Comments

2

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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
5

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

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

good

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

edit flag offensive delete link more

Comments

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 -0600 )edit

as long as it is consistently so -- ok.no problem ;)

berak gravatar imageberak ( 2018-11-20 09:10:56 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 2,586 times

Last updated: Nov 20 '18