1. 相关运算与卷积运算什么区别?

在只考虑R这个实数域下

这两个式子在表达上只有一个差别,就是在运算时,后面的函数的变量多了一个负号。

以下区别

  1. 卷积是对称的 conv(f,g)=conv(g,f)。而滑动相关不对称,ccorr(f,g)~=ccorr(g,f)。

  2. 卷积是两个系统作用时的响应,(由于对称性,谁作用于谁并不本质)。滑动相关是衡量两个函数相似度,与相对位置之间的关系。

  3. 信号处理上卷积可以进行局部操作(就是滤波),例如图像的高斯模糊。系统响应分析。滑动相关一般用来进行特征检测,比如图像特征提取

2. 空域平滑主要去掉了什么信息?空域锐化突出了什么类型的信息?

  1. 平滑技术是为了减少图像的噪声。一般情况下,在空域内用邻域平均减少噪声;在频域内用低通滤波来减少噪声

  2. 锐化技术是为了突出边缘,加强轮廓特征。在空域内用微分法使图像清晰;在频域内用高通滤波来清晰图像。

3. 实现点运算算法

图片求反

核心代码

    def inversion_f(digit):return 255 - digit

效果

增强对比度

核心代码

    def contrast_f(image, x1, y1, x2, y2):f = np.zeros(256)for i in range(len(f)):if i < x1:f[i] = y1/x1 *ielif i < x2:f[i] = ((y2 - y1) / (x2 - x1)) * (i - x1) + y1else:f[i] = ((y2 - 255.0) / (x2 - 255.0)) * (i - 255.0) + 255.0#f = [np.round(i)for i in f]image = cv2.LUT(image, f)#f的一一映射return np.uint8(image+0.5)

效果和函数图像

log 变换

核心代码

def logf(image,alpha): return np.uint8( alpha * np.log(1 + image) + 0.5)

效果

Gamma变换

s = c r γ 灰 度 归 一 化 r ∈ [ 0 , 1 ] , γ 和 c 是 自 定 义 的 s = cr^\gamma\quad 灰度归一化r∈[0,1],\gamma和c是自定义的 s=crγ灰度归一化r∈[0,1],γ和c是自定义的
核心代码

    def gammaf(image, c, g):f = np.zeros(256)for i in range(256):f[i] = c * i ** gimage = cv2.LUT(image, f)return np.uint8(image + 0.5)

效果

此处gamma = 2.5

4. 实现空域平滑算法

空域平滑的本质是使用卷积来进行计算
离散化格式如下

均值

核心代码

mat = np.array([[1/9,1/9,1/9],[1/9,1/9,1/9],[1/9,1/9,1/9]])for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat[0][0]*con[y+1][x+1] +  mat[0][1]*con[y+1][x] +  mat[0][2]*con[y+1][x-1]+mat[1][0]*con[y][x+1] + mat[1][1]*con[y][x] +  mat[1][2]*con[y][x-1] + mat[2][0]*con[y-1][x+1] +  mat[2][1]*con[y-1][x] +  mat[2][2]*con[y-1][x-1]

效果

加权均值

核心代码

mat = np.array([[1/16,2/16,1/16],[2/16,4/16,2/16],[1/16,2/16,1/16]])#其余部分几乎一致

效果

中值

对相邻的奇数个点进行排序并取中间值作为新值
核心代码

arr = np.array((0,0,0,0,0))new_gray = np.zeros_like(gray)
for y in range(height):for x in range(width):for i in range(5):if (x-2+i < 0 or x-2+i >= width):arr[i] = 0 else: arr[i] = gray[y][x-2+i]arr.sort()new_gray[y][x] = arr[2]

效果

5. 实现空域锐化算法

锐 化 便 得 使 用 相 关 运 算 锐化便得使用相关运算 锐化便得使用相关运算

sobel

核心代码

mat1 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
mat2 = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat1[0][0]*con[y-1][x-1] +  mat1[0][1]*con[y-1][x] +  mat1[0][2]*con[y-1][x+1]+mat1[1][0]*con[y][x-1] + mat1[1][1]*con[y][x] +  mat1[1][2]*con[y][x+1] + mat1[2][0]*con[y+1][x-1] +  mat1[2][1]*con[y+1][x] +  mat1[2][2]*con[y+1][x+1])

效果

prewitt

核心代码

mat1 = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
mat2 = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])#其余基本一致

效果

krisch

核心代码

mat1 = np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])
mat2 = np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])


效果

laplace

核心代码

