File "<ipython-input-6-b985bbbd8c62>", line 21
cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
^
IndentationError: expected an indented block
my code
import cv2
import numpy as np
#variables
#True while mouse button down, False while mouse button up
drawing = False
ix,iy = -1
#Function
def draw_rectangle(event,x,y,param,flags):
global ix,iy,drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
#Showing images with opencv
#black
img = np.zeros((612,612,3))
cv2.namedwindow(winname='draw_painting')
cv2.setMouseCallback('draw_painting',draw_rectangle)
while True:
cv2.imshow('draw_painting',img)
cv2.waitkey(20) & 0xFF = 27:
break
cv2.destryAllWindows()
This is your current code:
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
Note that, after the if statement, you need to indent your code:
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
Other errors:
Also, it seems that you intend your final if statement to be inside an if block and remember to use ==
rather than =
to check for equality.
You need to give indentation for the line after the if statement
if drawing == True:
cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.