# Drawing in a video

Hey guys, I am doing an exercise in which the program has to follow that routine: when the user clicks on any point of a video, it gets that pixel and turns all pixels in a range of 5% into red. Since it is a vidoe, I am saving the points (x,y) the user clicks in a list and making the retangle for every point. Here is the code:

import numpy as np
import cv2

cap =cv2.VideoCapture(-1)
#creates list
lista= []
# mouse callback function
def getpixel(event,x,y,flags,param):
# adds point (x,y) in pixels ont the current list
if event == cv2.EVENT_LBUTTONDOWN:
lista.append([x,y])
print (x,y),img[x][y]

while(cap.isOpened()):
if ret:
cv2.imshow('image',img)
cv2.setMouseCallback('image',getpixel)
i=0
#draw the retangle
while i<lista.__len__():
img[int(0.95*lista[i][0]):int(1.05*lista[i][0]),int(0.95*lista[i][1]):int(1.05*lista[i][1])]=(0,0,255)
i+=1
if cv2.waitKey(20) & 0xFF == 27:
break

cap.release()
cv2.destroyAllWindows()


I have already checked and it is creating the list, adding the points and entering in the "while" part of the code. However, I can't see the retangles. What am I missing?

edit retag close merge delete

Sort by ยป oldest newest most voted

"What am I missing?" -- a cv2.imshow("lala", img) call before the cv2.waitKey() one.

then, this is the most creative ( or absurd, can't make up my mind :} ) way to draw a rectangle, please revisit:

>>> help(cv2.rectangle)
Help on built-in function rectangle:

rectangle(...)
rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img


(you could just use that, with tl and br points, and use -1 for thickness, to have it "filled")

more

Official site

GitHub

Wiki

Documentation