mat1 = np.array([[1,1,1],[1,-8,1],[1,1,1]])
mat1*=-1
mat2 = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])

效果

整合代码


from multiprocessing.connection import wait
from turtle import heading
from cv2 import waitKey
import numpy as np
import cv2
import math
import matplotlib.pyplot as pltclass point_ope():def inversion_f(digit):return 255 - digitdef inversion(image):cv2.imshow('',image)gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)cv2.imshow('start',gray)for i in range(len(gray)):gray[i] = point_ope.inversion_f(gray[i])image[i] = point_ope.inversion_f(image[i])cv2.imshow('gray',gray)cv2.imshow('new',image)x = np.arange(0,256)y = 255 - xplt.figure(dpi=50)plt.plot(x, y ,"b")plt.show()#展现函数形式cv2.waitKey(0)def contrast_f(image, x1, y1, x2, y2):f = np.zeros(256)for i in range(len(f)):if i < x1:f[i] = y1/x1 *ielif i < x2:f[i] = ((y2 - y1) / (x2 - x1)) * (i - x1) + y1else:f[i] = ((y2 - 255.0) / (x2 - 255.0)) * (i - 255.0) + 255.0#f = [np.round(i)for i in f]image = cv2.LUT(image, f)#f的一一映射return np.uint8(image+0.5) def contrast(image):cv2.imshow('init', image)gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)cv2.imshow('gray', gray)x1 = 50y1 = 25x2 = 205y2 = 230new = point_ope.contrast_f(image, x1, y1, x2, y2)gray =point_ope.contrast_f(gray, x1, y1, x2, y2)cv2.imshow('after', new)cv2.imshow('after gray', gray)plt.figure(dpi=50 )plt.rc('font',family = 'SimHei')#显示中文plt.plot([0, x1, x2, 255], [0, y1, y2, 255], 'b', linewidth=1)plt.plot([x1, x1, 0], [0, y1, y1], 'r--')plt.plot([x2, x2, 0], [0, y2, y2], 'r--')plt.title('自定义函数')plt.xlim([0, 255]), plt.ylim([0, 255])plt.show()cv2.waitKey(0)def logf(image,alpha): return np.uint8( alpha * np.log(1 + image) + 0.5)def log(image):cv2.imshow('init', image)gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)cv2.imshow('gray', gray)r = 1alpha = 255/np.log(1+255*r)new = point_ope.logf(image, alpha)gray =point_ope.logf(gray, alpha)cv2.imshow('after', new)cv2.imshow('after gray', gray)x = np.arange(0,256,0.01)y = alpha * np.log(1 + x)plt.figure(dpi=50 )plt.rc('font',family = 'SimHei')#显示中文plt.plot(x, y, 'b', linewidth = 1)plt.title('log函数')plt.xlim([0, 256]), plt.ylim([0, 256])plt.show()cv2.waitKey(0)def gammaf(image, c, g):f = np.zeros(256)for i in range(256):f[i] = c * i ** gimage = cv2.LUT(image, f)return np.uint8(image + 0.5)def gamma(image):cv2.imshow('init', image)gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)cv2.imshow('gray', gray)g = 2.5c = 255/(255**g)print(c*255**g)new = point_ope.gammaf(image, c, g)gray =point_ope.gammaf(gray, c, g)cv2.imshow('after', new)cv2.imshow('after gray', gray)plt.figure(dpi=50 )plt.rc('font',family = 'SimHei')#显示中文plt.title('自定义函数')plt.xlim([0, 255]), plt.ylim([0, 255])x = np.arange(0,256,0.01)y = (c * x ** g) plt.plot(x, y, 'r', linewidth = 1)plt.show()cv2.waitKey(0)class spatial_smooth():def mean(image):cv2.imshow("image", image)mat = np.array([[1/9,1/9,1/9],[1/9,1/9,1/9],[1/9,1/9,1/9]])res = cv2.filter2D(image, -1, mat);cv2.imshow("?",res)#自己的实现height, width = image.shape[:2]con = np.zeros((height+2, width+2, 3))new_img = np.zeros_like(image,dtype=np.uint8)for y in range(height):for x in range(width):con[y+1][x+1] = image[y][x]for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat[0][0]*con[y+1][x+1] +  mat[0][1]*con[y+1][x] +  mat[0][2]*con[y+1][x-1]+mat[1][0]*con[y][x+1] + mat[1][1]*con[y][x] +  mat[1][2]*con[y][x-1] + mat[2][0]*con[y-1][x+1] +  mat[2][1]*con[y-1][x] +  mat[2][2]*con[y-1][x-1])cv2.imshow("new", np.uint8(new_img))waitKey(0)def weighted_mean(image):#在矩阵元素为9的情况下,高斯平均等于加权平均cv2.imshow("image", image)mat = np.array([[1/16,2/16,1/16],[2/16,4/16,2/16],[1/16,2/16,1/16]])res = cv2.filter2D(image, -1, mat);cv2.imshow("?",res)#自己的实现height, width = image.shape[:2]con = np.zeros((height+2, width+2, 3))new_img = np.zeros_like(image,dtype=np.uint8)for y in range(height):for x in range(width):con[y+1][x+1] = image[y][x]for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat[0][0]*con[y+1][x+1] +  mat[0][1]*con[y+1][x] +  mat[0][2]*con[y+1][x-1]+mat[1][0]*con[y][x+1] + mat[1][1]*con[y][x] +  mat[1][2]*con[y][x-1] + mat[2][0]*con[y-1][x+1] +  mat[2][1]*con[y-1][x] +  mat[2][2]*con[y-1][x-1])cv2.imshow("new", np.uint8(new_img))waitKey(0)def middle(image):gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)height, width = gray.shape[:2]arr = np.array((0,0,0,0,0))new_gray = np.zeros_like(gray)for y in range(height):for x in range(width):for i in range(5):if (x-2+i < 0 or x-2+i >= width):arr[i] = 0 else: arr[i] = gray[y][x-2+i]arr.sort()new_gray[y][x] = arr[2]cv2.imshow("gray", gray)cv2.imshow("new",new_gray)waitKey(0)class spatial_sharpening():def sobel(image):gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)mat1 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])mat2 = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])#res = cv2.filter2D(image, -1, mat);cv2.imshow("?",gray)#自己的实现height, width = gray.shape[:2]con = np.zeros((height+2, width+2))new_img = np.zeros_like(gray)for y in range(height):for x in range(width):con[y+1][x+1] = gray[y][x]for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat1[0][0]*con[y-1][x-1] +  mat1[0][1]*con[y-1][x] +  mat1[0][2]*con[y-1][x+1]+mat1[1][0]*con[y][x-1] + mat1[1][1]*con[y][x] +  mat1[1][2]*con[y][x+1] + mat1[2][0]*con[y+1][x-1] +  mat1[2][1]*con[y+1][x] +  mat1[2][2]*con[y+1][x+1])cv2.imshow("horizontal", np.uint8(new_img))for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat2[0][0]*con[y-1][x-1] +  mat2[0][1]*con[y-1][x] +  mat2[0][2]*con[y-1][x+1]+mat2[1][0]*con[y][x-1] + mat2[1][1]*con[y][x] +  mat2[1][2]*con[y][x+1] + mat2[2][0]*con[y+1][x-1] +  mat2[2][1]*con[y+1][x] +  mat2[2][2]*con[y+1][x+1])cv2.imshow("portrait", np.uint8(new_img))waitKey(0)def prewitt(image):gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)mat1 = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])mat2 = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])#res = cv2.filter2D(image, -1, mat);cv2.imshow("?",gray)#自己的实现height, width = gray.shape[:2]con = np.zeros((height+2, width+2))new_img = np.zeros_like(gray)for y in range(height):for x in range(width):con[y+1][x+1] = gray[y][x]for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat1[0][0]*con[y-1][x-1] +  mat1[0][1]*con[y-1][x] +  mat1[0][2]*con[y-1][x+1]+mat1[1][0]*con[y][x-1] + mat1[1][1]*con[y][x] +  mat1[1][2]*con[y][x+1] + mat1[2][0]*con[y+1][x-1] +  mat1[2][1]*con[y+1][x] +  mat1[2][2]*con[y+1][x+1])cv2.imshow("horizontal", np.uint8(new_img))for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat2[0][0]*con[y-1][x-1] +  mat2[0][1]*con[y-1][x] +  mat2[0][2]*con[y-1][x+1]+mat2[1][0]*con[y][x-1] + mat2[1][1]*con[y][x] +  mat2[1][2]*con[y][x+1] + mat2[2][0]*con[y+1][x-1] +  mat2[2][1]*con[y+1][x] +  mat2[2][2]*con[y+1][x+1])cv2.imshow("portrait", np.uint8(new_img))waitKey(0)def krisch(image):gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)mat1 = np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])mat2 = np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])#res = cv2.filter2D(image, -1, mat);cv2.imshow("?",gray)#自己的实现height, width = gray.shape[:2]con = np.zeros((height+2, width+2))new_img = np.zeros_like(gray)for y in range(height):for x in range(width):con[y+1][x+1] = gray[y][x]for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat1[0][0]*con[y-1][x-1] +  mat1[0][1]*con[y-1][x] +  mat1[0][2]*con[y-1][x+1]+mat1[1][0]*con[y][x-1] + mat1[1][1]*con[y][x] +  mat1[1][2]*con[y][x+1] + mat1[2][0]*con[y+1][x-1] +  mat1[2][1]*con[y+1][x] +  mat1[2][2]*con[y+1][x+1])cv2.imshow("mat1", np.uint8(new_img))for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat2[0][0]*con[y-1][x-1] +  mat2[0][1]*con[y-1][x] +  mat2[0][2]*con[y-1][x+1]+mat2[1][0]*con[y][x-1] + mat2[1][1]*con[y][x] +  mat2[1][2]*con[y][x+1] + mat2[2][0]*con[y+1][x-1] +  mat2[2][1]*con[y+1][x] +  mat2[2][2]*con[y+1][x+1])cv2.imshow("mat2", np.uint8(new_img))waitKey(0)def laplace(image):gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)mat1 = np.array([[1,1,1],[1,-8,1],[1,1,1]])mat1*=-1mat2 = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])#res = cv2.filter2D(image, -1, mat);cv2.imshow("?",gray)#自己的实现height, width = gray.shape[:2]con = np.zeros((height+2, width+2))new_img = np.zeros_like(gray)for y in range(height):for x in range(width):con[y+1][x+1] = gray[y][x]for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat1[0][0]*con[y-1][x-1] +  mat1[0][1]*con[y-1][x] +  mat1[0][2]*con[y-1][x+1]+mat1[1][0]*con[y][x-1] + mat1[1][1]*con[y][x] +  mat1[1][2]*con[y][x+1] + mat1[2][0]*con[y+1][x-1] +  mat1[2][1]*con[y+1][x] +  mat1[2][2]*con[y+1][x+1])cv2.imshow("mat1", np.uint8(new_img))for y in range(1,height+1):for x in range(1,width+1):new_img[y-1][x-1] = (mat2[0][0]*con[y-1][x-1] +  mat2[0][1]*con[y-1][x] +  mat2[0][2]*con[y-1][x+1]+mat2[1][0]*con[y][x-1] + mat2[1][1]*con[y][x] +  mat2[1][2]*con[y][x+1] + mat2[2][0]*con[y+1][x-1] +  mat2[2][1]*con[y+1][x] +  mat2[2][2]*con[y+1][x+1])cv2.imshow("mat2", np.uint8(new_img))waitKey(0)
def test_point():image = cv2.imread("pic4.png")#point_ope.inversion(image)#point_ope.contrast(image)#point_ope.log(image)point_ope.gamma(image)def test_spatial():image = cv2.imread("pic4.png")#spatial_smooth.mean(image)#spatial_smooth.weighted_mean(image)image = cv2.imread("pic5.png")spatial_smooth.middle(image)def test_sharpen():image = cv2.imread("pic4.png")#spatial_sharpening.sobel(image)#spatial_sharpening.prewitt(image)#spatial_sharpening.krisch(image)spatial_sharpening.laplace(image)if __name__ == '__main__':#test_point()#test_spatial()test_sharpen()

