๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (6)] ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ (๊ธฐํ•˜, ํฌ๊ธฐ ๋ณ€ํ™˜) & ๋ณด๊ฐ„๋ฒ• ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Image Processing

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (6)] ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ (๊ธฐํ•˜, ํฌ๊ธฐ ๋ณ€ํ™˜) & ๋ณด๊ฐ„๋ฒ•

์ง•์ง•์•ŒํŒŒ์นด 2023. 12. 28. 11:59
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’— 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
๋ฐ˜์‘ํ˜•
Comments