Head vs breakz
[code] - OpenCV GrabCut 본문
import numpy as np
import cv2
from matplotlib import pyplot as plt
for i in range(1,1001):
img = cv2.imread('./image/'+str(i)+'.jpg')
#이미지 크기조절
img = cv2.resize(img,dsize=(350,200),interpolation=cv2.INTER_LINEAR)
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
height,width,c= img.shape
#이미지 내 위치 확인 후 rect 조절
a = int(width*0.06)
b = int(height*0.08)
c = int(width*0.86)
d = int(height*0.7)
rect = (a,b,c,d)
# rect = (box[0][0], box[0][1], box[0][2]-box[0][0], box[0][3]-box[0][1])
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
# plt.imshow(img),plt.colorbar(),plt.show()
cv2.imwrite('./image_test/'+str(i)+'.jpg',img)
현재 알약 사진 1000장의 배경부분을 없애기 위한 작업을 진행 중이다. 위의 코드를 실행 시킬 경우
위와 같이 이미지가 resize를 통해 축소되고, 배경 부분이 사라진 상태가 된다.
'Head > Code' 카테고리의 다른 글
[code] - 이미지에서 글씨 찾기 구글 API / windows에서 (0) | 2020.04.20 |
---|---|
[code] - 이미지에서 색상 투명화 하기 (0) | 2020.04.16 |
[code] - 백준 수학 1193,2869,10250,2775,1712,2839,2292 python (0) | 2020.04.08 |
[code] - 백준 문자열 11654,11720,10809,2675,1157,1152,2908,5622,2941,1316번 python (0) | 2020.04.04 |
[code] - 백준 함수 15596,4673,1065번 python (0) | 2020.04.03 |
Comments