본문 바로가기

OpenCV

OpenCV 파이선 강의 실습 코드 4

반응형

 

강의 코드 시작은 OpenCV 공간 기반 처리 1  

 

강의 자료 다운받는 곳

 

OpenCV 강의 전체 소스코드

 

OpenCV 파이선 강의 실습 코드 1 

OpenCV 파이선 강의 실습 코드 2 

OpenCV 파이선 강의 실습 코드 3 

 

 

회색조 영상에 대하여 사용자가 정의한 마스크로 컨볼루션을 수행  p.281

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

kernel1 = np.ones((5,5), dtype = np.float64) / 25.0
kernel2 = cv2.getGaussianKernel(5, 3)
kernel3 = np.array([[-1, 0, 1],
                    [-1, 0, 1],
                    [-1, 0, 1]])
kernel4 = np.array([[0, -1, 0],
                    [-1, 4, -1],
                    [0, -1, 0]])

k_image1 = cv2.filter2D(g_image, -1, kernel1)
k_image2 = cv2.filter2D(g_image, -1, kernel2)
k_image3 = cv2.filter2D(g_image, -1, kernel3)
k_image4 = cv2.filter2D(g_image, -1, kernel4)

cv2.imshow('Original Image', g_image)
cv2.imshow('Filtering Image 1', k_image1)
cv2.imshow('Filtering Image 2', k_image2)
cv2.imshow('Filtering Image 3', k_image3)
cv2.imshow('Filtering Image 4', k_image4)

cv2.waitKey(0)

 

 

5x5 크기 커널과 11x11 크기 커널을 이용하여 평균값 필터링을 수행  slide p.291

 

import cv2

i_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)

f_image1 = cv2.blur(i_image, (5, 5))
f_image2 = cv2.blur(i_image, (11, 11), anchor=(-1, -1))

cv2.imshow('Original Image', i_image)
cv2.imshow('Average filtered image (5 x 5)', f_image1)
cv2.imshow('Average filtered image (11 x 11)', f_image2)

cv2.waitKey(0)

 

 

가우시안 필터링 (Gaussian filtering) 

입력 영상에 대하여 5x5 크기의 커널과 11x11 크기의 커널을 이용하여 가우시안 필터링을 수행하는 코드 slide 297  

 

import cv2

i_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)

f_image1 = cv2.blur(i_image, (5, 5), 0)
f_image2 = cv2.blur(i_image, (5, 5), 3)
f_image3 = cv2.blur(i_image, (11, 11), 0)
f_image4 = cv2.blur(i_image, (11, 11), 3)

cv2.imshow('Original Image', i_image)
cv2.imshow('(5x5, 0) Gaussian filtered image', f_image1)
cv2.imshow('(5x5, 3) Gaussian filtered image', f_image1)
cv2.imshow('(11x11, 0) Gaussian filtered image', f_image1)
cv2.imshow('(11x11, 3) Gaussian filtered image', f_image1)

cv2.waitKey(0)

 

 

중간값 필터링 

입력한 영상에 대하여 3x3 크기의 커널과 9x9 크기의 커널을 이용하여 중간값 필터링을 수행  slide 302

 

import cv2

i_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)

f_image1 = cv2.medianBlur(i_image, 3)
f_image2 = cv2.medianBlur(i_image, 9)

cv2.imshow('Original Image', i_image)
cv2.imshow('(3x3) Median filtered image', f_image1)
cv2.imshow('(9x9) Median filtered image', f_image2)

cv2.waitKey(0)

 

 

양방향 필터링 (Bilateral filtering) 

양방향 필터링을 수행하고, 가우시안 필터링과 차이를 보여주는 코드  slide 308

 

import cv2

i_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)

f_image1 = cv2.bilateralFilter(i_image, 9, 75,75)


f_image2 = cv2.GaussianBlur(i_image, (9, 9), 0)
f_image3 = cv2.normalize(f_image1 - f_image2, None, 0, 255,
                         cv2.NORM_MINMAX)

cv2.imshow('Original Image', i_image)
cv2.imshow('Bilateral filtered image', f_image1)
cv2.imshow('Gaussian filtered image', f_image2)
cv2.imshow('Difference', f_image3)

