๋ฐ์ํ
๐ก ๋ณธ ๋ฌธ์๋ 'Python ์ด๋ฏธ์ง ์ฌ๋ฐฑ ์ง์ฐ๊ธฐ (numpy, mask, ...)'์ ๋ํด ์ ๋ฆฌํด๋์ ๊ธ์ ๋๋ค.
์ด๋ฏธ์ง ์์ฒด์ ์ฌ๋ฐฑ์ ์์ ๊ฑฐ๋ ์ด๋ฏธ์ง๋ฅผ ๋ณํฉํ๋ ๊ณผ์ ์์ ์๊ธด ์ฌ๋ฐฑ์ ์ง์ฐ๊ณ ์ถ์ ๊ฒฝ์ฐ, ๋ค์์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ฒ๋ฆฌ๋ฅผ ์งํํ์๋ฉด ๋๋ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค.
๊ฒ์์ ์ฌ๋ฐฑ ์ ๊ฑฐํ๊ธฐ
def crop_image_only_outside(img,tol=0):
# img is 2D image data
# tol is tolerance
mask = img > tol
if img.ndim==3:
mask = mask.all(2)
m,n = mask.shape
mask0,mask1 = mask.any(0),mask.any(1)
col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()
row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()
return img[row_start:row_end,col_start:col_end]
ํฐ์ ์ฌ๋ฐฑ ์ ๊ฑฐํ๊ธฐ
def crop_image_only_outside(img,tol=255):
# img is 2D image data
# tol is tolerance
mask = img < tol
if img.ndim==3:
mask = mask.all(2)
m,n = mask.shape
mask0,mask1 = mask.any(0),mask.any(1)
col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()
row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()
return img[row_start:row_end,col_start:col_end]
์ฐธ๊ณ
- [StackOverFlow] Removing white space around a saved image: https://stackoverflow.com/questions/11837979/removing-white-space-around-a-saved-image
- [StackExchange] Crop black border of image using NumPy: https://codereview.stackexchange.com/questions/132914/crop-black-border-of-image-using-numpy
๋ฐ์ํ