Ask Your Question
1

Windows imshow sometimes showing gray image

asked 2013-10-08 05:50:04 -0600

SergioBasurco gravatar 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);
Mat pano = imread(result_name , CV_LOAD_IMAGE_COLOR);
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 flag offensive close merge delete

Comments

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

Niraj gravatar imageNiraj ( 2014-02-13 05:58:02 -0600 )edit

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

Avio gravatar imageAvio ( 2016-10-04 04:11:36 -0600 )edit

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

Mat result8u;
result.convertTo(result8u,CV_8U);
Avio gravatar imageAvio ( 2016-10-04 04:15:32 -0600 )edit

4 answers

Sort by ยป oldest newest most voted
1

answered 2015-07-20 10:26:22 -0600

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

    img_warped.convertTo(img_warped_s, CV_16S);
    .
    .
    .
    blender->feed(img_warped_s, mask_warped, corners[img_idx]);
    .
    .
    .
    blender->blend(result, result_mask);

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

The solution is converting image to CV_8U

.
.
blender->blend(result, result_mask);
Mat result8u;
result.convertTo(result8u,CV_8U);
imshow("you can see the image now",result8u);
edit flag offensive delete link more

Comments

Thanks, this worked for me in the stitching_detailed example.

Avio gravatar imageAvio ( 2016-10-04 04:14:04 -0600 )edit
0

answered 2014-05-05 03:51:27 -0600

thePhi gravatar image

updated 2014-05-05 07:15:00 -0600

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

imgL = cv2.imread('tsukuba_l.png', 0)
imgR = cv2.imread('tsukuba_r.png', 0)

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

edit flag offensive delete link more
0

answered 2015-03-10 15:07:34 -0600

Manie gravatar image

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.

edit flag offensive delete link more
-1

answered 2014-03-26 06:38:57 -0600

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.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-10-08 05:50:04 -0600

Seen: 23,629 times

Last updated: Jul 20 '15