I do not understand this code taken from OpenCV documentation:
import cv2
import numpy as np
# mouse callback function
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
Specifically, I do not understand why the parameters of the draw_circle() function are that way but never used later. Can someone explain to me what's behind that, please?
Bellow my code is working fine just check it out..
import cv2
import numpy as np
img1=cv2.imread('Image/Desert.jpg')
def draw(event,x,y,flage,param):
if event==cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img1,(x,y),4,(0,0,0),4)
cv2.namedWindow('Image')
cv2.setMouseCallback('Image',draw)
while(1):
cv2.imshow('Image',img1)
if cv2.waitKey(20) & 0xFF ==27:
break
cv2.destroyAllWindows()
Related
I have the following code that prints the following image. Why don't I have the option to close the window (small Red Cross missing in top left corner)?
import cv2
img = cv2.imread('/Users/natashabustnes/Desktop/geeks14.png')
cv2.imshow('image', img)
cv2.waitKey(0)
Your code displays window and waits for a keypress.
When you pressed a key, waitKey returned and the GUI froze because there's was no more instructions.
Do something like this instead.
import cv2
img = cv2.imread('/Users/natashabustnes/Desktop/geeks14.png')
cv2.imshow('image', img)
while True:
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
This code waits until you press the 'q' button before closing.
OpenCV by default does not support closing windows using the normal close button.
i am new to open cv. i am drawing a circle on frame using the function. it works perfectly but dont understand how it works as i am passing no parameters to function. Below is the code. the function is draw_circle in the cv2.setMouseCallback.
import cv2
import numpy as np
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),100,(0,255,0),-1)
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow(winname='my_drawing')
#doesn't understand the below code how can we call the draw_circle without passing parameters
cv2.setMouseCallback('my_drawing',draw_circle)
while True:
cv2.imshow('my_drawing',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
That's because when you set a callback, you're not actually calling the function yourself, but plugging it in somewhere else that will call the function for you. In this case, whenever there is a mouse event, opencv internals will automatically call your function draw_circle. The arguments aren't provided by you, but by opencv, which keeps track of mouse movements and position and so on.
I have the code:
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im = cv2.imread(r'C:\Users\james\OneDrive\Desktop\logos\normal.png')
bbox, label, conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()
That detects most objects in the picture, but I want to be able to make it from live video, I tried using cap = cv2.VideoCapture(0) but I could not get it to work.
A typical program executes every line until it's done, and then exits. Your code asks it to open a stream and show a frame. Then, because it's done everything that you've asked it to do, it exits.
In the code you provided in the comments, you are using matplotlib's .clf() immediately after .show(), which will clear the figure and potentially is the cause of your issue. You should be using cv2.imshow() instead.
If you want it to remain open, you need to put your displaying code inside a while True loop as shown in the getting started part of the docs. Combining their code with yours, you would get something like (however I haven't tested it):
import numpy as np
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Detect objects and draw on screen
bbox, label, conf = cv.detect_common_objects(frame)
output_image = draw_bbox(im, bbox, label, conf)
cv2.imshow('output',output_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
For a webcam, this will keep going until you press q to interrupt the program.
Be warned, this may not run smoothly because object detection models are typically fairly computationally expensive. But that is a challenge for another question.
I am using OpenCV in python and the aim of the code is to display one image after another in the same window without destroying it. For eg. i1.jpg is displayed and then in the same window left arrow is displayed after a pause of 5s.
In my case, i1.jpg is displayed and then the window is destroyed and then i2.jpg is displayed in another window.
Here is the code for the same -
t_screen_time = 5000
import numpy
import cv2
left_arrow = "left_arrow.jpg"
right_arrow = "right_arrow.jpg"
img = cv2.imread(left_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
img = cv2.imread(right_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
The argument in cv2.waitKey() is the time to wait for a keypress and not exactly the time the image is displayed for.
cv::imshow() or cv2.imshow() was not designed to be a complete GUI. It was designed to let you quickly debug and display images/videos.
You can achieve what you need using some form of sleep() in a thread or so. But this is harder to do in python. You could also just edit your code to
t_screen_time = 5000
import numpy
import cv2
left_arrow = r"first/image"
right_arrow = r"second/image"
img = cv2.imread(left_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
img = cv2.imread(right_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
by removing the cv2.destroyAllWindows() before displaying the second image. I've tested this code and it works, but if you're looking for more GUI functionality, use something like Qt.
I'm running OpenCV 3 on Python 3.4 on a 2015 15" MacBook Pro. Below is a minimal example that illustrates my problem:
import cv2 as cv
import numpy as np
def mouse_callback(event, x, y, flags, param):
print("Callback!")
cv.namedWindow("Display")
cv.setMouseCallback("Display", mouse_callback)
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
cv.imshow("Display", frame)
if cv.waitKey(1) == ord("q"):
break
When I click on the screen, the text "Callback!" takes about 3 seconds to appear on the terminal screen. I'm not sure why I'm seeing this much lag--my laptop shouldn't be so bad that I can't even run this simple script.
Additionally, the problem persists when I reduce the webcam resolution, or even when I replace the webcam altogether with a still image. I rewrote a similar program in C++, and the C++ OpenCV libraries also suffer from this lag.
Any tips on how I can reduce or eliminate the lag?
Try this :
import cv2 as cv
import numpy as np
def mouse_callback(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
print("Callback!")
cv.namedWindow("Display")
cv.setMouseCallback("Display", mouse_callback)
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
cv.imshow("Display", frame)
if cv.waitKey(1) == ord("q"):
break
Although its mostly a problem with something else. Maybe your webcam resolution is too high.
So try this also:
cap.set(3,640)
cap.set(4,480)
Put the above code above the while loop and check.