Python Forum
cv2.waitkey(0) not working coreectly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cv2.waitkey(0) not working coreectly
#1
This is what I'm trying (it's the beginning of a cv2 powered OMR multiple choice marking program) :

Along the way, I want to check on the image, see what it looks like.

# load the image, convert it to grayscale, blur it
# slightly, then find edges
mypath = '/home/pedro/PDF_IN/test1.png'
# image = cv2.imread(args["image"])
image = cv2.imread(mypath)
# check that the image loads ok
cv2.imshow("Original image", image)
cv2.waitKey(0)
cv2.destroyAllWindows() 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)
Without cv2.waitkey() the Python kernel will crash. (Tried a few times!)

According to what I read, cv2.waitkey(0) should wait for a key to be pressed, then, I thought, dismiss the image window.

But what I am getting is this:

I enter this in Idle:

Quote:cv2.imshow("Original image", image)
I get a little window with no content, a bit greyish.

I enter this in Idle:

cv2.waitKey(3000)

My image pops up in a window. After 3 seconds, Idle shows:

Quote:>>> cv2.waitKey(3000)
-1

The window with my image is still there. This kills it.

cv2.destroyAllWindows()

Shouldn't the image window disappear after 3 seconds?? If not, what is the point??

If I set:

Quote:cv2.waitKey(0)

I can not get out of waitkey(0)

Doesn't matter what key I press, I remain waiting. Ctrl-c cannot break the loop in Idle, only shell restart gives me the shell back.

Quote:>>> cv2.waitKey(0)
w
vvv
mm

Obviously, I am doing something wrong. Any tips please??
Reply
#2
I use
if cv2.waitKey(1) & 0xFF == ord('q'):
break

video_capture.release()
cv2.destroyAllWindows()
Pedroski55 likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020