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

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 *i
            elif i < x2:
                f[i] = ((y2 - y1) / (x2 - x1)) * (i - x1) + y1
            else:
                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 ** g

        image = 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 plt

class point_ope():
    def inversion_f(digit):
        return 255 - digit
    def 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 - x
        plt.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 *i
            elif i < x2:
                f[i] = ((y2 - y1) / (x2 - x1)) * (i - x1) + y1
            else:
                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 = 50
        y1 = 25
        x2 = 205
        y2 = 230
        
        new = 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 = 1
        alpha = 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 ** g

        image = 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.5
        c = 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*=-1
        mat2 = 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()
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值