Skip to content
Snippets Groups Projects
Commit 32b8c0bd authored by parkminwoo's avatar parkminwoo
Browse files

delete unnecessary

parent 7efe4098
No related branches found
No related tags found
No related merge requests found
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:code id:solved-logan tags:
``` python
# 가장 큰 영역을 갖는 컨투어를 찾는다.
# param : contours - 컨투어들의 리스트이다.
# return : max_index - contours에서 가장 큰 영역을 갖는 컨투어의 인덱스이다.
def findMaxArea(contours):
max_area = -1
max_index = -1
# 가장 큰 영역을 갖는 컨투어를 찾는다.
for i,contour in enumerate(contours):
# cv2.contourArea : 컨투어의 넓이를 얻는다.
area = cv2.contourArea(contour)
# cv.boundingRect : 컨투어 라인을 둘러싸는 사각형을 그린다.
x,y,w,h = cv2.boundingRect(contour)
if (w*h)*0.4 > area:
continue
if w > h:
continue
if area > max_area:
max_area = area
max_index = i
if max_area < 10000:
max_index = -1
return max_index
```
%% Cell type:code id:parental-police tags:
``` python
# 이미지(문서 영역)를 정면 방향으로 변환한다.
# param : img_input - 정면 방향으로 변환할 이미지이다, points - img_input의 꼭짓점이다.
# return : img_warped - 정면 방향으로 변환된 이미지이다.
def transform(img_input, points):
# sort_points(): 다음 순서를 갖도록 꼭지점을 정렬한다.
# top left, top right, bottom right, bottom left
points = sort_points(points)
topLeft, topRight, bottomRight, bottomLeft = points
print(topLeft, topRight, bottomRight, bottomLeft)
print(topLeft[0] + topLeft[1], topRight[0]+topRight[1],
bottomRight[0]+bottomRight[1], bottomLeft[0]+bottomLeft[1])
# 변환 후, 이미지(문서 영역)의 너비와 높이를 결정한다.
topWidth = distance(bottomLeft, bottomRight)
bottomWidth = distance(topLeft, topRight)
maxWidth = max(int(topWidth), int(bottomWidth))
leftHeight = distance(topLeft, bottomLeft)
rightHeight = distance(topRight, bottomRight)
maxHeight = max(int(leftHeight), int(rightHeight))
# 정면 방향의 이미지(문서 영역)의 좌표를 결정한다.
dst = np.array([[0, 0],[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]],
dtype = "float32")
# 정면 방향의 이미지(문서 영역)로 변환한다.
H = cv2.getPerspectiveTransform(points, dst)
img_warped = cv2.warpPerspective(img_input, H, (maxWidth, maxHeight))
return img_warped
```
%% Cell type:code id:stupid-playback tags:
``` python
# 다음 순서를 갖도록 꼭짓점을 정렬한다.
# top left, top right, bottom right, bottom left
# param : points - 정렬되기 전의 꼭짓점의 ndarray이다.
# return : new_points - 정렬된 후의 꼭짓점의 ndarray이다.
def sort_points(points):
points = points.astype(np.float32)
new_points = np.zeros((4, 2), dtype = "float32")
# top left를 구한다.
s = points.sum(axis = 1)
min_index = np.argmin(s)
new_points[0] = points[min_index]
points = np.delete(points, min_index, axis = 0)
# bottom right를 구한다.
s = points.sum(axis = 1)
max_index = np.argmax(s)
new_points[2] = points[max_index]
points = np.delete(points, max_index, axis = 0)
# top right, bottom left를 구한다.
v0 = points[0] - new_points[0]
v1 = points[1] - new_points[0]
# angle_between() : 벡터 사이의 각도를 구한다.
angle = angle_between(v0, v1)
if angle < 0:
new_points[1] = points[1]
new_points[3] = points[0]
else:
new_points[1] = points[0]
new_points[3] = points[1]
return new_points
```
%% Cell type:code id:antique-acrobat tags:
``` python
# 두 벡터 사이의 각도를 구한다.
# param : A, B - 벡터를 표현한 ndarray이다.
# return : angle - 벡터 A와 B 사이의 각도이다.
def angle_between(A, B):
x1 = A[0]
y1 = A[1]
x2 = B[0]
y2 = B[1]
dot = x1*x2 + y1*y2
det = x1*y2 - y1*x2
angle = np.arctan2(det, dot) * 180/np.pi
return angle
```
%% Cell type:code id:ambient-canberra tags:
``` python
# 사용자의 마우스 입력을 통하여 이미지(문서 영역)의 꼭지점을 조정하는 마우스 콜백 함수이다.
# param : event - 마우스 이벤트이다, (x, y) - 윈도우 창을 기준으로 좌측 상단점 좌표이다,
# flags - 마우스 이벤트가 발생할 때 키보드 또는 마우스 상태이다,
# param - 전달하고 싶은 데이터로 사용하지 않아도 매개변수로 받아야 한다.
# return : none
def mouse_callback(event, x ,y, flags, param):
global mouse_is_pressing, points
# step == 1 이면,마우스 콜백 함수가 등록된 것이다.
# step != 1 이면, cv2.setMouseCallback()을 통해 마우스 콜백 함수가 등록되지 않은 것이다.
if step != 1:
return
# CASE 1 - 마우스가 움직이고
if event == cv2.EVENT_MOUSEMOVE:
# 눌린 상태일 때,
if mouse_is_pressing == True:
# 마우스의 좌표와, 포인트(꼭짓점)의 거리가 가깝다면, 꼭지점을 이동한다.
for i,point in enumerate(points):
if distance((x,y), point) < 15:
points[i][0] = x
points[i][1] = y
break
# CASE 2 - 마우스 왼쪽 버튼이 눌린 상태일 때, `
elif event == cv2.EVENT_LBUTTONDOWN:
# 꼭짓점과의 거리가 가깝다면 mouse_is_pressing을 True로 바꾼다.
for point in points:
if distance((x,y), point) < 10:
mouse_is_pressing = True
break
# CASE3 - 마우스 왼쪽 버튼이 눌려있지 않으면, mouse_is_pressing을 False로 바꿈
elif event == cv2.EVENT_LBUTTONUP:
mouse_is_pressing = False
```
%% Cell type:code id:circular-clear tags:
``` python
# 두 점 사이의 거리를 구한다.
# param : point1, point2 - 거리를 구할 두 점이다.
# return : point1, point2 사이의 거리이다.
def distance(point1, point2):
x1, y1 = point1
x2, y2 = point2
return int(np.sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment