How to dynamically crop the pics from the thresholded images using Opencv?

32 views Asked by At

1

So the I have Thresholded the Image using OpenCV Hers a piece of code

X = [] # Image data
y = [] # Labels

# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
  img = cv.imread(path) # Reads image and returns np.array
  img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # Converts into the corret colorspace (GRAY)
  img_thresh = cv.threshold(img, 50, 225, cv.THRESH_BINARY)[1]  # Threshholds the grayscale image and the pixels below 50 are considered as black and above it are white
  img_thresh = cv.bitwise_not(img_thresh) 
  X.append(img_thresh)
  
  # Processing label in image path
  category = path.split("\\")[4]
  label = int(category.split("_")[0][1]) # We need to convert 10_down to 00_down, or else it crashes
  y.append(label)

# Turn X and y into np.array to speed up train_test_split
X = np.array(X, dtype="uint8")
X = X.reshape(len(imagepaths), 240, 640, 1) # Needed to reshape so CNN knows it's different images
y = np.array(y)

print("Images loaded: ", len(X))
print("Labels loaded: ", len(y))

print(y[0], imagepaths[0]) # Debugging

Can anyone please help me in dynamically croping the pic such that I crop the pics on the tips of the thresholded pics

0

There are 0 answers