Head vs breakz

[code] - OpenCV GrabCut 본문

Head/Code

[code] - OpenCV GrabCut

headbreakz 2020. 4. 16. 15:48
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를 통해 축소되고, 배경 부분이 사라진 상태가 된다. 

Comments