OpenCV 마우스 이벤트

tpids·2024년 7월 12일

OpenCV

목록 보기
7/8

반자동 문서 스캐너 프로그램 만들기

마우스 이벤트 설정

import cv2 
import numpy as np
import matplotlib.pyplot as plt
# 이미지 선택해서 cv2로 이미지 창 띄우기 
img = cv2.imread('./data/pets.jpg')
cv2.imshow('img', img)

def mouse_click(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x,y, '왼쪽 마우스 클릭')

cv2.namedWindow('img')
cv2.setMouseCallback('img',mouse_click)
cv2.waitKey(0)
cv2.destroyAllWindows()

#
349 114 왼쪽 마우스 클릭

사다리꼴 펴기

# 이미지 읽기 출력
img = cv2.imread('./data/newspaper.jpg')

# 출력될 최종 이미지 크기 width, height(x,y)
width, height = 640,240 # 가로, 세로

# 접근 좌표 정리하기 : 좌상, 우상, 우하, 좌하(시계방향 4지점(x,y)) > 좌표값을 리스트에 담기
xy_list = []  # [[좌상x,y],[우상x,y],...]

def mouse_click(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy_list.append([x,y])

cv2.imshow('img', img)
cv2.namedWindow('img')
cv2.setMouseCallback('img',mouse_click)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(xy_list)

#
[[515, 352], [992, 351], [1105, 571], [467, 556]]
# 이미지 크롭(crop: 잘라내기)
src = np.array(xy_list, np.float32) # 입력된 좌표 4개
# 좌표의 이동점(새로운 윈도우 창 크기값 지정 부분)
dst = np.array([[0,0],[width,0],[width,height],[0,height]], np.float32)

matrix = cv2.getPerspectiveTransform(src, dst) # matrix 얻기(변환 데이터 확인)
result = cv2.warpPerspective(img, matrix, (width,height)) # matrix 크기대로 변

cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

회전 이미지 바로 세우기

# pocker.jpg
img = cv2.imread('./data/poker.jpg')
cv2.imshow('img', img)

xy_list = []
def mouse_click(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy_list.append([x,y])

cv2.namedWindow('img')
cv2.setMouseCallback('img',mouse_click)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 이미지 크롭(crop: 잘라내기)
width, height = 320,460 # 가로x, 세로y
src = np.array(xy_list, np.float32) # 입력된 좌표 4개
# 좌표의 이동점(새로운 윈도우 창 크기값 지정 부분)
dst = np.array([[0,0],[width,0],[width,height],[0,height]], np.float32)

matrix = cv2.getPerspectiveTransform(src, dst) # matrix 얻기(변환 데이터 확인)
result = cv2.warpPerspective(img, matrix, (width,height)) # matrix 크기대로 변

cv2.imshow('result', result)
# cv2.imwrite('./data/scan_img2.jpg',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
src_img = cv2.imread('./data/poker.jpg')

point_list = []
COLOR = (255,0,255)

def mouse_handler(event, x,y, flags, param):
    
    if event == cv2.EVENT_LBUTTONDOWN: # 왼쪽 손가락 클릭(다운)
        point_list.append((x,y))
   
    # 클릭한 횟수만큼만 circle출력
    # for point in point_list:
        cv2.circle(src_img, (x,y), 15, COLOR, cv2.FILLED)
            
    if len(point_list)==4:
        show_result()
    
    cv2.imshow('img', src_img)

def show_result():
    width, height = 530,710
    src = np.float32(point_list)
    dst = np.array([[0,0],[width, 0],[width, height],[0,height]], dtype = np.float32)
    # 좌상, 우상, 우하, 좌하 ( 시계방향 4지점 설정)
    
    matrix = cv2.getPerspectiveTransform(src, dst)
    result = cv2.warpPerspective(src_img, matrix,(width, height))
    cv2.imshow('result', result)
    cv2.imwrite('./data/scan_img.jpg',result)


cv2.namedWindow('img')
cv2.setMouseCallback('img', mouse_handler)
cv2.waitKey(0)
cv2.destroyAllWindows()
profile
개발자

0개의 댓글