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

时间:2018-07-26 14:49:17

标签: python image opencv edge-detection

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

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

1。使用Canny边缘检测

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

2。使用枕头填充物

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

但是,我无法调整轮廓厚度。

1 个答案:

答案 0 :(得分:0)

这里是一个解决方案:

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))

enter image description here

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