【DAY_3】运动模糊、浮雕、腐蚀膨胀、锐化、晕影、直方图均衡处理

本文介绍了多种图像处理技术,包括运动模糊的处理,图像的锐化以增强细节,应用浮雕效果来突出图像纹理,以及使用腐蚀和膨胀操作改变图像边缘。此外,还探讨了如何通过晕影效果调整图像的视觉焦点,以及通过直方图均衡化来提升灰度图和彩色图的对比度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.运动模糊

import cv2
import numpy as np

img = cv2.imread('try.jpg',cv2.IMREAD_COLOR)
cv2.imshow('original',img)

size =15

#generating the kernel
kernel_motion_blur = np.zeros((size,size))
#水平运动模糊
kernel_motion_blur[ int((size-1)/2),: ] = np.ones(size)
#垂直运动模糊
# kernel_motion_blur[ int((size-1)/2),: ] = np.ones(size)
kernel_motion_blur =kernel_motion_blur /size

#applying the kernel to the input image
output = cv2.filter2D(img,-1,kernel_motion_blur)
cv2.namedWindow('Motion Blur',cv2.WINDOW_NORMAL)
cv2.imshow('Motion Blur',output)
cv2.waitKey()

2.锐化

import cv2
import numpy as np
img = cv2.imread('try.jpg')
cv2.imshow('Original', img)
# generating the kernels
#kernel总值为1
kernel_sharpen_1 = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
kernel_sharpen_2 = np.array([[1,1,1], [1,-7,1], [1,1,1]])
kernel_sharpen_3 = np.array([[-1,-1,-1,-1,-1],
[-1,2,2,2,-1],
[-1,2,8,2,-1],
[-1,2,2,2,-1],
[-1,-1,-1,-1,-1]]) / 8.0
# applying different kernels to the input image
output_1 = cv2.filter2D(img, -1, kernel_sharpen_1)
output_2 = cv2.filter2D(img, -1, kernel_sharpen_2)
output_3 = cv2.filter2D(img, -1, kernel_sharpen_3)
cv2.imshow('Sharpening', output_1)
cv2.imshow('Excessive Sharpening', output_2)
cv2.imshow('Edge Enhancement', output_3)
cv2.waitKey(0)

3.浮雕

import cv2
import numpy as np
img_emboss_input = cv2.imread('try.jpg')
# generating the kernels
kernel_emboss_1 = np.array([[0,-1,-1],
                            [1,0,-1],
                            [1,1,0]])
kernel_emboss_2 = np.array([[-1,-1,0],
                            [-1,0,1],
                            [0,1,1]])
kernel_emboss_3 = np.array([[1,0,0],
                            [0,0,0],
                            [0,0,-1]])
# converting the image to grayscale
gray_img = cv2.cvtColor(img_emboss_input,cv2.COLOR_BGR2GRAY)
# applying the kernels to the grayscale image and adding the offset
output_1 = cv2.filter2D(gray_img, -1, kernel_emboss_1) + 128
output_2 = cv2.filter2D(gray_img, -1, kernel_emboss_2) + 128
output_3 = cv2.filter2D(gray_img, -1, kernel_emboss_3) + 128
cv2.imshow('Input', img_emboss_input)
cv2.imshow('Embossing - South West', output_1)
cv2.imshow('Embossing - South East', output_2)
cv2.imshow('Embossing - North West', output_3)
cv2.waitKey(0)

4.腐蚀和膨胀

import cv2
import numpy as np

img = cv2.imread('try.jpg')
kernel = np.ones((5,5),np.uint8)
#参数3是迭代次数
img_erosion = cv2.erode(img,kernel,iterations=1)
img_dilation = cv2.dilate(img,kernel,iterations=1)
cv2.namedWindow('input',cv2.WINDOW_NORMAL)
cv2.imshow('input',img)
cv2.namedWindow('erosion',cv2.WINDOW_NORMAL)
cv2.imshow('erosion',img_erosion)
cv2.namedWindow('dilae',cv2.WINDOW_NORMAL)
cv2.imshow('dilae',img_dilation)
cv2.waitKey()

5.晕影

#晕影过滤器
import cv2
import numpy as np

img =  cv2.imread('try.jpg',cv2.IMREAD_COLOR)
# print(img.shape[:2])
rows,cols = img.shape[:2]

#generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(cols,200)
kernel_y = cv2.getGaussianKernel(rows,200)

# .T转置矩阵
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
output = np.copy(img)

#applying the mask to each channel in the input image

for i in range(3):
    output[:,:,i] = output[:,:,i] * mask
cv2.imshow('Original',img)
cv2.imshow('Vignette',output)
cv2.waitKey()

 we want to focus on a different region in the image, as shown in the following figure.

import cv2
import numpy as np
img = cv2.imread('try.jpg')
rows, cols = img.shape[:2]
# generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(int(1.5*cols),200)
kernel_y = cv2.getGaussianKernel(int(1.5*rows),200)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
mask = mask[int(0.5*rows):, int(0.5*cols):]
output = np.copy(img)
# applying the mask to each channel in the input image
for i in range(3):
    output[:,:,i] = output[:,:,i] * mask
cv2.imshow('Input', img)
cv2.imshow('Vignette with shifted focus', output)
cv2.waitKey(0)

6.直方图均衡--增加对比度(灰度图)

import cv2
import numpy as np
img = cv2.imread('try.jpg', 0)
# equalize the histogram of the input image
histeq = cv2.equalizeHist(img)
cv2.imshow('Input', img)
cv2.imshow('Histogram equalized', histeq)
cv2.waitKey(0)

7.直方图均衡--增强对比度(彩色图)

import cv2
import numpy as np

img = cv2.imread('try.jpg')

img_yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV)

#equalize the histogram of the Y channle 直方图均衡Y通道
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])

#conver the yuv image back to RGM format 将YUV颜色模型转换为RGB

img_output = cv2.cvtColor(img_yuv,cv2.COLOR_YUV2BGR)

cv2.imshow('input',img)
cv2.imshow('Historgram equalized',img_output)
cv2.waitKey()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值