简体   繁体   中英

Mask and smooth an image using OpenCV

The following code, as explained here , effectively finds the contours of an object in an image, and fills those contours with white, while setting the background to black.

import cv2
import numpy as np
# Read image
im_in = cv2.imread('bee-02.png', cv2.IMREAD_GRAYSCALE)
th, im_th = cv2.threshold(im_in, 250, 255, cv2.THRESH_BINARY_INV)
# Copy the thresholded image.
im_floodfill = im_th.copy()
# Mask used to flood filling.
h, w = im_th.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0,0), 255)
# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
# Combine the two images to get the foreground.
im_out = im_th | im_floodfill_inv
# Display images.
cv2.imshow("Foreground", im_out)
cv2.waitKey(0)

Here's an example of what it does:

Image of a bee =>

在此处输入图片说明

What's a good way to augment the above code such that the exterior contours of the image are smoothed? If possible, I'd like to use a method that makes it possible to specify the gradation of the smoothing. I guess that one way to do it would be to apply heavy blurring before applying the masking function, but presumably, there's a better way.

I would look into morphological opening and closing operations. Specifically I would try a morphological close with a nice big disc operator and you'll probably get something close to what you want.

They will operate directly on the binary data you've got (much less expensive than blurring) and will likely achieve the effect you're looking for in terms of simplifying or "blurring" the visual outline.

在阈值化之前,您还可以使用大内核(例如7x7或15x15)使高斯模糊。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM