如何更改图像中某个区域的灰度值? [英] How to change the grey scale value of a region in an image?

查看:542
本文介绍了如何更改图像中某个区域的灰度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,不确定如何解决此问题.

I am new to Python and not really sure how to attack this problem.

我想做的是拍摄黑白图像,并将边缘(x像素厚)的值从255更改为其他灰度值.

What I am trying to do is to take a black and white image and change the value of the edge (x pixels thick) from 255 to some other greyscale value.

我需要对文件夹内的一组png图像执行此操作.所有图像都是几何形状(主要是直线的组合),没有疯狂的曲线或图案.使用Python 3.

I need to do this to a set of png images inside of a folder. All images will be geometric (mostly a combination of straight lines) no crazy curves or patterns. Using Python 3.

请检查图像.

典型文件如下所示: https://drive.google.com/open?id=13ls1pikNsO7ZbsHatC6cp4O6Fj0MPOZ

A typical file will look like this: https://drive.google.com/open?id=13ls1pikNsO7ZbsHatC6cOr4O6Fj0MPOZ

推荐答案

我认为这就是您想要的.这些评论应该很好地解释了我的情况:

I think this is what you want. The comments should explain pretty well what I going on:

#!/usr/bin/env python3

import numpy as np
from PIL import Image, ImageFilter
from skimage.morphology import dilation, square

# Open input image and ensure it is greyscale
image = Image.open('XYbase.png').convert('L')

# Find the edges
edges = image.filter(ImageFilter.FIND_EDGES)

# Convert edges to Numpy array and dilate (fatten) with our square structuring element
selem = square(6)
fatedges = dilation(np.array(edges),selem)

# Make Numpy version of our original image and set all fatedges to brightness 128
imnp = np.array(image)
imnp[np.nonzero(fatedges)] = 128

# Convert Numpy image back to PIL image and save
Image.fromarray(imnp).save('result.png')

所以,如果我从这张图片开始:

So, if I start with this image:

(中间)边看起来像这样:

The (intermediate) edges look like this:

结果如下:

如果要使轮廓更粗/更细,请增大/减小以下位置的6:

If you want the outlines fatter/thinner, increase/decrease the 6 in:

selem = square(6)

如果要使轮廓更亮/更暗,请在以下位置增大/减小128:

If you want the outlines lighter/darker, increase/decrease the 128 in:

imnp[np.nonzero(fatedges)] = 128


关键字:图像,图像处理,加肥,加粗,轮廓,迹线,边缘,高光,块状,PIL,枕头,边缘,边缘,形态,结构元素,skimage,scikit图像,腐蚀,腐蚀,膨胀,膨胀.


Keywords: image, image processing, fatten, thicken, outline, trace, edge, highlight, Numpy, PIL, Pillow, edge, edges, morphology, structuring element, skimage, scikit-image, erode, erosion, dilate, dilation.

这篇关于如何更改图像中某个区域的灰度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