總結:Python 圖像處理

目錄

1、灰度化功能

2、反轉功能

3、通道分離功能

4、噪音、濾波功能

5、高斯雙邊濾波功能

6、均值偏移濾波功能

7、圖像二值化功能

8、Canny邊緣檢測功能

9、直線檢測功能

10、圓形檢測功能

11、輪廓發現功能

12、人臉檢測功能


該項目可實現圖像的多樣化處理,基本上包含了OpenCV模塊常用的圖像處理功能,非常適合初學者理解和應用,包括:灰度化功能、反轉功能、通道分離功能、噪音濾波功能、高斯雙邊濾波功能、均值偏移濾波功能、圖像二值化功能、Canny邊緣檢測功能、直線檢測功能、圓形檢測功能、輪廓發現功能和人臉檢測功能。

1、灰度化功能

                灰度化功能,效果如下所示:

                

實現代碼如下所示:

    # 灰度化
    def to_gray(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
        # print("類型", type(gray))
        # get_image_info(gray)
        self.decode_and_show_dst(gray)

2、反轉功能

               反轉功能,按位取反,白變黑,黑變白,效果如下所示:

               

實現代碼如下所示:

    # 反轉
    def bitwise_not(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
        dst = cv.bitwise_not(src)  
        self.decode_and_show_dst(dst)

3、通道分離功能

              通道分離功能,效果如下所示:

               

實現代碼如下所示:

    # 通道分離
    def channels_split(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
        b, g, r = cv.split(src)
        merge_image = cv.merge([b, g, r])
        """創建三維數組,0維爲B,1維爲G,2維爲R"""
        height, width, channels = src.shape
        img = np.zeros([height*2, width*2, channels], np.uint8)
        img[0:height, 0:width] = np.expand_dims(b, axis=2)
        img[0:height, width:width*2] = np.expand_dims(g, axis=2)
        img[height:height*2, 0:width] = np.expand_dims(r, axis=2)
        img[height:height*2, width:width*2] = merge_image
 
        self.decode_and_show_dst(img)

4、噪音、濾波功能

       圖像處理中噪聲:主要有三種:

  • 椒鹽噪聲(Salt & Pepper):含有隨機出現的黑白亮度值。
  • 脈衝噪聲:只含有隨機的正脈衝和負脈衝噪聲。
  • 高斯噪聲:含有亮度服從高斯或正態分佈的噪聲。高斯噪聲是很多傳感器噪聲的模型,如攝像機的電子干擾噪聲。

       濾波器主要兩類:線性和非線性

  • 線性濾波器:使用連續窗函數內像素加權和來實現濾波,同一模式的權重因子可以作用在每一個窗口內,即線性濾波器是空間不變的。

       如果圖像的不同部分使用不同的濾波權重因子,線性濾波器是空間可變的。因此可以使用卷積模板來實現濾波。線性濾波器對去除高斯噪聲有很好的效果。常用的線性濾波器有均值濾波器和高斯平滑濾波器。

       (1) 均值濾波器:最簡單均值濾波器是局部均值運算,即每一個像素只用其局部鄰域內所有值的平均值來置換。

       (2) 高斯平滑濾波器是一類根據高斯函數的形狀來選擇權值的線性濾波器。 高斯平滑濾波器對去除服從正態分佈的噪聲是很有效的。

  • 非線性濾波器:

       (1) 中值濾波器:均值濾波和高斯濾波運算主要問題是有可能模糊圖像中尖銳不連續的部分。中值濾波器的基本思想使用像素點鄰域灰度值的中值來代替該像素點的灰度值,它可以去除脈衝噪聲、椒鹽噪聲同時保留圖像邊緣細節。中值濾波不依賴於鄰域內與典型值差別很大的值,處理過程不進行加權運算。中值濾波在一定條件下可以克服線性濾波器所造成的圖像細節模糊,而對濾除脈衝干擾很有效。

       (2) 邊緣保持濾波器:由於均值濾波:平滑圖像外還可能導致圖像邊緣模糊和中值濾波:去除脈衝噪聲的同時可能將圖像中的線條細節濾除。邊緣保持濾波器是在綜合考慮了均值濾波器和中值濾波器的優缺點後發展起來的,它的特點是:濾波器在除噪聲脈衝的同時,又不至於使圖像邊緣十分模糊。

       過程:分別計算[i,j]的左上角子鄰域、左下角子鄰域、右上角子鄰域、右下角子鄰域的灰度分佈均勻度V;然後取最小均勻度對應區域的均值作爲該像素點的新灰度值。分佈越均勻,均勻度V值越小。v=<(f(x, y) - f_(x, y))^2

噪音、濾波功能,效果如下所示:
                  

 實現代碼如下所示:

    def noise_and_blur(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        # 加高斯噪聲
        h, w, c = src.shape
        for row in range(h):
            for col in range(w):
                s = np.random.normal(0, 20, 3)  # normal(loc=0.0, scale=1.0, size=None),均值,標準差,大小
 
                b = src[row, col, 0]
                g = src[row, col, 1]
                r = src[row, col, 2]
 
                src[row, col, 0] = clamp(b + s[0])
                src[row, col, 1] = clamp(g + s[1])
                src[row, col, 2] = clamp(r + s[2])
 
        img = np.zeros([h * 2, w * 2, c], np.uint8)
        img[0:h, 0:w] = src
 
        # GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)
        # ksize表示卷積核大小,sigmaX,Y表示x,y方向上的標準差,這兩者只需一個即可,並且ksize爲大於0的奇數
        dst = cv.GaussianBlur(src, (5, 5), 0)  # 高斯模糊,sigmaX與ksize一個爲0
        img[0:h, w:w*2] = dst
 
        self.decode_and_show_dst(img)

5、高斯雙邊濾波功能

高斯雙邊濾波功能,同時考慮空間與信息和灰度相似性,達到保邊去噪的目的。雙邊濾波的核函數是空間域核與像素範圍域核的綜合結果:在圖像的平坦區域,像素值變化很小,對應的像素範圍域權重接近於1,此時空間域權重起主要作用,相當於進行高斯模糊;在圖像的邊緣區域,像素值變化很大,像素範圍域權重變大,從而保持了邊緣的信息。效果如下所示:
                  

 實現代碼如下所示:

    # 高斯雙邊濾波
    def bilateral_filter(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        dst = cv.bilateralFilter(src, 0, 100, 15)
        self.decode_and_show_dst(dst)

6、均值偏移濾波功能

             均值偏移濾波功能,效果如下所示:

             

實現代碼如下所示:

    # 均值偏移濾波
    def mean_shift_filter(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        dst = cv.pyrMeanShiftFiltering(src, 10, 50)  # 均值偏移濾波
        self.decode_and_show_dst(dst)

7、圖像二值化功能

                圖像二值化功能,效果如下所示:

                

 實現代碼如下所示:

    # 圖像二值化
    def threshold(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
 
        # 這個函數的第一個參數就是原圖像,原圖像應該是灰度圖。
        # 第二個參數就是用來對像素值進行分類的閾值。
        # 第三個參數就是當像素值高於(有時是小於)閾值時應該被賦予的新的像素值
        # 第四個參數來決定閾值方法,見threshold_simple()
        # ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
        ret, dst = cv.threshold(gray, 127, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
        self.decode_and_show_dst(dst)

8、Canny邊緣檢測功能

                Canny邊緣檢測功能,效果如下所示:

                

實現代碼如下所示:

    # Canny邊緣檢測
    def canny_edge(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        blurred = cv.GaussianBlur(src, (3, 3), 0)
        gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
 
        grad_x = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
        grad_y = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
 
        dst = cv.Canny(grad_x, grad_y, 30, 150)
        # dst = cv.Canny(gray, 50, 150)
        self.decode_and_show_dst(dst)

9、直線檢測功能

直線檢測功能,效果如下所示:

                

 實現代碼如下所示:

    # 直線檢測
    def hough_line(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
        edges = cv.Canny(gray, 50, 150, apertureSize=3)
        lines = cv.HoughLines(edges, 1, np.pi/180, 200)
 
        for line in lines:
            rho, theta = line[0]
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0+1000*(-b))
            y1 = int(y0+1000*(a))
            x2 = int(x0-1000*(-b))
            y2 = int(y0-1000*(a))
            cv.line(src, (x1, y1), (x2, y2), (0, 0, 255), 2)
 
        self.decode_and_show_dst(src)

10、圓形檢測功能

圓形檢測功能,效果如下所示:

           

實現代碼如下所示:

    # 圓檢測
    def hough_circles(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        dst = cv.pyrMeanShiftFiltering(src, 10, 100)
        cimage = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
        circles = cv.HoughCircles(cimage, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            cv.circle(src, (i[0], i[1]), i[2], (0, 0, 255), 2)
            cv.circle(src, (i[0], i[1]), 2, (255, 0, 255), 2)
 
        self.decode_and_show_dst(src)

11、輪廓發現功能

輪廓發現功能,並進行覆蓋,效果如下所示:

                   

 實現代碼如下所示:

    # 輪廓發現
    def find_contours(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        dst = cv.GaussianBlur(src, (3, 3), 0)
        gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
        ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
        contous, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        for i,contou in enumerate(contous):
            cv.drawContours(src, contous, i, (0, 0, 255), 1)
 
        # 輪廓
        self.decode_and_show_dst(src)
 
        # 輪廓覆蓋
        for i,contou in enumerate(contous):
            cv.drawContours(src, contous, i, (0, 0, 255), -1)
        self.decode_and_show_dst(src)

12、人臉檢測功能

人臉檢測功能,效果如下所示:

                 

 實現代碼如下所示:

    #人臉識別 正臉 需要下載xml模型 haarcascade_frontalface_alt.xml
    def face_recognize(self):
        src = self.cv_read_img(self.src_file)
        if src is None:
            return
 
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
        face_cascade = cv.CascadeClassifier('haarcascade_frontalface_alt2.xml')
        faces = face_cascade.detectMultiScale(
            gray,
            scaleFactor=1.15,
            minNeighbors=3,
            minSize=(5, 5)
        )
 
        for (x, y, w, h) in faces:
            cv.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 2)
        self.decode_and_show_dst(src)

 項目鏈接: https://pan.baidu.com/s/1z35PUbz0rROO3nvZIsYjVg     提取碼: 5zf3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章