PYTHON Image Processing import cv 2 import matplotlib
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) plt. imshow(img, cmap='gray') plt. show()
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) h, w = img. shape hist = [0 for i in range(256)] for pixel in np. reshape(img, (h*w, -1)): hist[pixel[0]] += 1 plt. hist(np. array(hist), np. arange(256)) plt. xlabel('brightness', fontsize = 12) plt. xticks(fontsize = 12) plt. yticks(fontsize = 12) plt. show() Image Histogram
PYTHON | Image Processing Threshold = 200
PYTHON | Image Processing Threshold = 200 0 255
PYTHON | Image Processing import cv 2 matplotlib. pyplot as plt numpy as np copy def thresholding(img: np. ndarray, thres: int, dst: np. ndarray = None): tmp = copy. deepcopy(img) ? Practice#1 image thresholding을 진행하는 함수 완성하기 if dst is None: return tmp else: np. copyto(dst, tmp) img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) thresholding(img, 0, img) plt. imshow(img, cmap='gray') plt. show() 어떤 방법을 사용하던 상관 없음
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np cv 2. threshold(이미지, 임계값, 최대값, . . . ) img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) cv 2. threshold(img, 0, maxval=255, type=cv 2. THRESH_BINARY, dst=img) plt. imshow(img, cmap='gray') plt. show() cv 2. THRESH_BINARY_INV cv 2. THRESH_TRUNC cv 2. THRESH_TOZERO_INV
PYTHON | Image Processing Dilation Erosion 팽창 침식
5 PYTHON | Image Processing 3 Practice#2 import cv 2 import matplotlib. pyplot as plt import numpy as np 1 1 diamond kernel을 만드는 함수 완성하기 (size는 홀수만 가능) 0 1 1 1 0 0 0 1 1 1 1 1 0 def diamond_kernel(odd_size: int): tmp = [] # 생략가능 import numpy as np ? def diamond_kernel(odd_size: int): ? return np. array(tmp, dtype='uint 8') img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) cv 2. threshold(img, 128, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = diamond_kernel(odd_size=3) cv 2. erode(img, kernel=kernel, iterations=1, dst=img) return np. array(tmp, dtype='uint 8’) kernel = diamond_kernel(odd_size=3) print(kernel) ----------출력결과---------- plt. imshow(img, cmap='gray') plt. show() [[0 1 0] [1 1 1] [0 1 0]] 0 0 1 0 0
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) cv 2. threshold(img, 0, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) cv 2. erode(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show() cv 2. erode(이미지, 커널, 반복횟수, 변경대 상) OPENCV 침식 연산
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) cv 2. threshold(img, 0, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) cv 2. dilate(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show() cv 2. dilate(이미지, 커널, 반복횟수, 변경 대상) OPENCV 팽창 연산
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) cv 2. threshold(img, 128, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) cv 2. erode(img, kernel=kernel, iterations=1, dst=img) cv 2. dilate(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show() Opening Operation 모폴로지 열기 연산
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) cv 2. threshold(img, 128, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) cv 2. dilate(img, kernel=kernel, iterations=1, dst=img) cv 2. erode(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show() Closing Operation 모폴로지 닫기 연산
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) # cv 2. threshold(img, 128, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) cv 2. erode(img, kernel=kernel, iterations=1, dst=img) # cv 2. dilate(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show()
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) # cv 2. threshold(img, 128, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) # cv 2. erode(img, kernel=kernel, iterations=1, dst=img) cv 2. dilate(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show()
PYTHON | Image Processing import cv 2 import matplotlib. pyplot as plt import numpy as np img = cv 2. imread('. /finger. bmp', cv 2. IMREAD_GRAYSCALE) # cv 2. threshold(img, 128, maxval=255, type=cv 2. THRESH_BINARY, dst=img) kernel = np. ones((3, 3), np. uint 8) cv 2. erode(img, kernel=kernel, iterations=1, dst=img) cv 2. dilate(img, kernel=kernel, iterations=1, dst=img) plt. imshow(img, cmap='gray') plt. show()
PYTHON | Image Processing
PYTHON | Image Processing
PYTHON | Image Processing
PYTHON | Image Processing import cv 2 import numpy as np import matplotlib. pyplot as plt def median_filter(img: np. ndarray, size: int): pad_img = np. pad(img, size, mode='reflect') tmp = np. zeros(img. shape, dtype='uint 8') for y, line in enumerate(tmp): for x, pixel in enumerate(tmp): np. copyto(pixel, np. sort(pad_img[x-size: x+size, y-size: y+size], axis=None)[size**2+(size-1)**2 -1]) return tmp img = cv 2. imread('. /flower. bmp', cv 2. IMREAD_GRAYSCALE) plt. imshow(median_filter(img, )) ?
PYTHON | Image Processing import cv 2 import numpy as np import matplotlib. pyplot as plt def average_filter(img: np. ndarray, size: int): pad_img = np. pad(img, size, mode='reflect') tmp = np. zeros(img. shape, dtype='uint 8') Practice#3 중간값을 활용하는 median filter와 달리, 평균값을 활용해 noise를 제거하는 average filter 구현하기 for y, line in enumerate(tmp): for x, pixel in enumerate(tmp): np. average(넘파이배열) 함수 활용 ? np. copyto(pixel, return tmp img = cv 2. imread('. /flower. bmp', cv 2. IMREAD_GRAYSCALE) plt. imshow(average_filter(img, 1)) ? )
- Slides: 20