数字图像处理100问—18 Emboss 滤波器

提示:内容整理自:https://github.com/gzr2017/ImageProcessing100Wen
CV小白从0开始学数字图像处理

18 Emboss 滤波器

Emboss 滤波器可以使物体轮廓更加清晰,按照以下式子定义:

      -2 -1  0
K = [ -1  1  1 ]
       0  1  2

代码如下:

1.引入库

CV2计算机视觉库

import cv2
import numpy as np

2.读入数据

img = cv2.imread("imori.jpg").astype(np.float)
H, W, C = img.shape

b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

3.灰度化

gray = 0.2126 * r + 0.7152 * g + 0.0722 * b
gray = gray.astype(np.uint8)

4.Sobel 滤波器

K_size = 3

5.边缘补0

pad = K_size // 2
out = np.zeros((H + pad*2, W + pad*2), dtype=np.float)
out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float)
tmp = out.copy()

6 Emboss vertical

K = [[-2., -1., 0.],[-1., 1., 1.], [0., 1., 2.]]

7.处理

for y in range(H):
    for x in range(W):
        out[pad+y, pad+x] = np.sum(K * (tmp[y:y+K_size, x:x+K_size]))

out[out < 0] = 0
out[out > 255] = 255

out = out[pad:pad+H, pad:pad+W].astype(np.uint8)

8.保存结果

cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()


9.Emboss 滤波器处理后结果

在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题描述: 在图像处理中,有许多常见的操作,如图像缩放、旋转、翻转、裁剪、滤波等。本题要求编写一个Python程序,实现对图像文件的缩放、旋转、翻转、裁剪、滤波等基本操作。 数据建立: 程序需要读取一张图片文件,并对图片进行多种基本操作。用户需要指定操作类型和操作参数。 方法分析: 程序需要使用Python的PIL库进行图像处理。通过PIL库,可以读取图片文件,并对图片进行缩放、旋转、翻转、裁剪、滤波等基本操作。程序主要包括以下步骤: 1. 导入PIL库,读取图片文件。 2. 获取用户指定的操作类型和操作参数。 3. 根据用户指定的操作类型和操作参数,对图片进行相应的操作。 4. 保存处理后的图片。 主要程序: ```python from PIL import Image, ImageFilter # 读取图片文件 img = Image.open('image.jpg') # 获取用户指定的操作类型和操作参数 operation = input('请输入要进行的操作类型(scale/rotate/flip/crop/filter):') if operation == 'scale': width = int(input('请输入缩放后的图片宽度:')) height = int(input('请输入缩放后的图片高度:')) elif operation == 'rotate': angle = int(input('请输入旋转的角度:')) elif operation == 'flip': flip_type = input('请输入翻转类型(left/right/top/bottom):') elif operation == 'crop': left = int(input('请输入裁剪区域左上角的横坐标:')) top = int(input('请输入裁剪区域左上角的纵坐标:')) right = int(input('请输入裁剪区域右下角的横坐标:')) bottom = int(input('请输入裁剪区域右下角的纵坐标:')) elif operation == 'filter': filter_type = input('请输入滤波类型(BLUR/CONTOUR/DETAIL/EDGE_ENHANCE/EDGE_ENHANCE_MORE/EMBOSS/FIND_EDGES/SHARPEN/SMOOTH/SMOOTH_MORE):') # 根据用户指定的操作类型和操作参数,对图片进行相应的操作 if operation == 'scale': img = img.resize((width, height), Image.ANTIALIAS) elif operation == 'rotate': img = img.rotate(angle) elif operation == 'flip': if flip_type == 'left': img = img.transpose(Image.FLIP_LEFT_RIGHT) elif flip_type == 'right': img = img.transpose(Image.FLIP_TOP_BOTTOM) elif flip_type == 'top': img = img.transpose(Image.FLIP_TOP_BOTTOM) elif flip_type == 'bottom': img = img.transpose(Image.FLIP_BOTTOM_TOP) elif operation == 'crop': img = img.crop((left, top, right, bottom)) elif operation == 'filter': if filter_type == 'BLUR': img = img.filter(ImageFilter.BLUR) elif filter_type == 'CONTOUR': img = img.filter(ImageFilter.CONTOUR) elif filter_type == 'DETAIL': img = img.filter(ImageFilter.DETAIL) elif filter_type == 'EDGE_ENHANCE': img = img.filter(ImageFilter.EDGE_ENHANCE) elif filter_type == 'EDGE_ENHANCE_MORE': img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) elif filter_type == 'EMBOSS': img = img.filter(ImageFilter.EMBOSS) elif filter_type == 'FIND_EDGES': img = img.filter(ImageFilter.FIND_EDGES) elif filter_type == 'SHARPEN': img = img.filter(ImageFilter.SHARPEN) elif filter_type == 'SMOOTH': img = img.filter(ImageFilter.SMOOTH) elif filter_type == 'SMOOTH_MORE': img = img.filter(ImageFilter.SMOOTH_MORE) # 保存处理后的图片 img.save('processed_image.jpg') ``` 运行结果: 输入操作类型和操作参数后,程序将会对图片进行相应的操作,并保存处理后的图片文件。 注意:本程序仅提供常见的图像处理操作,对于更复杂的图像处理需求,需要根据实际情况进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值