Home > database >  Combining 2 tresholded images to get this effect
Combining 2 tresholded images to get this effect

Time:12-20

I have the following 2 images: enter image description here

How could I combine the images to get any of these 2 images? enter image description here

My code:

import cv2
import numpy as np

image = cv2.imread('skadi.png')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
cv2.imshow('greyscale',gray)
_, binary = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)
cv2.imshow('treshold',binary)
binary= 255 - binary
cv2.imshow('inverted',binary)
kernel = np.ones((25, 25), np.uint8)
closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing',closing)
#closing = 255-closing
closing2 =  cv2.bitwise_not(closing)
cv2.imshow('invetedclosing',closing2)
result = cv2.bitwise_or(closing, closing2)
cv2.imshow('convned',result)
edges = cv2.Canny(result, 100, 200)

cv2.waitKey(0)
cv2.destroyAllWindows()

I tried combining them wit cv2.bitwise_or and cv2.bitwise_xor, but ended with a white screen.

Any help appreciated!

CodePudding user response:

Here's a handy script that basically extracts the biggest white blob in a binary image. Since the biggest white blob in your image is blob is the foreground (and the shape you are looking for), this should give you the expected result.

It basically gets all the external contours and keeps the contour with the biggest area. It then draws it on a new image.

This is the code, I'm using enter image description here

  • Related