数字图像处理个人练习02--点运算、空域平滑、空域锐化相关推荐

  1. 数字图像处理:线性和非线性滤波的平滑空间滤波器(Smoothing Spatial Filters)

    引言 本系列文章记录老猿自学冈萨雷斯<数字图像处理>的感悟和总结,不过估计更新会比较慢,白天要工作,都是晚上抽空学习,学习完一章再回头总结,想学的朋友可以自己下载英文原版(目前到第四版)和 ...

  2. 数字图像处理100问—02灰度化(Grayscale)

    提示:内容整理自:https://github.com/gzr2017/ImageProcessing100Wen CV小白从0开始学数字图像处理 02灰度化(Grayscale) 将图像灰度化,通过 ...

  3. 数字图像处理 实验二:图像的平滑滤波

    基于Matlab的图像的平滑滤波 DIP实验2:图像的平滑滤波 实验目的 实验内容 参考代码 实验结果 DIP实验2:图像的平滑滤波 实验目的 平滑的目的是减少噪声对图像的影响.掌握线性滤波和中值滤波 ...

  4. 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波13 - 平滑低通滤波器 -盒式滤波器核

    这里写目录标题 平滑(低通)空间滤波器 盒式滤波器核 平滑(低通)空间滤波器 平滑(也称平均)空间滤波器用于降低灰度的急剧过渡 在图像重取样之前平滑图像以减少混淆 用于减少图像中无关细节 平滑因灰度级 ...

  5. 数字图像处理【3】图像空间滤波-平滑

    上一篇内容讲了图像的灰度变换,它主要是运用在单点的处理,在图像的对比度.亮度等方面进行增强.这一次介绍图像的空间滤波,它会利用像素周围领域的像素信息,对图像的灰度进行处理.处理的目的主要有以下几个方面 ...

  6. 数字图像处理学习笔记(六)平滑滤波:均值滤波、中值滤波

    实验截图: 均值滤波和中值滤波处理后的结果 实验代码: 代码(1): img=imread('microchip.png'); img=rgb2gray(img); subplot(1,3,1); i ...

  7. Apr.11st 数字图像处理连载(02)

    之前的blog因为手机号别的用户提前注册了,用户名不是我喜欢的所以就找客服重新注册了一个账号,之前的blog过两天也会搬运回来 第二章:数字图像基础 2.4 图像的取样和量化 2.4.2 数字图像的表 ...

  8. 第4章 Python 数字图像处理(DIP) - 频率域滤波11 - 使用高通滤波器锐化图像

    目录 使用高通滤波器锐化图像 由低通滤波器得到理想.高斯和巴特沃斯高通滤波器 指纹增强 频域中的拉普拉斯 钝化掩蔽.高提升滤波和高频强调滤波 同态滤波 使用高通滤波器锐化图像 由低通滤波器得到理想.高 ...

  9. matlab数字图像处理:时空域图像增强

    在数字图像处理领域,对于时空域图像的增强一般包括运用直方图均衡.亮度变换.空域平滑和锐化等方法实现对于给定图像的时空域的增强. 本文所展示的内容包括: (1)计算并绘制图像直方图,编程实现图像的直方图 ...

  10. 中南民族大学计算机图像处理实验报告,中南民族大学数字图像处理实验报告.docx...

    院 系:计算机科学学院 专 业: 计算机科学与技术 年 级: 2011 级 课程 名称: 数字图像处理 组 号: 02 姓名(学号): 谢枫石小飞黄煜柳卫平李春豪指导教师:徐胜舟 2014年4月29日 ...

