0

How do i fill the background outside the contour with white color ?

I can get a very good contour around my product(see image bellow). Now I need to figure out how to invert and fill everything around my contour with white.

enter image description here enter image description here

import cv2
import numpy as np


# load the images
empty = cv2.imread("empty.jpg")
full = cv2.imread("3.jpg")

# save color copy for visualization
full_c = full.copy()

# convert to grayscale
empty_g = cv2.cvtColor(empty, cv2.COLOR_BGR2GRAY)
full_g = cv2.cvtColor(full, cv2.COLOR_BGR2GRAY)

# blur to account for small camera movement
# you could try if maybe different values will maybe
# more reliable for broader cases
empty_g = cv2.GaussianBlur(empty_g, (41, 41), 0)
full_g = cv2.GaussianBlur(full_g, (41, 41), 0)

# get the difference between full and empty box
diff = full_g - empty_g
cv2.imwrite("diff.jpg", diff)

# inverse thresholding to change every pixel above 190
# to black (that means without the bag)
_, diff_th = cv2.threshold(diff, 240, 245, 1)
cv2.imwrite("diff_th.jpg", diff_th)

# combine the difference image and the inverse threshold
# will give us just the bag

bag = cv2.bitwise_and(diff, diff_th, None)
cv2.imwrite("just_the_bag.jpg", bag)

# threshold to get the mask instead of gray pixels
_, bag = cv2.threshold(bag, 100, 255, 0)

# dilate to account for the blurring in the beginning
kernel = np.ones((6, 6), np.uint8)
bag = cv2.dilate(bag, kernel, iterations=1)

# find contours, sort and draw the biggest one
contours, _ = cv2.findContours(bag, cv2.RETR_TREE,
                                  cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:3]
cv2.drawContours(full_c, [contours[0]], -1, (0, 255, 0), 2)

# show and save the result
cv2.imshow("bag", full_c)

cv2.imwrite("result2.jpg", full_c)
cv2.waitKey(0)

This is the contour i'm getting.

enter image description here

I can't figure out how to fill everything outside the contour with white.

This is my desired output :

enter image description here

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Andie31
  • 305
  • 3
  • 13
  • Does this answer your question? [Create a mask from outer contour to remove image background](https://stackoverflow.com/questions/60319634/create-a-mask-from-outer-contour-to-remove-image-background) – beaker Apr 25 '23 at 15:24
  • Specifically, the `# Apply mask` portion of [Rotem's answer](https://stackoverflow.com/a/60325881/1377097). Basically, you use a mask to copy the portion of the image inside the contour to another blank image. (You'll have to modify to make it white instead of black.) – beaker Apr 25 '23 at 15:25
  • @beaker so you say i just have to use this : ```res = np.zeros_like(orig_im) res[(mask > 0)] = orig_im[(mask > 0)]``` into my code ? – Andie31 Apr 25 '23 at 15:29
  • That will put your contoured image onto a black background. – beaker Apr 25 '23 at 15:31
  • @beaker how i can modify it to get a white background than ? – Andie31 Apr 25 '23 at 15:34
  • https://stackoverflow.com/questions/10465747/how-to-create-a-white-image-in-python – beaker Apr 25 '23 at 15:41
  • @beaker so ```img = np.zeros([100,100,3],dtype=np.uint8) img.fill(255) # or img[:] = 255``` and ```res = np.zeros_like(orig_im) res[(mask > 0)] = orig_im[(mask > 0)]``` together ? – Andie31 Apr 25 '23 at 15:45
  • Did you try it? Or are you concerned that typing in those commands might cause your computer to explode? – beaker Apr 25 '23 at 15:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253317/discussion-between-andie31-and-beaker). – Andie31 Apr 25 '23 at 15:51

0 Answers0