OpenCV -> Python - image window does not close - python

I need the user input after showing an image, but the problem is that the image window will not close and will freeze. Do you have some solution or alternative that I can use to get the user input?
Code:
import cv2
img = cv2.imread("begin.jpg")
cv2.imshow("test",img)
cv2.destroyAllWindows()
raw_input("test:")
Problem: Window that show image will not close
Cause: raw_input(). Removing the raw_input() the window closes as expected.
I expect more than one character from the user.
Thank you so much

I had the same problem: I found the next info in openCV DevZone, it looks like a bug: http://code.opencv.org/issues/2911 it works for me.

Related

How to use the ImageGrab.grab().load() function or any other function to get pixel updates?

I have been trying to check if a pixel on the screen is changing. What do I need to do?
I have surfed the internet for a long time with no success. I have experimented with the code given on the net, and found out that my code is only giving data from the screen that was open when the code was run. ie, if the screen was white when the code was run, it will read pixels from the white screen, even though the screen color already changed.
from PIL import ImageGrab
px=ImageGrab.grab().load()
m=px[613,296]
print(m)
while 1:
if m!=px[613,296]:
m=px[613,296]
print(m)
I ran the code and started a video, I expected the values to keep changing but all I got was (255,255,255) (the white screen of the idle) I also tried to change the screen manually.
I tried runing the code without console and print the output without the while loop in a text file, I got correct values.But the task I need to complete needs to run the code several times to check if pixel updates. How should I accomplish this?
This works for me:
#!/usr/bin/env python3
from PIL import ImageGrab
while True:
px=ImageGrab.grab().load()
m=px[613,296]
print(m)
I think it will be faster if you just grab one pixel though by specifying a bounding box like this so you only grab one pixel:
#!/usr/bin/env python3
from PIL import ImageGrab
while True:
screen=ImageGrab.grab(bbox=(613,296,614,297))
px = screen.load()
m=px[0,0]
print(m,screen.size)

Python OpenCV - remove title bar, toolbar, and status bar