最新文章

  1. pytorch BiLSTM+CRF代码详解 重点
  2. ubuntu系统安装mysql二进制压缩包(tar.gz)以及navicat远程连接服务器(linux系统)
  3. python从1到n出现了多少个1-【算法21】从1到n的正数中1的出现次数
  4. python中and与or的执行顺序-python之执行顺序随记
  5. c# 多线程多个参数
  6. 如何在一个程序集中序列化在另一个中反序列化
  7. boost::detail模块实现宏BOOST_DETAIL_IS_XXX_DEF的测试程序
  8. 香港科技大学TensorFlow速成(1)
  9. mysql 单表多级查询_mysql单表与多表查询
  10. class layout basic 2
  11. Java-多线程第三篇3种创建的线程方式、线程的生命周期、线程控制、线程同步、线程通信...
  12. 水电图纸——电气系统识图-3.4
  13. 电脑没有声音怎么安装声卡驱动?驱动人生声卡驱动安装失败原因
  14. 【R】R语言指定包安装目录
  15. Android Framework 的整体建构
  16. C++代码审阅–ice104协议从站(5)
  17. Fedora linux 3322动态域名解析设置
  18. 徐直军:今年至少3亿设备用上鸿蒙,互联网又一领域暗藏“金矿“
  19. 软件工程小组课程设计项目
  20. Prometheus自带指标整理

热门文章

  1. Android studio 和 gradle插件版本对应关系(最新 2022年)
  2. 操作系统常见面试题(2021最新版)
  3. matlab模拟股票价格,MATLAB进行模拟股票价格???
  4. 我学编程时最后悔的事!
  5. 压铸模具的失效形式与模具设计要点
  6. java基于springboot的家庭理财记账
  7. 关于JavaScript的箭头函数
  8. 六面钻调试第二步配置端口映射
  9. C++递增和递减运算符
  10. 【技术贴】asms文件,安装windows xp原版时,需要“asms”文件,H:\I386\asm