Ask Your Question
0

imshow just opens a tiny window

asked 2017-09-11 22:00:41 -0600

LorenWard gravatar image

import numpy as np import cv2

img = cv2.imread('C:\\Users\\mlarn\\Downloads\\20170818_171339.jpg',0)
#cv2.namedWindow('C:\\Users\\mlarn\\Downloads\\20170818_171339.jpg', cv2.WINDOW_NORMAL)
cv2.imshow('C:\\Users\\mlarn\\Downloads\\20170818_171339.jpg',0)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

Just opens a window like this link

If I uncomment the WINDOW_NORMAL flag and resize the window, it is just black. link

I've tried it on multiple files of differing types and get the same result.

What am I doing wrong?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-09-11 23:05:23 -0600

Tetragramm gravatar image

Well, the most important thing is that you are not actually giving it the image.

The imshow call has two parameters. The first is the name of the window, and the second is the image to show. So you're telling it to show the image 0. Try this:

cv2.imshow('Blah', img)
edit flag offensive delete link more

Comments

That's what I thought too, but

cv2.imshow('Title',C:\\Users\\mlarn\\Downloads\\20170818_171339.jpg)
                    ^
SyntaxError: invalid syntax
LorenWard gravatar imageLorenWard ( 2017-09-11 23:38:00 -0600 )edit

That is correct, that will not work. That is not the image, that is the path to the image. imread reads the image into memory. (The variable img in your code) imshow takes the image in memory and displays it.

Tetragramm gravatar imageTetragramm ( 2017-09-12 17:50:57 -0600 )edit

Oh, okay, I think I understand. Thank you!

So what does the "img" in cv2.imshow('Blah',img) mean?

This works now:

import numpy as np
import cv2



img = cv2.imread('C:\\Users\\mlarn\\Downloads\\20170818_171339.jpg',1)
#cv2.namedWindow('C:\\Users\\mlarn\\Downloads\\20170818_171339.jpg', cv2.WINDOW_NORMAL)
cv2.imshow('Title',img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

Also, the biggest problem I think I will have for the project I'm working on is the images I have are Geotiff files with an infrared layer. When I run the above code on one of the Geotif files, I just get a black image that matches the tilted footprint of the image itself. Same thing in Gimp.

LorenWard gravatar imageLorenWard ( 2017-09-12 18:01:12 -0600 )edit

Do you have any experience with python?

The line img = cv2.imread(...) takes the image path you give it, reads the image, and stores it in the variable named img. the function imshow(...) takes the name you give it for the window, and a variable containing an image, and displays the image.

The other thing is that the IR layer is probably a 16-bit image. The window divides the values by 256 before showing them, so if the image doesn't fill the full 16 bit range, it looks very dark. Try multiplying your image by a power of 2, like 32, or 64 before you display it.

Tetragramm gravatar imageTetragramm ( 2017-09-12 19:41:54 -0600 )edit
0

answered 2017-09-12 01:58:00 -0600

VxW gravatar image

try:

import cv2

img = cv2.imread('a.jpg', 0) #0=grayscale, 1=rgb
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-11 21:59:31 -0600

Seen: 7,509 times

Last updated: Sep 12 '17