I've found documentation regarding C ++, but not much with python.
The basic code to display in python is:
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('messi.jpg',0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
To show the image below. But how do I turn this
To look like this?
I also want to keep the size. So I've read some people saying to go "full screen". The only way I could think that might work is do "full screen, but then resize it? Not sure if that's a solution either though (also trying to find out how to do that as well... I'm brand new to OpenCV).
cap2 = cv2.VideoCapture(0)
cap2.set(3,320)
cap2.set(4,200)
ret2, image2 = cap2.read()
cv2.imshow('frame2',image2)
cv2.namedWindow('frame2',cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('frame2', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
i have found a trick, just put
cv2.namedWindow('frame2',cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('frame2', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
below
cv2.imshow('frame2',image2)
Here, what we are actualy doing is playing full video in smaller size. That's why there is no title bar and borders.
Did a little more looking around:
using these flags is how to do it with QT backend. CV_GUI_NORMAL or CV_GUI_EXPANDED: CV_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas CV_GUI_EXPANDED is a new enhanced GUI.
unfortunetly, cv2.namedWindow('image', flags=cv2.CV_GUI_EXPANDED) does not work, even though I'm pretty sure I have QT backend (actually I'm positive I do).
After looking up help(cv2), I found similar flags WINDOW_GUI_EXPANDED and WINDOW_GUI_NORMAL. So use those.
img = cv2.imread('messi.jpg',0)
# Removes toolbar and status bar
cv2.namedWindow('image', flags=cv2.WINDOW_GUI_NORMAL)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
But still having trouble trying to remove the title bar.

Python 3.6.3 pyautogui.locateOnScreen not working

I have a "find.png" (attached)
The pyautogui.locateOnScreen is returning None, even after 20 screenshots!!!!
Using interpreter (code attached)
Note: 1) the image having the line, "import pyautogui as auto", is "find.png"
2)I have "find.png" in the same directory, it is founded( checked with PIL- Image)
After struggling with this forever also, finally figured out that you either use command line or print screen button with the windows key to take the screenshot. Using the snipping tool does not work
so try:
image = pyautogui.screenshot()
image.save('testing.png')
Go and crop testing.png as small as possible so that locateOnScreen works faster. Then go back to the terminal and type:
pyautogui.locateOnScreen('testing.png')
Try this:
location = pyautogui.locateOnScreen('testing.png', confidence =.8)
None simply means that PyAutoGui was unable to find your image on the screen, do remember the window is active where find.png was sampled from.
If this doesn't work out then I believe the resolutions(from when you took the sample and the monitor used by you) are different.
Let us know if you face any problem.

Problems using webcam in python + openCV

I am using the following code to access my webcam using openCV + python...
import cv
cv.NamedWindow('webcam_feed', cv.CV_WINDOW_AUTOSIZE)
cam = cv.CaptureFromCAM(-1)
I am then getting the following error in the console...
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
I was originally using,
cv.CaptureFromCAM(0)
to access the same and had the same issue and used -1 because it is suppose to pick up any webcam.
I also tested to see if Ubuntu recognizes the webcam and it does. I am using v4l2 for the webcam.
{EDIT}
I am using the following code to display a video feed, it seems to only be showing just one image the web cam captured instead of a continuous video feed...
import cv
cv.NamedWindow('webcam_feed', cv.CV_WINDOW_AUTOSIZE)
cam = cv.CaptureFromCAM(-1)
feed = cv.QueryFrame(cam)
cv.ShowImage("webcam_feed", feed)
cv.WaitKey(-1)
WOW, answered my own question in 15 after me posting this. I did some research and the reason for the web cam only grabbing one image is because of the...
cv.WaitKey(-1)
This doesn't allow the contents of the window to refresh. I set the number to 10...
cv.WaitKey(10)
and it worked beautifully. I also tried 100, but saw no difference. I only saw a difference when the number was 1000. I use 1 because seems that it runs the smoothest.
Here is the full code to display a web cam feed
import cv
cv.NamedWindow("webcam", 1)
cam = cv.CaptureFromCAM(-1)
While True:
feed = cv.QueryFrame(cam)
cv.ShowImage("webcam", feed)
cv.WaitKey(1)
I believe you need to put
frame = cv.QueryFrame(cam)
cv.ShowImage("Webcam Feed", frame)
in a loop to continuously update the image shown in the window. That is, the frame from cv.QueryFrame is a static image, not a continuous video.
If you want to be able to exit with a key press, test cv.WaitKey with a small timeout in the loop too.
For me, the command in root
xhost +
save my time, Note to close and open new terminal.
See you.

Problem with Opencv and Python

I'm new to python and Opencv and I tried to put in the following code to save an image to my computer from my webcam:
import cv
if __name__=='__main__':
pCapturedImage = cv.CaptureFromCAM(1)
rospy.sleep(0.5)
pSaveImg=cv.QueryFrame(pCapturedImage)
cv.SaveImage("test.jpg", pSaveImg)
But when I try to open it,
I find that the jpeg is empty.
Could someone please help?
Also, I tried a program to show what my webcam is seeing:
import cv
if __name__=='__main__':
cv.NamedWindow("camera",1)
capture=cv.CaptureFromCAM(0)
while True:
img=cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10)==27:
break
cv.DestroyedWindow("camera")
But when I run it, I get an application that just shows me a gray screen.
Could someone help with this too?
Thanks.
Have you tried the demo programs? They show how to use the webcam among many other things.
For the first problem, I am not familiar with using cameras in opencv, but I got it to work by opening the capture (capture.open(device_id) in the code below)
Here is a working python sample (I use the newer c++ interface: imread, imwrite, VideoCapture, etc... which you can find in the OpenCV docs listed as "cv2" when it is available for python.):
import cv2
capture = cv2.VideoCapture() # this is the newer c++ interface
capture.open(0) # Use your device id; I think this is what you are missing.
image = capture.read()[1]
cv2.imwrite("test.jpg", image)
I got your second sample also working just by using open on the capture object:
import cv2
cv2.namedWindow("camera", 1) # this is where you will put the video images
capture = cv2.VideoCapture()
capture.open(0) # again, use your own device id
while True:
img = capture.read()[1]
cv2.imshow("camera", img)
if cv2.waitKey(10) == 27: # waiting for the esc key
break
cv2.destroyWindow("camera")

Categories