Ask Your Question
0

Drawing in a video

asked 2017-04-02 10:48:24 -0600

vitorbordini96 gravatar image

updated 2017-04-02 10:53:23 -0600

berak gravatar image

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()):
     ret,img= cap.read()
     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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-02 11:00:31 -0600

berak gravatar image

updated 2017-04-02 11:01:57 -0600

"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")

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-04-02 10:48:24 -0600

Seen: 5,787 times

Last updated: Apr 02 '17