图像去噪,简单边框裁剪

保存代码。若能帮上他人,荣幸之至

2.图像预处理工作

1.背景裁剪

经过分析发现,从网络途径采集到的文物图片中,有许多图片背景所占区域较大,而文物本身所占的比例过小。考虑到背景信息属于无关信息,为了适当降低背景区域的干扰,提高有用信息在图像数据中的比重,有如下两种思路: 1)进行前背景像素级分割; 2)按照矩形框简单裁剪放大。考虑到实际工作中由于光照环境、仪器等等因素,采集到的文物图片往往背景是多样化的。前背景像素级分割目前有基于深度学习地方法以及传统基于图的方法,考虑到其计算复杂度较高,而且如果分离效果不够好还可能造成文物图像边缘信息丢失。因此本文采取进行有效区域矩形裁剪的方式,保证在不丢失所有有效信息同时,尽量减少空白背景区域比例。矩形裁剪主要流程如下图:

在这里插入图片描述

2.图像去噪

图像在采集、传输、压缩等过程中,可能会由于各种操作引入不同的噪声,比如椒盐噪声,高斯噪声等,针对不同的噪声有不同的处理算法。常见的去噪滤波算法有均值滤波,中值滤波,高斯滤波,维纳滤波等。中值滤波法是一种非线性滤波,它将像素点的灰度值设置为该像素点邻域窗口内所有像素值的中值,从而消除孤立的噪声点,对于消除椒盐噪声比较有效,并且在滤除噪声的同时,能够较好地保护信号的边缘。均值滤波主要思想为使用邻域像素的平均值来代替原 来像素值,其缺点是在去噪的同时也破坏了图像的细节部分,从而使图像变得模糊,其优点是简单快速。高斯滤波是一种线性平滑滤波,每一个像素点的值,都由该像素值和该像素值邻域内的其他像素值经过加权平均后得到,而邻域不同像素的权值则是根据邻域像素和目标像素的远近以及高斯核的大小来决定,对于抑制高斯噪声非常有效。除此之外还有维纳滤波,也称之为最小均方差滤波器,其主要思想采用了统计学的手段,目标是找到一组函数,使输入信号经过函数的输出与期望输出的均方误差为最小。

3.效果展示

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XHMmyFiE-1641042252790)(E:/Typora/图片位置/image-20210328102158737.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sgjrobFo-1641042252792)(E:/Typora/图片位置/image-20210328102231664.png)]

至此图像预处理工作结束

代码如下:

  • 图像去噪
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('1.jpg')
dst = cv.fastNlMeansDenoisingColored(img,None,10,10,7,21)
import numpy as np
import cv2 as cv
import os


rootdir = 'D:\CeramicsQingXin'


for (dirpath, dirnames, filenames) in os.walk(rootdir):
    for filename in filenames:
        name = os.path.join(dirpath, filename)
        print(name)
        img = cv.imread(name)
        image = cv.fastNlMeansDenoisingColored(img,None,10,10,7,21)
        cv.imwrite(name, image)
  • 背景去除
# coding:utf-8
import numpy as np
import cv2 as cv
import os
import heapq


def tailor(string, index1=1, index2=1):
    '''
    :param string: 图片名加图片类型 例如:'1.jpg'
    :param index1: 如果值为1则显示原始图像,默认为1
    :param index2: 如果值为1则显示裁后图像,默认为1
    :return: 裁后图像的数字化值
    '''
    # 读取彩色图像数字化值
    image = cv.imread(string)
    # 转换得到灰色图像数字化值
    img = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    # 看一眼原始图像
    if index1 == 1:
        cv.imshow('image', image)
        cv.waitKey(0)
        cv.destroyAllWindows()
    # 灰度图去噪声,采用高斯核
    Gauss = cv.GaussianBlur(img, (5, 5), 0)
    # 计算梯度
    sobelx64f = cv.Sobel(Gauss, cv.CV_64F, 1, 0, ksize=5)
    abs_sobel64f = np.absolute(sobelx64f)
    sobel_8u = np.uint8(abs_sobel64f)
    # 梯度图去噪声
    Gauss_sobel = cv.GaussianBlur(sobel_8u, (5, 5), 0)
    # 二值化
    ret, th = cv.threshold(Gauss_sobel, 127, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
    # 形态学计算获得结构元素
    # 形态学腐蚀再膨胀
    kernel = np.ones((5, 5), np.uint8)
    opening = cv.morphologyEx(th, cv.MORPH_OPEN, kernel)
    # 查询轮廓点
    contours, hierarchy = cv.findContours(opening, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    # 获得裁剪范围
    ind = [len(contours[i]) for i in range(len(contours))]
    re1 = list(map(ind.index, heapq.nlargest(4, ind)))
    lis1 = np.concatenate((np.squeeze(contours[re1[0]]), np.squeeze(contours[re1[1]])), axis=0)
    lis2 = np.concatenate((np.squeeze(contours[re1[2]]), np.squeeze(contours[re1[3]])), axis=0)
    lis = np.concatenate((lis1, lis2), axis=0)
    yi = [li[0] for li in lis]
    max_yi = max(yi)
    min_yi = min(yi)
    xi = [li[1] for li in lis]
    max_xi = max(xi)
    min_xi = min(xi)
    # 裁剪
    tailored = image[min_xi:max_xi, min_yi:max_yi]
    # 看一眼裁剪后的图像
    if index2 == 1:
        cv.imshow('tailored', tailored)
        cv.waitKey(0)
        cv.destroyAllWindows()
    # 返回裁剪后图像的数字化值
    return tailored


if __name__ == '__main__':
    # 进入图片所在目录
    os.chdir(r'D:\大创文物分类\故宫文物图片集\金')
    image = tailor('385418.jpg', 0, 0)
    print(image)
    os.chdir(r'C:\Users\DELL\Desktop')
    cv.imwrite("./new_img.jpg", image)
# coding:utf-8
import numpy as np
import cv2 as cv
import os
import heapq

rootdir = 'D:\CeramicsQing'

for (dirpath, dirnames, filenames) in os.walk(rootdir):
    for filename in filenames:
        name = os.path.join(dirpath, filename)
        print(name)
        image = tailor(name, 0, 0)
        cv.imwrite(name, image)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值