Goal:
I am training a tensorflow model to be able to outline what a jacket/shirt is. In order to create this model, I must create a dataset of masks from pictures of shirts. unfortunately, I was unable to find any datasets that did this for me or any pre existing models that could handle this task.
Because of this, I am trying to use OpenCV to create my own dataset. The problem is, I can't get consistent results with different images.
Here is the code that I grabbed from the OpenCV website on making contours:
import numpy as np
from matplotlib import pyplot as plt
import cv2
im = cv2.imread('./jackets/jacket.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Create an output of all zeroes that has the same shape as the input
# image
out = np.zeros_like(im)
# On this output, draw all of the contours that we have detected
# in white, and set the thickness to be 3 pixels
cv2.drawContours(out, contours, 1, 255, cv2.FILLED)
# Spawn new windows that shows us the donut
# (in grayscale) and the detected contour
cv2.imshow('Original', im)
cv2.imshow('Generated Mask', out)
# Wait indefinitely until you push a key. Once you do, close the windows
cv2.waitKey(0)
cv2.destroyAllWindows()
This code works great for some pictures, giving me exactly what I want. Here is an example of OpenCV doing what I want:
Unfortunately, it does not work for all my images. Here are a few examples where it fails spectacularly.
Example 1:
Example 2:
Anyone have any suggestion on fixing this?