0

I'm having a series of images with shipping labels on boxes and I need to extract the whole white area of the label. I'm extremely new to opencv and using these answers (detect rectangle in image and crop) i managed to put together the following code(it extracts only the top most part of the label):

import cv2
import numpy as np

#

path_to_image = 'IMG_0184b.jpg'

#
img = cv2.imread(path_to_image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 0, 17, 17)

kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(gray,kernel,iterations = 2)
kernel = np.ones((4,4),np.uint8)
dilation = cv2.dilate(erosion,kernel,iterations = 2)

edged = cv2.Canny(dilation, 30, 200)
cnt, h = cv2.findContours(edged,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

largestArea = []
for contour in cnt:
    largestArea.append(cv2.contourArea(contour))

print(sorted(largestArea, reverse=True)[0:3])
for contour in cnt:
    approx = cv2.approxPolyDP(contour, 0.01* cv2.arcLength(contour, True), True)
    area = cv2.contourArea(contour)
    if area == 612144.5:
        cv2.drawContours(img, [approx], 0, (0, 0, 0), 5)
        x = approx.ravel()[0]
        y = approx.ravel()[1] - 5
        #
        if len(approx) == 4 :
            x, y , w, h = cv2.boundingRect(approx)
            aspectRatio = float(w)/h
            #print(aspectRatio)
            if aspectRatio >= 0.95 and aspectRatio < 1.05:
                cv2.putText(img, "square", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

            else:
                cv2.putText(img, "rectangle", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

cv2.namedWindow('custom window', cv2.WINDOW_KEEPRATIO)
cv2.imshow('custom window', img)
cv2.resizeWindow('custom window', 800, 800)
cv2.waitKey(0)
cv2.destroyAllWindows()

How do I capture the whole white area of the label just like in the example below?

Original picture OriginalPicture

Desired result DesiredResult

Many thanks

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

0 Answers0