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

2024-10-01 15:45:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图提取图像的边缘(它的轮廓)并改变它的厚度。我想给它像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.使用枕头填充物

^{pr2}$

但是,我不能调整轮廓厚度。在


Tags: 图像图层http示例img风格cv2边缘
1条回答
网友
1楼 · 发布于 2024-10-01 15:45:17

这里有一个解决方案:

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中更改参数宽度来更改厚度。在

相关问题 更多 >

    热门问题