I am trying to find no. of boxes available in the warehouse.
I am using:
Here is my code for the same.
# Import libraries import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('box.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(gray, cmap='gray') blur = cv2.GaussianBlur(gray, (11,11), 0) plt.imshow(blur, cmap='gray') canny = cv2.Canny(blur, 20, 50, 3) plt.imshow(canny, cmap='gray')
# Here I am facing difficulty to draw exact lines for boxes as we can see small contours are also created in corner as well as other parts of box image. dilated = cv2.dilate(canny, (1, 1), iterations=0) plt.imshow(dilated, cmap='gray') (cnt, hierarchy) = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.drawContours(rgb, cnt, -1, (0, 255, 0), 2) plt.imshow(rgb)
print("Boxes in the image : ", len(cnt)) #Boxes in the image : 1169(--Error in counting boxes)
So my question is how to remove small contours or remove noise to calculate exact no. of boxes?