如何更改图像边缘(轮廓)的厚度?

How to change the thickness of the edge (contour) of an Image?

我正在尝试提取图像的边缘(轮廓)并更改其厚度。我想给它类似 Photoshop 图层样式的描边效果。 Photoshop描边效果示例: http://projectwoman.com/2012/11/smart-objects-and-strokes-in-photoshop.html

我能够从图像中提取边缘。使用 canny edgepillow 函数。

1.using canny边缘检测

img = cv2.imread(img_path,0)
edges = cv2.Canny(img,300,700)

2.using 枕头填充物

image = Image.open(img_path).convert('RGB')
image = image.filter(ImageFilter.FIND_EDGES())

但是,我无法调整轮廓粗细。

这里有一个解决方案:

import cv2
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('mickey.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2YCR_CB)[...,0]

def show_img(im, figsize=None, ax=None, alpha=None):
    if not ax: fig,ax = plt.subplots(figsize=figsize)
    ax.imshow(im, alpha=alpha)
    ax.set_axis_off()
    return ax

def getBordered(image, width):
    bg = np.zeros(image.shape)
    _, contours, _ = cv2.findContours(image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    biggest = 0
    bigcontour = None
    for contour in contours:
        area = cv2.contourArea(contour) 
        if area > biggest:
            biggest = area
            bigcontour = contour
    return cv2.drawContours(bg, [bigcontour], 0, (255, 255, 255), width).astype(bool) 

im2 = getBordered(image, 10)

show_img(im2, figsize=(10,10))

您可以通过更改 getBordered 中的参数宽度来更改厚度。