cv2.waitKey(0)

 

 

로버츠 (Roberts) 및 프리윗 (Prewitt) 필터 

회색조 영상에 대하여 로버츠 필터와 프리윗 필터를 적용하여 출력 slide 320  

 

 

import cv2
import numpy as np

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

roberts_x = np.array([[0,0,-1],[0,1,0],[0,0,0]])
roberts_y = np.array([[-1,0,0],[0,1,0],[0,0,0]])

prewitt_x = np.array([[1,0,-1],[1,0,-1],[1,0,-1]])
prewitt_y = np.array([[-1,-1,0],[0,0,0],[1,1,1]])

r_imageX = cv2.convertScaleAbs(cv2.filter2D(g_image, -1, roberts_x))
r_imageY = cv2.convertScaleAbs(cv2.filter2D(g_image, -1, roberts_y))

p_imageX = cv2.convertScaleAbs(cv2.filter2D(g_image, -1, prewitt_x))
p_imageY = cv2.convertScaleAbs(cv2.filter2D(g_image, -1, prewitt_y))

cv2.imshow('Original Image', g_image)
cv2.imshow('Roberts X direction image', r_imageX)
cv2.imshow('Roberts Y direction image', r_imageY)
cv2.imshow('prewitt X direction image', p_imageX)
cv2.imshow('prewitt Y direction image', p_imageY)

cv2.waitKey(0)

 

 

소벨 필터 (Sobel filter) 

회색조 영상에 대하여 소벨 필터(마스크)를 이용하여 x 방향 변화율, y 방향 변화율, x-y 방향 변화율을 계산  p.324

 

import cv2
import numpy as np

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

s_imageX  =cv2.Sobel(g_image, cv2.CV_8U, 1, 0, ksize=3 )
s_imageY  =cv2.Sobel(g_image, cv2.CV_8U, 0, 1, ksize=3 )
s_imageXY  =cv2.Sobel(g_image, cv2.CV_8U, 1, 1, ksize=3 )

cv2.imshow('Original Image', g_image)
cv2.imshow('Sobel X direction image', s_imageX)
cv2.imshow('Sobel Y direction image', s_imageY)
cv2.imshow('Sobel X-Y direction image', s_imageXY)

cv2.waitKey(0)

 

 

라플라시안 필터 (Laplacian filter) 

회색조 영상에 라플라시안 필터(마스크)를 이용하여 경계선을 검출  p.333

 

import cv2
import numpy as np

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

l_imageX  =cv2.Laplacian(g_image, cv2.CV_8U, ksize=3 )


cv2.imshow('Original Image', g_image)
cv2.imshow('Laplacian image', l_imageX)

cv2.waitKey(0)

 

 

Scharr 필터 

회색조 영상에 대하여 Scharr 필터(마스크)를 이용하여 x 방향 변화율, y 방향 변화율을 계산하여 출력  

 

 

import cv2
import numpy as np

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

s_imageX  =cv2.Scharr(g_image, cv2.CV_8U, 1, 0 )
s_imageY  =cv2.Scharr(g_image, cv2.CV_8U, 0, 1 )


cv2.imshow('Original Image', g_image)
cv2.imshow('Scharr X direction image', s_imageX)
cv2.imshow('Scharr Y direction image', s_imageY)

cv2.waitKey(0)

 

 

Canny 경계선 검출기 

회색조 영상에 대하여 Canny 경계선 검출기를 이용하여 경계선을 검출  

 

 

import cv2
import numpy as np

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

c_image1  =cv2.Canny(g_image, 10, 50)
c_image2  =cv2.Canny(g_image, 150, 300)


cv2.imshow('Original Image', g_image)
cv2.imshow('Canny image 1', c_image1)
cv2.imshow('Canny image 2', c_image2)

cv2.waitKey(0)

 

 

크기 변환 (Scaling) - 확대 및 축소 

512x256 크기의 컬러 영상에 대하여 128x64 크기로 축소한 후에 다양 한 보간 방법으로 확대하여 출력  p.361

 

import cv2

c_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)

cv2.imshow('Original image', c_image)

s_image = cv2.resize(c_image, (128, 64), interpolation=cv2.INTER_LINEAR)
cv2.imshow('128*64 image', s_image)

z_image1 = cv2.resize(s_image, (512, 256), interpolation=cv2.INTER_NEAREST)
z_image2 = cv2.resize(s_image, None, None, 4, 4, cv2.INTER_LINEAR)
z_image3 = cv2.resize(s_image, None, fx=4, fy=4, interpolation=cv2.INTER_CUBIC)
z_image4 = cv2.resize(s_image, None, fx=4, fy=4, interpolation=cv2.INTER_AREA)

cv2.imshow('Nearest neighbor intp image', z_image1)
cv2.imshow('Bilinear intp image', z_image2)
cv2.imshow('Cubic intp image', z_image3)
cv2.imshow('Area intp image', z_image4)
cv2.waitKey(0)

 

이동 변환 (Translation) 

512x256 크기 컬러 영상에 대하여 다양한 이동 변환을 수행  p.368

 

import numpy as np
import cv2

c_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)
cv2.imshow('Original image', c_image)
rows, cols = c_image.shape[:2] # channel a
Mat = np.float32([ [1, 0, 30],
                    [0, 1, 60] ])


t_image1 = cv2.warpAffine (c_image, Mat, (cols, rows))
t_image2 = cv2.warpAffine (c_image, Mat, (cols, rows), borderMode=cv2.BORDER_CONSTANT, borderValue=(255, 255, 255))
t_image3 = cv2.warpAffine (c_image, Mat, (cols, rows), borderMode=cv2.BORDER_REPLICATE)
t_image4 = cv2.warpAffine (c_image, Mat, (cols, rows), borderMode=cv2.BORDER_REFLECT)
t_image5 = cv2.warpAffine (c_image, Mat, (cols, rows), borderMode=cv2.BORDER_WRAP)

cv2.imshow('Original image', c_image)
cv2.imshow('Translation image-default', t_image1)
cv2.imshow('Translation image - BORDER_CONSTANT', t_image2)
cv2.imshow('Translation image - BORDER_REPLICATE', t_image3)
cv2.imshow('Translation image - BORDER_REFLECT', t_image4)
cv2.imshow('Translation image - BORDER_WRAP', t_image5)

cv2.waitKey(0)

 

 

회전 변환 (Rotation) 

512x256 크기 컬러 영상에 대하여 다양한 회전 변환을 수행하는 코드 slide 374  

 

import cv2
c_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)
cv2.imshow('Original image', c_image)

rows, cols = c_image.shape[:2] # channel 여부 무시

Mat1 = cv2.getRotationMatrix2D((0, 0), 45, 1.0)
Mat2 = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1.0)
Mat3 = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1.0)

r_image1 = cv2.warpAffine (c_image, Mat1, (cols, rows))
r_image2 = cv2.warpAffine (c_image, Mat2, (cols, rows), borderMode=cv2.BORDER_REPLICATE)
r_image3 = cv2.warpAffine (c_image, Mat2, (cols, rows), borderMode=cv2.BORDER_DEFAULT)
r_image4 = cv2.warpAffine (c_image, Mat3, (cols, rows))

cv2.imshow('Original image', c_image)
cv2.imshow('Rotation image (0, 0), 45', r_image1)
cv2.imshow('Rotation image (w/2, h/2), 45 replicate', r_image2)
cv2.imshow('Rotation image (w/2, h/2), 90', r_image4)

cv2.waitKey(0)

 

 

어파인(Affine) 변환 

다양한 어파인 변환을 수행하는 코드: 어파인 변환에 의한 영상 변환을 쉽게 확인하기 위하여 RGB 색상으로 각 변환 기준 점의 위치를 표시  

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)
c_image = cv2.cvtColor(g_image, cv2.COLOR_GRAY2BGR)
cv2.imshow('Original image', c_image)

