# Windows imshow sometimes showing gray image

I have done a bit of research on this topic, apparently Windows OpenCV builds might have an error using imshow. This problem varies a bit but the result is a gray image instead of the correct RGB image stored in a Mat structure.

Here it's suggested that it could be due to the jpeg codecs. It's also mentioned as a bug in the developer zone, that it's related to namedWindows.

I'm using the stitching_detailed example. The relevant part is the final panorama, which is stored in the result Mat. When saving it to a file it works correctly.

imwrite(result_name, result); // works


If I show the image either like this:

imshow("result", result);
waitKey(0);


or like this:

namedWindow("result", CV_WINDOW_NORMAL);
imshow("result", result);
waitKey(0);


I get a gray image displayed.

But if I do the following:

imwrite(result_name, result);
namedWindow("result", CV_WINDOW_NORMAL); //works without this line too
imshow("result", pano);
waitKey(0);


I get the correct image displayed, i.e. Saving it to a file and then reading it works. But not the live rendering of the generated image.

What can be the cause?

edit retag close merge delete

Hi... I have the same problem, Do you got anything on this?

( 2014-02-13 05:58:02 -0500 )edit

I'm also having the same problem. Changing from jpg to png didn't solve.

( 2016-10-04 04:11:36 -0500 )edit

Converting the image to CV_8U solved my problem in the stitching_detailed example.

Mat result8u;
result.convertTo(result8u,CV_8U);

( 2016-10-04 04:15:32 -0500 )edit

Sort by » oldest newest most voted

lets look some points of the code ( stitching_detailed.cpp )

    img_warped.convertTo(img_warped_s, CV_16S);
.
.
.
.
.
.


the reason for this problem is type of the image is CV_16S .

The solution is converting image to CV_8U

.
.
Mat result8u;
result.convertTo(result8u,CV_8U);
imshow("you can see the image now",result8u);

more

Thanks, this worked for me in the stitching_detailed example.

( 2016-10-04 04:14:04 -0500 )edit

I've got the same problem with cv2.imshow(). I found a solution by modifying the data type, and imshow() can now read the file :

import numpy as np
import cv2

stereo = cv2.createStereoBM(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgL, imgR, cv2.CV_32F)

cv2.namedWindow("disparity_tsukuba", 0)
res = cv2.convertScaleAbs(disparity)
cv2.imshow('disparity_tsukuba', res)

cv2.waitKey(0)


The problem is indeed that imshow() works badly with files other than CV_8U file and can result in wrong image (like all gray images) when given 32F file, which are produced by some functions like StereoBM or other : doc. So we need to convert from a CV_32F to a readable format for imshow()

more

After having spent a whole day searching, does anybody have a solution for this?

There is no one solution that seems te be working for me. And solutiong is a far reach for the meaning.

more

I suddenly got the same problem, after manipulating my code. I finally figured that it was because I had removed tha code below, which was copied from an example.

Code necessary to make imshow work for me: if cv2.waitKey(1) & 0xFF == ord('q'): break The ncode itselfe has no effect, I hace to use <ctrl> C to stop the program. But without it, my images turn grey.

more

Official site

GitHub

Wiki

Documentation