Python Opencv, dont put circle on the video

2024/11/15 4:10:48

I wrote the following script with OpenCV

import cv2
import numpy as npcap = cv2.VideoCapture(0)
ix, iy = -1, -1def draw_circle(event, x, y, flags, param):global ixglobal iyix,iy = x,yif event == cv2.EVENT_LBUTTONDOWN:cv2.circle(img, (200, 399), 10, (0, 255, 255), -1)print("Text Put")elif event == cv2.EVENT_LBUTTONUP:print("EVENT_LBUTTONUP")elif event == cv2.EVENT_RBUTTONDOWN:print("Appuyé Droite")elif event == cv2.EVENT_RBUTTONUP:print("EVENT_RBUTTONUP")elif event == cv2.EVENT_MOUSEMOVE:print("Move on", x, y)while(1):ret, img = cap.read()cv2.setMouseCallback('image', draw_circle)cv2.imshow('image', img)if cv2.waitKey(20) & 0xFF == 27:breakcv2.destroyAllWindows()

But when I do a right button click, no circle appears on the display. What is wrong with my code ?

Answer

This code snippet may help you get started:

import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ix, iy = -1,-1def mouseCallback(event,x,y,flags,param):global ixglobal iyif event == cv2.EVENT_LBUTTONDOWN:ix = x # saves the position of the last clickiy = yelif event == cv2.EVENT_LBUTTONUP:print("EVENT_LBUTTONUP")elif event == cv2.EVENT_RBUTTONDOWN:print("Appuyé Droite")elif event == cv2.EVENT_RBUTTONUP:print("EVENT_RBUTTONUP")def draw_circle_onscreen(frame, x,y):cv2.circle(frame, (x,y), 10,(0, 0, 255),-1)cv2.namedWindow('frame')
cv2.setMouseCallback('frame',mouseCallback) # mouse callback has to be set only oncewhile(1):ret, img = cap.read()draw_circle_onscreen(img,ix,iy) # draws circle on screencv2.imshow('frame',img)if cv2.waitKey(20) & 0xFF == 27:breakcv2.destroyAllWindows()
https://en.xdnf.cn/q/119081.html

Related Q&A

List coordinates between a set of coordinates

This should be fairly easy, but Im getting a headache from trying to figure it out. I want to list all the coordinates between two points. Like so:1: (1,1) 2: (1,3) In between: (1,2)Or1: (1,1) 2: (5,1)…

NA values in column is not NaN Pandas Python [duplicate]

This question already has answers here:Prevent pandas from interpreting NA as NaN in a string(7 answers)Closed 2 years ago.I got a CSV File. I got a column Product. One of the products in it, called NA…

How to fix pandas column data

Workflow is :Read CSV file using Pythons pandas library and get Variation Column Variation Column data isVariation ---------- Color Family : Black, Size:Int:L Color Family : Blue, Size:Int:M Color Fam…

Connect to Oracle Database and export data as CSV using Python

I want to connect oracle database to python and using select statement whatever result i will get, I want that result to be exported as csv file in sftp location. I know we can connect oracle with pyth…

Pandas data frame: convert Int column into binary in python

I have dataframe eg. like below Event[EVENT_ID] = [ 4162, 4161, 4160, 4159,4158, 4157, 4156, 4155, 4154]need to convert each row word to binary. Event[b]=bin(Event[EVENT_ID]) doesnt work TypeError: can…

I have an issue : Reading Multiple Text files using Multi-Threading by python

Hello Friends, I hope someone check my code and helping me on this issue. I want to read from multiple text files (at least 4) sequentially and print their content on the screenFirst time not using Thr…

How to print \ in python?

print "\\"It print me in console...But I want to get string \How to get string string \?

Replace word, but another word with same letter format got replaced

Im trying to replace a word in python, but another word with same letter format got replaced example : initial : bg bgt goal : bang banget current result : bang bangtheres what my code…

Python: Split timestamp by date and hour

I have a list of timestamps in the following format:1/1/2013 3:30I began to learn python some weeks ago and I have no idea how to split the date and time. Can anyone of you help me?Output should be on…

ModuleNotFoundError: when importing curses in IDE

I get the error ModuleNotFoundError: No module named _curses every time I try to uses curses in VS Code or PyCharm. But it works in the command prompt (Im on Windows BTW) Code is from Tech With Tim tut…