rows, cols=c_image.shape[:2] # channel 여부 무시
# pts1 좌표 표시
pts1 = np.float32([[200, 200], [300, 200], [200, 300]])
cv2.circle(c_image, (200,200), 9, (255,0,0),-1)
cv2.circle(c_image, (300,200), 9, (0,255,0),-1)
cv2.circle(c_image, (200,300), 9, (0,0,255), -1)

pts2 = np.float32([[200, 200], [350, 200], [200, 250]])
Mat1 = cv2.getAffineTransform(pts1, pts2)
r_image1 = cv2.warpAffine (c_image, Mat1, (cols, rows))


pts2 = np.float32([[200,200], [300, 230], [260, 300]])
Mat2 = cv2.getAffineTransform (pts1, pts2)
r_image2 = cv2.warpAffine (c_image, Mat2, (cols, rows))

pts2= np.float32([[200, 200], [100, 170], [170,100]])
Mat3 = cv2.getAffineTransform(pts1, pts2)
r_image3 = cv2.warpAffine (c_image, Mat3, (cols, rows))

cv2.imshow('Original image', c_image)
cv2.imshow('Affine 1 image', r_image1)
cv2.imshow('Affine 2 image', r_image2)
cv2.imshow('Affine 3 image', r_image3)
cv2.waitKey(0)

 

 

원근(Perspective) 변환 

회색조 영상을 컬러 영상으로 변환한 후에 다양한 원근 변환을 수행  p.381

 

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)
c_image = cv2.cvtColor(g_image, cv2.COLOR_GRAY2BGR)
cv2.imshow('Original image', c_image)

rows, cols=c_image.shape[:2] # channel

#pts1 좌표 표시
pts1 = np.float32([[80, 280], [220, 220], [250, 480], [60,420]])
cv2.circle(c_image, (80,280), 9, (255,0,0),-1)
cv2.circle(c_image, (220,220), 9, (0,255,0),-1)
cv2.circle(c_image, (250,480), 9, (0,0,255), -1)
cv2.circle(c_image, (60,420), 9, (0,255,255), -1)
#평행선 표시
cv2.line(c_image, (0, 340), (511, 340), (255, 0, 0), 2)
cv2.line (c_image, (0, 380), (511, 380), (0, 0, 255), 2)


pts2 = np.float32([[10,10], [502,10], [502, 502], [10, 502]])
Mat1 = cv2.getPerspectiveTransform(pts1, pts2)

print('Perspective matrix')
print (Mat1)

r_image = cv2.warpPerspective (c_image, Mat1, (cols, rows))

cv2.imshow('Original image', c_image)
cv2.imshow('Perspective image', r_image)
cv2.waitKey(0)

 

 

대칭(flip) 또는 반사(reflection) 변환 

512x512 크기 컬러 영상을 다양한 대칭 변환을 수행하는 코드  

 

 

import numpy as np
import cv2

c_image = cv2.imread('figures100.jpg', cv2.IMREAD_UNCHANGED)

f_image0 = cv2.flip(c_image, 0)
f_image_p1 = cv2.flip(c_image, 1)
f_image_m1 = cv2.flip(c_image, -1)

cv2.imshow('Original image', c_image)
cv2.imshow('Flip image (up/down, 0)', f_image0)
cv2.imshow('Flip image (left/right, > 0)', f_image_p1)
cv2.imshow('Flip image (Y-=X, <0)', f_image_m1)

cv2.waitKey(0)

 

 

구조 요소 (Structure Element) 

OpenCV에서 지원하는 구조화 요소의 모양을 출력하는 코드  p.393

 

 

import cv2

print("Rectangle 3x3")
print(cv2.getStructuringElement (cv2.MORPH_RECT, (3, 3)))
print("Rectangle 5x5")
print(cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5)))
print("Cross 3x3")
print (cv2.getStructuringElement (cv2.MORPH_CROSS, (3, 3)))
print("Cross 5x5")
print(cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5)))
print("Ellipse 3x3")
print (cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (3, 3)))
print("Ellipse 5x5")
print(cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5)))

 

 

팽창 연산 (Dilation) 

