pythontensorflowopencvimage-processingcrop

How do I crop an image based on custom mask


I start my model and get a mask prediction of the area I would like to crop.

img = cv2.imread('picture.jpg')
img = cv2.resize(img, (224, 224))
print(img.shape)
T = np.zeros((1, 224, 224, 3), dtype='float32')
T[0]=img
prediction = model.predict(T , verbose=1)
prediction = prediction[0, :, : , :]
plt.imshow(img)
plt.show()
print(prediction.shape)
plt.imshow(np.squeeze(prediction).astype(np.float32))
plt.show()

enter image description here

This code is the closest I have gotten to the desired image, the problem with this image that is provided is that it doesn't crop out black background

result = prediction * img
plt.imshow((result/255).astype(np.float32))
plt.show()

enter image description here

My desired image should be like this enter image description here


Solution

  • You could try this:

    prediction = np.squeeze(prediction).astype(np.float32)
    threshold = 0.5
    xmin,ymin,xmax,ymax = (-1,-1,-1,-1)
    for j in range(224):
        for i in range(224):
            if prediction[j,i] <= threshold: continue
            xmin = i if xmin == -1 or xmin > i else xmin
            xmax = i if xmax == -1 or xmax < i else xmax
            ymin = j if ymin == -1 or ymin > j else ymin
            ymax = j if ymax == -1 or ymax < j else ymax
    cropImg = img[ymin:ymax,xmin:xmax]