๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (6)] ์ด๋ฏธ์ง ๋ณํ (๊ธฐํ, ํฌ๊ธฐ ๋ณํ) & ๋ณด๊ฐ๋ฒ ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/Image Processing
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (6)] ์ด๋ฏธ์ง ๋ณํ (๊ธฐํ, ํฌ๊ธฐ ๋ณํ) & ๋ณด๊ฐ๋ฒ
์ง์ง์ํ์นด 2023. 12. 28. 11:59728x90
๋ฐ์ํ
๐ channle ๋ณ ํ๋ ฌ ๋ฐ์ดํฐ ๊ตฌ์กฐ
๐ ์ ๋ ฅ ์์์ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ณต ์คํ
def scale_nogood(img, scale_x=1, scale_y=1):
height, width = img.shape
img_ = np.zeros((int(height*scale_y), int(width*scale_x)),
dtype=np.uint8)
for y in range(height):
for x in range(width):
try:
img_[int(y*scale_y), int(x*scale_x)] = img[y, x]
except:
pass
return img_
import cv2
import numpy as np
img = cv2.imread("images/lena.jpg", cv2.IMREAD_GRAYSCALE)
result = scale_nogood(img, 0.5, 0.5)
cv2.imshow('Origin', img)
cv2.imshow('Scale NG', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread("images/lena.jpg", cv2.IMREAD_GRAYSCALE)
result = scale_nogood(img, 1.5, 1.0)
cv2.imshow('Origin', img)
cv2.imshow('Scale NG', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
ํ๋๋ ํ์ง๋ง ์ ๋ณด๋ ์์
์ ๋ณด๋ฅผ ์ง์ด๋ฃ๋ ๋ฐฉ๋ฒ๋ ์ฐพ์์ผ๋จ!
๐ ์ถ๋ ฅ ์์์ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ณต ์คํ
import numpy as np
def scale_nogood2(img, scale_x=1, scale_y=1):
height, width = img.shape
height_n, width_n = int(height*scale_y), int(width*scale_x)
img_ = np.zeros((height_n, width_n), dtype=np.uint8)
for y in range(height_n):
for x in range(width_n):
img_[y, x] = img[int(y/scale_y), int(x/scale_x)]
return img_
import cv2
img = cv2.imread("images/lena.jpg", cv2.IMREAD_GRAYSCALE)
result = scale_nogood2(img, 1.5, 1.0)
cv2.imshow('Origin', img)
cv2.imshow('Scale NG', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread("images/lena.jpg", cv2.IMREAD_GRAYSCALE)
result = scale_nogood2(img, 0.2, 0.2)
cv2.imshow('Origin', img)
cv2.imshow('Scale NG', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
๐ ๋ณด๊ฐ๋ฒ
ํฌ๊ธฐ ๋ณํ ์ ํ์๊ฐ ๋งคํ๋์ง ์๋ ๊ฐ์ ์ง์ ํ๋ค
๐ฉ nearest
์ฃผ๋ณ ๊ฐ์ผ๋ก ์ฑ์ฐ๊ธฐ
import numpy as np
def scale_nearest(img, scale_x=1, scale_y=1):
height, width = img.shape
img_ = np.zeros((int(height*scale_y),int(width*scale_x)),
dtype=np.uint8)
for y in range(int(height*scale_y)):
for x in range(int(width*scale_x)):
try :
img_[y,x] = img[round(y/scale_y), round(x/scale_x)]
except:
pass
return img_
import cv2
lena_roi = cv2.imread("images/lena_roi.jpg", cv2.IMREAD_GRAYSCALE)
cv2.imshow('Lena ROI', lena_roi)
cv2.imshow('Scale Nearest', scale_nearest(lena_roi, 3, 3))
cv2.waitKey(0)
cv2.destroyAllWindows()
๐ฉ bilinear
import numpy as np
def scale_bilinear(img, scale_x=1, scale_y=1):
height, width = img.shape
img_ = np.zeros((int(height*scale_y), int(width*scale_x)), dtype=np.uint8)
for y in range(int(height*scale_y)):
for x in range(int(width*scale_x)):
q = x/scale_x-int(x/scale_x)
p = y/scale_y-int(y/scale_y)
try:
X = int(x/scale_x)
Y = int(y/scale_y)
value = (1-p)*(1-q)*img[Y, X] + p*(1-q)*img[Y+1, X] \
+ (1-p)*q*img[Y, X+1] + p*q*img[Y+1, X+1]
if value > 255:
img_[y, x] = 255
else:
img_[y, x] = int(value)
except:
pass
return img_
import cv2
lena_roi = cv2.imread("images/lena_roi.jpg", cv2.IMREAD_GRAYSCALE)
cv2.imshow('Lena ROI', lena_roi)
cv2.imshow('Scale bilinear', scale_bilinear(lena_roi, 3, 3))
cv2.waitKey(0)
cv2.destroyAllWindows()
๐ฉ maxpool2d
์ต๋๊ฐ๋ง ๋ฝ์๋ด๊ธฐ
def maxpool2d(img):
height, width = img.shape
img_ = np.zeros((int(height/2),int(width/2)),
dtype=np.uint8)
for y in range(int(height/2)):
for x in range(int(width/2)):
try :
img_[y,x] = np.max(img[2*y:2*y+2, 2*x:2*x+2])
except:
pass
return img_
import cv2
img = cv2.imread("images/lena.jpg", cv2.IMREAD_GRAYSCALE)
cv2.imshow('Nearest 0.5', scale_nearest(img, 0.5, 0.5))
cv2.imshow('Max Pooling', maxpool2d(img))
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread("images/lena_edge.png", cv2.IMREAD_GRAYSCALE)
cv2.imshow('Nearest 0.5', scale_nearest(img, 0.5, 0.5))
cv2.imshow('Max Pooling', maxpool2d(img))
cv2.waitKey(0)
cv2.destroyAllWindows()
maxpool ๋ก ๊ฒฝ๊ณ์ ์ฐพ๊ธฐ
728x90
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > Image Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++ ๋ก OpenCV (1)] OpenCV ์ค์นํ๊ธฐ (0) | 2023.12.28 |
---|---|
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (7)] ๋ฌธ์ ์ค์บํ๊ธฐ (0) | 2023.12.28 |
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (5)] ํ์คํ ๊ทธ๋จ (0) | 2023.12.28 |
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (4)] ๋๋๋ง(Dithering): ์ ํ๋ ์์ ์ด์ฉํ์ฌ ์์์ด๋ ์์ ๋ํ๋ด๋ ๊ฒ (0) | 2023.12.28 |
[Python ์ผ๋ก ์์์ฒ๋ฆฌ (3)] cv2.threshold() (0) | 2023.12.28 |
Comments