이진 영상 : 512x512 크기 컬러 영상을 회색조 영상 변환 후 128 임계치 를 사용하여 이진화를 수행한 후에 팽창 연산을 수행하여 출력  p.400

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)
ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)
morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

di_image1 = cv2.dilate (b_image, morph_rect, iterations = 1)
di_image2 = cv2.dilate (b_image, morph_cros, iterations = 1)
di_image3 = cv2.dilate (b_image, morph_elip, iterations = 1)
di_image4 = cv2.dilate (b_image, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Dilation image rect iter 1', di_image1)
cv2.imshow('Dilation image cross iter 1', di_image2)
cv2.imshow('Dilation image ellipse iter 1', di_image3)
cv2.imshow('Dilation image rect iter 4', di_image4)

cv2.waitKey(0)

 

 

침식 연산 (Erosion) 

이진 영상 : 512x512 크기 컬러 영상을 회색조 영상 변환 후 128 임계치 를 사용하여 이진화를 수행한 후에 침식 연산을 수행하여 출력  p.411

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)
ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)

morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

er_image1 = cv2.erode (b_image, morph_rect, iterations = 1)
er_image2 = cv2.erode (b_image, morph_cros, iterations = 1)
er_image3 = cv2.erode (b_image, morph_elip, iterations = 1)
er_image4 = cv2.erode (b_image, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Erosion image rect iter 1', er_image1)
cv2.imshow('Erosion image cross iter 1', er_image2)
cv2.imshow('Erosion image - ellipse iter 1', er_image3)
cv2.imshow('Erosion image - rect iter 4', er_image4)

cv2.waitKey(0)

 

 

 

열림 연산 (Opening) 

이진영상: 512x512 크기 컬러 영상을 회색조 영상으로 읽고, 128 임계 치를 사용하여 이진화를 수행한 후에 열림 연산을 수행하여 출력  

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)
ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)

morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

mp_image1 = cv2.morphologyEx (g_image, cv2.MORPH_OPEN, morph_rect, iterations = 1)
mp_image2 = cv2.morphologyEx (g_image, cv2.MORPH_OPEN, morph_cros, iterations = 1)
mp_image3 = cv2.morphologyEx (g_image, cv2.MORPH_OPEN, morph_elip, iterations = 1)
mp_image4 = cv2.morphologyEx (g_image, cv2.MORPH_OPEN, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Erosion image rect iter 1', mp_image1)
cv2.imshow('Erosion image cross iter 1', mp_image2)
cv2.imshow('Erosion image - ellipse iter 1', mp_image3)
cv2.imshow('Erosion image - rect iter 4', mp_image4)

cv2.waitKey(0)

 

 

닫힘 연산 (Closing) 

이진영상: 512x512 크기 컬러 영상을 회색조 영상으로 읽고, 128 임계 치를 사용하여 이진화를 수행한 후에 닫힘 연산을 수행하여 출력  

 

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)

morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

mp_image1 = cv2.morphologyEx (g_image, cv2.MORPH_CLOSE, morph_rect, iterations = 1)
mp_image2 = cv2.morphologyEx (g_image, cv2.MORPH_CLOSE, morph_cros, iterations = 1)
mp_image3 = cv2.morphologyEx (g_image, cv2.MORPH_CLOSE, morph_elip, iterations = 1)
mp_image4 = cv2.morphologyEx (g_image, cv2.MORPH_CLOSE, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Erosion image rect iter 1', mp_image1)
cv2.imshow('Erosion image cross iter 1', mp_image2)
cv2.imshow('Erosion image - ellipse iter 1', mp_image3)
cv2.imshow('Erosion image - rect iter 4', mp_image4)

cv2.waitKey(0)

 

 

그래디언트 연산 (Gradient) 

이진영상: 512x512 컬러 영상을 회색조 영상으로 읽고, 128 임계치를 사용하여 이진화를 수행한 후에 그래디언트 연산을 수행하여 출력  

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)

morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

