2つ画像を用意します。
※プロントで作業しています。ワイヤレスマウスUBS
import cv2
import numpy as np
img_last = cv2.imread(“a.jpg”)
img_last = cv2.cvtColor(img_last, cv2.COLOR_BGR2GRAY) # 白黒化
img_last = cv2.GaussianBlur(img_last, (9, 9), 0) # ぼかし
img_last = cv2.threshold(img_last, 100, 255, cv2.THRESH_BINARY)[1] # 画像の二値化
img = cv2.imread(“b.jpg”)
img_current = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_current = cv2.GaussianBlur(img_current, (9, 9), 0)
img_current = cv2.threshold(img_current, 100, 255, cv2.THRESH_BINARY)[1]
img_diff = cv2.absdiff(img_last, img_current)
cnts = cv2.findContours(img_diff, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
for pt in cnts:
x, y, w, h = cv2.boundingRect(pt)
if w < 30: continue
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite("result.png", img)
[/python]
OK, Let’s Gooooooooooooooooooooo