Reputation: 104
I have two binary images. The first is like this:
and the last one is like this:
They dont have the same sized curve. I want to add the second one's two white zones contained in the black zone to the first one's black zone.
My code runs like this,but this a wrong answer:
The question is like this,and I want get the the finally image which I draw in picture with the the final image:
How can I achieve this task?
Upvotes: 3
Views: 5447
Reputation: 114548
Assuming img1
is your first array (larger solid blob) and img2
is the second (smaller blob with holes), you need a method to identify and remove the outer region of the second image. The flood fill algorithm is a good candidate. It is implemented in opencv as cv2.floodFill
.
The easiest thing to do would be to fill the outer edge, then just add the results together:
mask = np.zeros((img2.shape[0] + 2, img2.shape[1] + 2), dtype=np.uint8)
cv2.floodFill(img2, mask, (0, 0), 0, 0)
result = img1 + img2
Here is a toy example that shows mini-images topologically equivalent to your originals:
img1 = np.full((9, 9), 255, dtype=np.uint8)
img1[1:-1, 1:-1] = 0
img2 = np.full((9, 9), 255, dtype=np.uint8)
img2[2:-2, 2:-2] = 0
img2[3, 3] = img2[5, 5] = 255
The images look like this:
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(img1)
ax2.imshow(img2)
After the flood fill, the images look like this:
Adding the resulting images together looks like this:
Keep in mind that floodFill
operates in-place, so you may want to make a copy of img2
before going down this road.
Upvotes: 5
Reputation: 208013
I think you want this:
#!/usr/local/bin/python3
from PIL import Image,ImageDraw, ImageColor, ImageChops
# Load images
im1 = Image.open('im1.jpg')
im2 = Image.open('im2.jpg')
# Flood fill white edges of image 2 with black
seed = (0, 0)
black = ImageColor.getrgb("black")
ImageDraw.floodfill(im2, seed, black, thresh=127)
# Now select lighter pixel of image1 and image2 at each pixel location and save it
result = ImageChops.lighter(im1, im2)
result.save('result.png')
If you prefer OpenCV, it might look like this:
#!/usr/local/bin/python3
import cv2
# Load images
im1 = cv2.imread('im1.jpg', cv2.IMREAD_GRAYSCALE)
im2 = cv2.imread('im2.jpg', cv2.IMREAD_GRAYSCALE)
# Threshold, because JPEG is dodgy!
ret, im1 = cv2.threshold(im1, 127, 255, cv2.THRESH_BINARY)
ret, im2 = cv2.threshold(im2, 127, 255, cv2.THRESH_BINARY)
# Flood fill white edges of image 2 with black
h, w = im2.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(im2, mask, (0,0), 0)
# Now select lighter of image1 and image2 and save it
result = np.maximum(im1, im2)
cv2.imwrite('result.png', result)
Upvotes: 4