mp_image1 = cv2.morphologyEx (g_image, cv2.MORPH_GRADIENT, morph_rect, iterations = 1)
mp_image2 = cv2.morphologyEx (g_image, cv2.MORPH_GRADIENT, morph_cros, iterations = 1)
mp_image3 = cv2.morphologyEx (g_image, cv2.MORPH_GRADIENT, morph_elip, iterations = 1)
mp_image4 = cv2.morphologyEx (g_image, cv2.MORPH_GRADIENT, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Erosion image rect iter 1', mp_image1)
cv2.imshow('Erosion image cross iter 1', mp_image2)
cv2.imshow('Erosion image - ellipse iter 1', mp_image3)
cv2.imshow('Erosion image - rect iter 4', mp_image4)

cv2.waitKey(0)

 

 

 

탑햇 연산 (Top-Hat) 

이진영상: 512x512 컬러 영상을 회색조 영상으로 읽고, 128 임계치를 사용하여 이진화를 수행한 후에 탑햇 연산을 수행하여 출력  

 

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)

morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

mp_image1 = cv2.morphologyEx (g_image, cv2.MORPH_TOPHAT, morph_rect, iterations = 1)
mp_image2 = cv2.morphologyEx (g_image, cv2.MORPH_TOPHAT, morph_cros, iterations = 1)
mp_image3 = cv2.morphologyEx (g_image, cv2.MORPH_TOPHAT, morph_elip, iterations = 1)
mp_image4 = cv2.morphologyEx (g_image, cv2.MORPH_TOPHAT, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Erosion image rect iter 1', mp_image1)
cv2.imshow('Erosion image cross iter 1', mp_image2)
cv2.imshow('Erosion image - ellipse iter 1', mp_image3)
cv2.imshow('Erosion image - rect iter 4', mp_image4)

cv2.waitKey(0)

 

 

블랙햇 연산 (Black-Hat) 

이진영상: 512x512 컬러 영상을 회색조 영상으로 읽고, 128 임계치를 사용하여 이진화를 수행한 후에 블랙햇 연산을 수행하여 출력  

 

 

import numpy as np
import cv2

g_image = cv2.imread('figures100.jpg', cv2.IMREAD_GRAYSCALE)

ret, b_image = cv2.threshold (g_image, 128, 255, cv2.THRESH_BINARY)

morph_rect = cv2.getStructuringElement (cv2.MORPH_RECT, (5, 5))
morph_cros = cv2.getStructuringElement (cv2.MORPH_CROSS, (5, 5))
morph_elip = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))

mp_image1 = cv2.morphologyEx (g_image, cv2.MORPH_BLACKHAT, morph_rect, iterations = 1)
mp_image2 = cv2.morphologyEx (g_image, cv2.MORPH_BLACKHAT, morph_cros, iterations = 1)
mp_image3 = cv2.morphologyEx (g_image, cv2.MORPH_BLACKHAT, morph_elip, iterations = 1)
mp_image4 = cv2.morphologyEx (g_image, cv2.MORPH_BLACKHAT, morph_rect, iterations = 3)

cv2.imshow('Original image', g_image)
cv2.imshow('Binary image', b_image)
cv2.imshow('Blackhat image rect iter 1', mp_image1)
cv2.imshow('Blackhat image cross iter 1', mp_image2)
cv2.imshow('Blackhat image - ellipse iter 1', mp_image3)
cv2.imshow('Blackhat image - rect iter 4', mp_image4)

cv2.waitKey(0)

 

 

모두 입력했다. OpenCV 대단하다. 길고 즐거운 여정이었다.

기억해라. 검은 띠를 따는 게 목적은 아니다. 검은 띠의 상태이자 검은 띠가 갖고 있는 라이프 스타일을 갖는 것이다.

 

"블랙벨트는 그저 벨트, 천 조각에 불과합니다.
여러분의 궁극적인 목표는 블랙벨트를 '획득'하는 것이 되어서는 안 됩니다.
여러분의 궁극적인 목표는 블랙벨트가 되는 것이어야 합니다.
"블랙벨트가 되는 것"은 정신 상태이자 라이프스타일입니다. "
(보디 샌더스 편집, "무술의 지혜" 중에서) 

 

 

 

 

 

 

 

 

 

반응형

더욱 좋은 정보를 제공하겠습니다.~ ^^