open CV_7 라인 찾기

chi yeong Yu·2022년 2월 27일
0

opencv

목록 보기
7/12
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('balloon.png')
img1 = img.copy()

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imread(img, IMREAD_GRAYSCLAE) 보다 속도가 더 빠르다 한다.
# 실시간 이미지 처리를 하거나 빠르게 할 경우 cvtColor를 사용
ret, thresh = cv2.threshold(gray_img, 127, 255, 0)
# 이미지를 이진화 해 임계값보다 작으면 0 , 크면 255 로 표현 
# cv2.threshold(img, max_value, change_value, flag(특성))
                        #임계값, 바꿀값 
contours, hierachy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#이미지 윤곽선 검출
cnt = contours[0]
# contours[0]은 좌표

x, y, w, h = cv2.boundingRect(cnt)
# 좌료를 구하는 것 
img1 = cv2.rectangle(img1, (x, y),(x+w, y+h), (0, 255, 255), 3) #yellow
# cv2.rectangle 직사각형
rect = cv2.minAreaRect(cnt)
# contours를 지나면서 면적이 가장 작은 직사각형을 구하는것
box = cv2.boxPoints(rect)
# 직사각형의 각 꼭지점 좌표
box = np.int0(box)
# float 값으로 나오기에 int로 변형
img1 = cv2.drawContours(img1, [box], 0, (0, 0, 255), 3) # blue
#외곽선 검출
(x, y), radius = cv2.minEnclosingCircle(cnt)
center = (int(x), int(y))
radius = int(radius)
img1 = cv2.circle(img1, center, radius, (255, 0, 0), 3) # red
#좌표에 근사한 원을 그리기

ellipse = cv2.fitEllipse(contours[0])
img1 = cv2.ellipse(img1, ellipse, (0, 255, 0), 3) # green
# 최적의 타원을 그리기 위해 fitting시키는 것 

titles=['OG', 'NEW']
images=[img, img1]

for i in range(2):
    plt.subplot(1, 2, i+1), plt.title(titles[i]), plt.imshow(images[i])
    plt.xticks([]), plt.yticks([])

plt.show()

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('balloon.png')
img1 = img.copy()

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imread(img, IMREAD_GRAYSCLAE) 보다 속도가 더 빠르다 한다.
# 실시간 이미지 처리를 하거나 빠르게 할 경우 cvtColor를 사용
ret, thresh = cv2.threshold(gray_img, 125, 255, 0)
# 이미지를 이진화 해 임계값보다 작으면 0 , 크면 255 로 표현 
# cv2.threshold(img, max_value, change_value, flag(특성))
                        #임계값, 바꿀값 
contours, hierachy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 외곽선 찾기
cnt = contours[0]

# 각 꼭지점 좌표 
leftmost = tuple(cnt[cnt[:, :, 0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()])[0]
topmost = tuple(cnt[cnt[:, :, 1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

cv2.circle(img1, leftmost, 20, (0, 0, 255), -1) # Blue
cv2.circle(img1, rightmost, 20, (0, 0, 255), -1) # Blue
cv2.circle(img1, topmost, 20, (0, 0, 255), -1) # Blue
cv2.circle(img1, bottommost, 20, (0, 0, 255), -1) # Blue

img1 = cv2.drawContours(img1, cnt, -1, (255, 0, 0), 5) # Red
# 외곽선 그리기
titles = ['OG', 'NEW']
images=[img, img1]
for i in range(2):
    plt.subplot(1, 2, i+1), plt.title(titles[i]), plt.imshow(images[i])
    plt.xticks([]), plt.yticks([])
plt.show()

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('road.jpg')

road = np.float32([[1200, 600], [800, 750], [1400, 600], [1300, 750]])
# 좌표는 좌-상, 좌-하, 우-상, 우-하
road2 = np.float32([[10, 10], [10, 1000], [1000, 10], [1000, 1000]])

cv2.circle(img, (1200, 600), 20, (255, 0, 0), -1)
cv2.circle(img, (800, 750), 20, (0, 255, 0), -1)
cv2.circle(img, (1400, 600), 20, (0, 0, 255), -1)
cv2.circle(img, (1300, 750), 20, (0, 255, 255), -1)

m = cv2.getPerspectiveTransform(road, road2)
#원근 변환으로 시각에 따라 멀어보이기도하고 가깝게보이기도 하는 방법
#죄표를 지정해주면 변환 행렬을 구해준다.
dst = cv2.warpPerspective(img, m, (1450, 1450))
# 변환 함수를 적용하기 위해 함수를 사용 
plt.subplot(121), plt.imshow(img), plt.title('image')
plt.subplot(122), plt.imshow(dst), plt.title('perspective')
plt.show()

profile
호기심천국

0개의 댓글