opencv-卷积/锐化/边缘检测/模糊滤波器详解

本文深入探讨了图像处理中的边缘检测技术,包括优化方法、锐化滤波器、边缘检测滤波器、模糊滤波器及脊状/浮雕效果滤波器的应用。通过具体代码示例,展示了如何使用OpenCV进行图像处理,如边缘检测优化、不同类型的滤波器应用等。
import cv2
import numpy
import utils

def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()



#边缘检测优化方法 先对图像进行模糊处理,再转为为灰度彩色图像,再使用laplacian检测边缘。此方法可较好的避免将噪声错误地识别为边缘。
def strokeEdges(src,dst,blurKsize = 7,edgeKsize = 5):
    if blurKsize >=3:
        blurredSrc = cv2.medianBlur(src,blurKsize)
        graySrc = cv2.cvtColor(blurredSrc,cv2.COLOR_BGR2GRAY)
    else:
        graySrc = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
    cv2.Laplacian(graySrc,cv2.CV_8U,graySrc,ksize = edgeKsize )
    #cv_show('binary',binary)
    normalizedInverseAlpha = (1.0 / 255) * (255 - graySrc)
    #cv_show('normalized',normalizedInverseAlpha)
    channels = cv2.split(src)
    for channel in channels:
        channel[:] = channel * normalizedInverseAlpha
        #cv_show('channel',channel)
    dst = cv2.merge(channels)
    return dst



#一般的卷积滤波器方法
class VConvolutionFilter(object):
    #A filter that applies a convolution to V (or all of BGR)
    def __init__(self,kernel):
        self._kernel = kernel
    def apply(self,src):
        #Apply the filter with a BGR or gray source /destination
        dst = cv2.filter2D(src,-1,self._kernel)
        return dst

#特定的锐化滤波器方法
class shapenFilter(VConvolutionFilter):
    #A shapen filter with a 1-pixel radius
    def __init__(self):
        kernel = numpy.array([
            [-1,-1,-1],
            [-1, 9,-1],
            [-1,-1,-1],
        ])#若不想改变图像亮度,则权重加起来为1。若使权重和为0,则得到一个边缘检测核,把边缘转化为白色,把非边缘区域转为黑色,例如下面的FindEdgesFilter方法
        VConvolutionFilter.__init__(self,kernel)

#边缘检测滤波器。
class FindEdgesFilter(VConvolutionFilter):
    #An edge-finding filter with a 1-pixel radius
    def __init__(self):
        kernel = numpy.array([
            [-1,-1,-1],
            [-1, 9,-1],
            [-1,-1,-1],
        ])
        VConvolutionFilter.__init__(self,kernel)


#模糊滤波器,为了达到模糊效果,通常权重和为1,而且临近像素的权重全为正。下面实现一个简单的邻近平均滤波器:
class BlurFilter(VConvolutionFilter):
    #A blur filter with a 2-piexl radius
    def __init__(self):
        kernel = numpy.array([
        [0.04,0.04,0.04,0.04,0.04],
        [0.04,0.04,0.04,0.04,0.04],
        [0.04,0.04,0.04,0.04,0.04],
        [0.04,0.04,0.04,0.04,0.04],
        [0.04,0.04,0.04,0.04,0.04],
        ])
        VConvolutionFilter.__init__(self,kernel)

#脊状/浮雕效果滤波器
class EmbossFilter(VConvolutionFilter):
    def __init__(self):
        kernel = numpy.array([
            [-2,-1, 0],
            [-1, 9, 1],
            [ 0, 1, 2],
        ])
        VConvolutionFilter.__init__(self,kernel)



image = cv2.imread(r'C:\Users\Owen\Pictures\lena.jpg')
dst = numpy.ones((image.shape[0],image.shape[1]),dtype = numpy.int8)

dst = strokeEdges(image,dst)
cv_show('strokeEdges',dst)

shape = shapenFilter()
dst = shape.apply(image)
cv_show('strokeEdges',dst)

FindEdgesFilter = FindEdgesFilter()
dst =FindEdgesFilter.apply(image)
cv_show('FindEdgesFilter',dst)

BlurFilter = BlurFilter()
dst = BlurFilter.apply(image)
cv_show('blurfilter',dst)

EmbossFilter = EmbossFilter()
dst = EmbossFilter.apply(image)
cv_show('EmbossFilter',dst)

边缘检测优化方法图像结果:
在这里插入图片描述
锐化滤波器方法图像结果:
在这里插入图片描述
边缘检测滤波器方法图像结果:
在这里插入图片描述
模糊滤波器方法图像结果:
在这里插入图片描述

脊状/浮雕效果滤波器图像效果: 在这里插入图片描述

Brightness Preserving Dynamic Fuzzy Histogram Equalization(BPDFHE) proposes a novel modification of the brightness preserving dynamic histogram equalization technique to improve its brightness preserving and contrast enhancement abilities while reducing its computational complexity. This technique, called uses fuzzy statistics of digital images for their representation and processing. Representation and processing of images in the fuzzy domain enables the technique to handle the inexactness of gray level values in a better way, resulting in improved performance. Besides, the imprecision in gray levels is handled well by fuzzy statistics, fuzzy histogram, when computed with appropriate fuzzy membership function, does not have random fluctuations or missing intensity levels and is essentially smooth. This helps in obtaining its meaningful partitioning required for brightness preserving equalization. Details of the method are available in D. Sheet, H. Garud, A. Suveer, J. Chatterjee and M. Mahadevappa, "Brightness Preserving Dynamic Fuzzy Histogram Equalization", IEEE Trans., Consumer Electronics, vol. 56, no. 4, pp. 2475 - 2480, Nov. 2010. [http://dx.doi.org/10.1109/TCE.2010.5681130] Implementation of the technique for grayscale images is outright. The function operates on non-sparce images of type uint8, uint16, int16, double and single. Using BPDFHE for Color images can be done accordingly rgbInputImage = imread('peppers.png'); labInputImage = applycform(rgbInputImage,makecform('srgb2lab')); Lbpdfhe = fcnBPDFHE(labInputImage(:,:,1)); labOutputImage = cat(3,Lbpdfhe,labInputImage(:,:,2),labInputImage(:,:,3)); rgbOutputImage = applycform(labOutputImage,makecform('lab2srgb')); Details of the technique for implementing BPDFHE on color images is detailed in Garud, H. Sheet, D. Suveer, A. Karri, P.K. Ray, A.K. Mahadevappa, M. Chatterjee, J., "Brightness preserving contrast enhancement in digital pathology", Proc. ICIIP 2011. [http://dx.doi.org/10.1109/ICIIP.2011.6108964]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值