Opencv draw a rectangle in a picture were never shown - python-3.x

Hey everybody i have some trouble using opencv 3.x and python 3.x.
What i want to do is to draw a basic rectangle in a picture but the rectangle will never be drawn.
I read this similar thread but it doesn't helped me with my fault.
Python OpenCV: mouse callback for drawing rectangle
It would be nice if someone could give me a hint.
#!/usr/bin/env python3
import cv2
import numpy as np
Path = 'picture.jpg'
image_float_size = 400.0
image_int_size = int(image_float_size)
color = [0,255,0]
rectangle = False
def on_event(event,x,y,flags,param):
global startpointx,startpointy,rectangle
if event == cv2.EVENT_LBUTTONDOWN:
rectangle = True
startpointx = x
startpointy = y
print('Down',x,y) #debugging
cv2.rectangle(resized,(x,y),(x,y),(0,255,0),-1)
elif event == cv2.EVENT_LBUTTONUP:
rectangle = False
print('Up',x,y)
cv2.rectangle(resized,(startpointx,startpointy),(x,y),(0,255,0),-1)
elif event == cv2.EVENT_MOUSEMOVE:
if rectangle:
print('Move',startpointx,startpointy,x,y)#debugging
cv2.rectangle(resized,(startpointx,startpointy),(x,y),(0,255,0),-1)
# Read the image and convert it into gray
image = cv2.imread(Path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# resize the image
ration = image_float_size / gray_image.shape[1]
dim = (image_int_size,int(gray_image.shape[0]*ration))
resized = cv2.resize(gray_image, dim, interpolation = cv2.INTER_AREA)
# set window for the image
cv2.namedWindow('window')
# mouse callback
cv2.setMouseCallback('window',on_event)
# wait forever for user to press any key, after key pressed close all windows
while True:
cv2.imshow('window',resized)
if cv2.waitKey(0):
break
cv2.destroyAllWindows()

You perform drawing (displaying of an image by using cv2.imshow) only once because cv2.waitKey(0) waits indefinitely. If you use some non-zero argument it will wait for that number of milliseconds. But notice that you're constantly rewriting/modifying an image. This is probably not what you want. I think you need to create a temporary (drawing) copy of an image first and restore it each time from original one before new drawing (rectangle).
#!/usr/bin/env python3
import cv2
import numpy as np
Path = 'data/lena.jpg'
image_float_size = 400.0
image_int_size = int(image_float_size)
color = [0,255,0]
rectangle = False
def on_event(event,x,y,flags,param):
global draw_image
global startpointx,startpointy,rectangle
if event == cv2.EVENT_LBUTTONDOWN:
rectangle = True
startpointx = x
startpointy = y
print('Down',x,y) #debugging
draw_image = resized.copy()
cv2.rectangle(draw_image,(x,y),(x,y),(0,255,0))
elif event == cv2.EVENT_LBUTTONUP:
rectangle = False
print('Up',x,y)
draw_image = resized.copy()
cv2.rectangle(draw_image,(startpointx,startpointy),(x,y),(0,255,0))
elif event == cv2.EVENT_MOUSEMOVE:
if rectangle:
print('Move',startpointx,startpointy,x,y)#debugging
draw_image = resized.copy()
cv2.rectangle(draw_image,(startpointx,startpointy),(x,y),(0,255,0))
# Read the image and convert it into gray
image = cv2.imread(Path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# resize the image
ration = image_float_size / gray_image.shape[1]
dim = (image_int_size,int(gray_image.shape[0]*ration))
resized = cv2.resize(gray_image, dim, interpolation = cv2.INTER_AREA)
draw_image = resized.copy()
# set window for the image
cv2.namedWindow('window')
# mouse callback
cv2.setMouseCallback('window',on_event)
while True:
cv2.imshow('window', draw_image)
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
cv2.destroyAllWindows()

Related

Is there a way to tell if pyautogui is detecting the color/clicking?

import cv2
import numpy as np
import pyautogui
SCREEN_SIZE = (1920, 1080)
#define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
#create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
while True:
#make a screenshot
img = pyautogui.screenshot(region=(680, 785, 560, 20))
#convert these pixels to a proper numpy array to work with OpenCV
frame = np.array(img)
#convert colors from BGR to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
black = [0,0,0]
for x in range(img.width):
for y in range(img.height):
if img.getpixel((x, y)) == black:
print(x, y)
pyautogui.click(x, y)
#write the frame
out.write(frame)
#show the frame
cv2.imshow("screenshot", frame)
# if the user clicks q, it exits
if cv2.waitKey(1) == ord("q"):
break
# make sure everything is closed when exited
cv2.destroyAllWindows()
out.release()
I am creating a script to detect black squares, and click them. For some reason when using this, there is no error, but it is not clicking. Is there a way to tell whether it is detecting the color/clicking?
Edit: It does not output the coordinates, and when changing it to print "black" once finding the color, there is still no output.

How to remove shadows from a video that has static background?

I am trying to detect moving object and remove shadow from a video that has a static background. I am using Mixture of Gaussians(MOG) method to detect moving objects. I am using opencv3 and python 3.5. How can I remove shadows from the video and foreground mask both? I have used erosion and dilation for reducing noise. But it doesn't remove the shadows.
import cv2
import numpy as np
cap = cv2.VideoCapture('TownCentreXVID.avi')
fgbg = cv2.createBackgroundSubtractorMOG2()
while(1):
_, frame = cap.read()
mask = fgbg.apply(frame)
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
window = cv2.namedWindow('Original', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO )
window = cv2.namedWindow('Mask', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO)
window = cv2.namedWindow('Opening', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO )
#window = cv2.namedWindow('Closing', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO)
cv2.imshow('Original',frame)
cv2.imshow('Mask',thresh)
cv2.imshow('Opening',opening)
#cv2.imshow('Closing',closing)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
The backgroundsubtractor returns a mask where foreground object are white and shadows are gray.
You can use thresholding to create a new mask without shadow, or with only the shadow.
Use the mask without the shadows to get only the foreground.
Use the mask with only shadow to replace the shadow on the background (with a reference background image).
Result:
Code:
import cv2
import numpy as np
# load image / mask
mask = cv2.imread("mask.png",0)
#threshold mask
ret, foreground = cv2.threshold(mask, 200, 255, cv2.THRESH_BINARY)
ret, shadow = cv2.threshold(mask, 200, 255, cv2.THRESH_TOZERO_INV)
# stack images vertically
res = np.concatenate((mask,foreground,shadow),axis=0)
#show image
cv2.imshow("Result",res)
cv2.waitKey(0)
cv2.destroyAllWindows()

Create Iterable mouse click event in Python?

I am trying to show image to user and asking to click on 4 points for a rectangle from user and once user is done he will press 'c' and exit and my script should return the touch points by user.
For this approach I have written below script using OpenCV,, but not sure how to make class for this purpose iterable, might be I am wrong with some OO technical part but requirement and logic is correct because when not created inside class both functions works fine.
Code:
class mouse_select_points:
''' Select 4 user points from user and crop to straighten'''
__depends__ = ['img_path']
__provides__ = ['ratio','image_resized','click_points']
def __init__(self, thickness=2,shape=(400,600),click_count=0,click_points=[],img_path=""):
self.thickness = thickness
self.shape = shape
self.lent = click_count
self.refPt = click_points
self.img = img_path
self.font = cv2.FONT_HERSHEY_SIMPLEX
def __call__(self):
image = cv2.imread(self.img)
print("first loaded image size:",image.shape)
orig_resized = image.copy() #create a copy of resized image
ratio = image.shape[1] / 600.0
self.shape = image.shape
cv2.namedWindow("image")
cv2.setMouseCallback("image", self._click_and_crop, param = [image] ) #setting param as image to be sent to mouse click function callback
# keep looping until the 'c' key is pressed
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
cv2.putText(image,"press 'c' to crop or 'r' to reset",(10,15), self.font, .5,(255,255,255),1,cv2.LINE_AA)
key = cv2.waitKey(1) & 0xFF
# if the 'c' key is pressed, break from the loop
elif key == ord("c") and self.lent == 4:
break
return ratio,orig_resized,self.refPt
def _click_and_crop(self,event, x, y, flags, param):
image = param[0]
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being performed
if event == cv2.EVENT_LBUTTONDOWN:
self.refPt.append([x, y])
cv2.circle(image,(int(x),int(y)),self.thickness,(255,1,255),-1)
self.lent += 1
print("inside if")
cv2.imshow("image", image)
##testing
ratio,orig_image = mouse_select_points(img_path=r"Image1.JPG")
I think you want to pick the points repeatly. Here is it.
#!/usr/bin/python3
# 2018.06.13 13:40:12 CST
# 2018.06.13 14:17:49 CST
import cv2
class PickPoints:
''' Select 4 user points from user and crop to straighten'''
__depends__ = ['img_path']
__provides__ = ['ratio','image_resized','click_points']
def __init__(self, thickness=2,shape=(400,600),click_count=0,click_points=[],img_path=""):
self.thickness = thickness
self.shape = shape
self.lent = click_count
self.refPt = click_points
self.img = img_path
self.font = cv2.FONT_HERSHEY_SIMPLEX
def __call__(self):
image = cv2.imread(self.img)
#print("first loaded image size:",image.shape)
print("="*60)
orig_resized = image.copy() #create a copy of resized image
self.shape = image.shape
self.lent = 0
self.refPt = []
self.to_exit = False
cv2.namedWindow("image")
cv2.setMouseCallback("image", self._click_and_crop, param = [image] ) #setting param as image to be sent to mouse click function callback
# keep looping until the 'c' key is pressed
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
cv2.putText(image,"press 'c' to crop or 'r' to reset",(10,15), self.font, .5,(255,255,255),1,cv2.LINE_AA)
key = cv2.waitKey(30) & 0xFF
# if the 'c' key is pressed, break from the loop
if key == ord("r"):
print("Reset")
self.lent = 0
self.refPt = []
image[:] = orig_resized
if key == ord("c"):
pass
if self.to_exit or key in (27, ord('q'), ord('Q')):
print("Exist")
cv2.destroyAllWindows()
self.to_exit = False
break
return self.refPt
def _click_and_crop(self,event, x, y, flags, param):
image = param[0]
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being performed
if event == cv2.EVENT_LBUTTONDOWN:
if len(self.refPt) <4:
self.refPt.append([x, y])
cv2.circle(image,(int(x),int(y)),self.thickness,(0,255,0),-1)
self.lent += 1
print("#{} {}".format(self.lent, self.refPt[-1]))
cv2.imshow("image", image)
if event == cv2.EVENT_RBUTTONDOWN:
self.to_exit = True
##testing
fname = "/home/auss/Pictures/test.png"
pk = PickPoints(img_path=fname)
print(pk())
print(pk())
print(pk())

grabscreen.py Python win32api

Any Linux or Mac OS equivalent libraries to Win32gui, or to this code ?
working on an outside project and this windows code will help me grab the screen. Havent been able to find any libraries that are similar. Thank you
def grab_screen(region=None):
hwin = win32gui.GetDesktopWindow()
if region:
left,top,x2,y2 = region
width = x2 - left + 1
height = y2 - top + 1
else:
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
signedIntsArray = bmp.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (height,width,4)
srcdc.DeleteDC()
memdc.DeleteDC()
win32gui.ReleaseDC(hwin, hwindc)
win32gui.DeleteObject(bmp.GetHandle())
return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
You can grab the screen with pyautogui:
import pyautogui
image = pyautogui.screenshot('filename.png')
You can do this :)
I think Mac OS can't use those WiIn32gui libraries.
instead you can use pillow for grabbing screen.
Screen size can be changed depends on the size you want.
import cv2
import numpy as np
import pyautogui
from PIL import ImageGrab
screen_w = 1920
screen_h = 1080
while True:
rgb = ImageGrab.grab(bbox=(0, 0, screen_w, screen_h))
rgb = np.array(rgb)
cv2.imshow('window_frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

How to save video Python 3.5

I have a video 256x300 uint8 (gray). I am tracking an object and displaying a rectangle in the video. I'd like to save that video (with the rectangle displayed). I tried some examples but they are all for RGB video.
import cv2
import sys
import subprocess as sp
if __name__ == '__main__' :
# Set up tracker.
# Instead of MIL, you can also use
# BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN
tracker = cv2.Tracker_create("MIL")
# Read video
video = cv2.VideoCapture("videotest.avi")
# Exit if video not opened.
if not video.isOpened():
print("Could not open video")
sys.exit()
# Read first frame.
ok, frame = video.read()
if not ok:
print("Cannot read video file")
sys.exit()
# Define an initial bounding box
bbox = (90, 10, 30, 30)
# Uncomment the line below to select a different bounding box
# bbox = cv2.selectROI(frame, False)
# Initialize tracker with first frame and bounding box
ok = tracker.init(frame, bbox)
while True:
# Read a new frame
ok, frame = video.read()
if not ok:
break
# Update tracker
ok, bbox = tracker.update(frame)
# Draw bounding box
if ok:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (0,0,255))
# Display result
cv2.imshow("Tracking", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27 : break
video.release()
cv2.destroyAllWindows()

Resources