๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (7)] ๋ฌธ์ ์ค์บํ๊ธฐ ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/Image Processing
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (7)] ๋ฌธ์ ์ค์บํ๊ธฐ
์ง์ง์ํ์นด 2023. 12. 28. 12:07728x90
๋ฐ์ํ
๐ ๋ฌธ์ ์ค์บํ๊ธฐ by.์ํ
import cv2
import numpy as np
from datetime import datetime
from time import sleep
points = np.zeros((4,2), dtype=np.float32)
count = 0
def mouseHandler(event, x, y, flags, param):
global count
if event==cv2.EVENT_LBUTTONDOWN:
cv2.circle(img, (x,y), 5, (0, 0, 255), -1) #๋ฐ์ง๋ฆ 5ํฌ๊ธฐ ๋นจ๊ฐ ์
cv2.imshow("Capture", img)
try:
points[count] = [x, y]
count += 1
if count == 4:
sum_ = points.sum(axis=1)
diff = np.diff(points, axis=1)
top_left = points[np.argmin(sum_)]
bottom_right = points[np.argmax(sum_)]
top_right= points[np.argmin(diff)]
bottom_left = points[np.argmax(diff)]
pts1 = np.float32([top_left, top_right, bottom_right, bottom_left])
width_bottom = abs(bottom_right[0] - bottom_left[0])
width_top = abs(top_right[0] - top_left[0])
height_right = abs(top_right[1] - bottom_right[1])
height_left = abs(top_left[1] - bottom_left[1])
width = int(max([width_bottom, width_top]))
height = int(max([height_right, height_left]))
print(width, height) #
# ์ผ๊ฐํจ์๋ฅผ ์ด์ฉํ ๋๊ฐ์ ์ ๊ธธ์ด ๊ณ์ฐ
# w1 = np.sqrt(abs(top_left[0]-top_right[0])**2 +
# abs(top_left[1]-top_right[1])**2)
# w2 = np.sqrt(abs(bottom_left[0]-bottom_right[0])**2 +
# abs(bottom_left[1]-bottom_right[1])**2)
# width = int(max(w1, w2))
# h1 = np.sqrt(abs(top_left[0]-bottom_left[0])**2 +
# abs(top_left[1]-bottom_left[1])**2)
# h2 = np.sqrt(abs(top_right[0]-bottom_right[0])**2 +
# abs(top_right[1]-bottom_right[1])**2)
# height = int(max(h1, h2))
# print(width, height)
pts2 = np.float32([[0,0], [width-1,0], [width-1,height-1], [0,height-1]])
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (width, height))
cv2.imshow("Capture", img)
cv2.imshow("Scanned", dst)
except Exception as e:
print(e)
cap = cv2.VideoCapture(0)
captured = False
if cap.isOpened():
delay = int(1000 / cap.get(cv2.CAP_PROP_FPS))
while True:
ret, img = cap.read()
if ret:
cv2.imshow("Capture", img)
key = cv2.waitKey(delay)
if key & 0xFF == 27 :
print("์๋ฌด ์์
๋ ํ์ง ์๊ณ ์ข
๋ฃํจ")
break
elif key == ord('c'): # ์บก์ณ ์์์ cํค๋ฅผ ๋๋ฅด์ธ์.
captured = True
break
else:
print(ret, img)
break
else:
print("File not opened")
if captured:
cap.release()
while True:
cv2.imshow("Capture", img)
cv2.setMouseCallback("Capture", mouseHandler)
key = cv2.waitKey(delay)
if key & 0xFF == 27 :
print("ESC Key pressed")
break
cap.release()
cv2.destroyAllWindows()
c ๋๋ฅด๋ฉด ํ๋ฉด์ด ๋ฉ์ถฐ์ฉ
๊ผญ์ง์ ๋ค๊ฐ ํด๋ฆญํ๋ฉด scan ๋ฉ๋๋ค
728x90
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > Image Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments