๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (7)] ๋ฌธ์„œ ์Šค์บ”ํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Image Processing

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (7)] ๋ฌธ์„œ ์Šค์บ”ํ•˜๊ธฐ

์ง•์ง•์•ŒํŒŒ์นด 2023. 12. 28. 12:07
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’— ๋ฌธ์„œ ์Šค์บ”ํ•˜๊ธฐ 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
๋ฐ˜์‘ํ˜•
Comments