Opencv中开运算、闭运算、膨胀、腐蚀

该文详细介绍了图像处理中的开运算和闭运算过程,包括将图像转为灰度图,使用大津二值化分割前景和背景,以及腐蚀和膨胀操作。提供的Python代码展示了如何实现这两种运算,用于去除小像素块和连接中断的像素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开运算过程:
    1、将图像转化为灰度图。
    2、进行大津二值化,利用阈值将原图像分成前景,背景两个图象。
    3、开运算则是首先进行N次腐蚀,再进行N次膨胀。开运算可以用来去除仅存的小块像素。

闭运算过程:
    1、将图像转化为灰度图。
    2、进行大津二值化,利用阈值将原图像分成前景,背景两个图象。
    3、闭运算则是先进行N次膨胀,再进行N次腐蚀, 闭运算能够将中断的像素连接起来。

  (1)膨胀:如果结果大于255的话,将中心像素设为255 (2)腐蚀:结果小于255×4的话,将中心像素设为0。

实现代码:

import cv2
import numpy as np


class Open_Close:
    def __init__(self, img, Dila_times=1, th=128):
        self.image = img
        self.Dila_time = Dila_times
        self.th = th

    def BGR2GRAY(self, img):
        b = img[:, :, 0].copy()
        g = img[:, :, 1].copy()
        r = img[:, :, 2].copy()

        out = r * 0.2126 + g * 0.7152 + b * 0.0722
        out = out.astype(np.uint8)
        return out

    def otsu_binarization(self, img, th=128):
        H, W = img.shape
        out = img.copy()

        max_sigma = 0
        max_t = 0

        for _t in range(1, 255):
            v0 = out[out < _t]
            m0 = np.mean(v0) if len(v0) > 0 else 0
            w0 = len(v0) / (H * W)
            v1 = out[out >= _t]
            m1 = np.mean(v1) if len(v1) > 0 else 0
            w1 = len(v1) / (H * W)
            sigma = w0 * w1 * ((m0 - m1) ** 2)
            if sigma > max_sigma:
                max_sigma = sigma
                max_t = _t

        print(f'Threshold:{max_t}')
        th = max_t
        out[out < th] = 0
        out[out >= th] = 255
        return out


    #膨胀具体操作
    def Morphology_Erode(self, img, Dil_times):
        H, W = img.shape
        out = img.copy()

        MF = np.array(((0, 1, 0),
               (1, 0, 1),
               (0, 1, 0)), dtype=np.int32)

        for i in range(Dil_times):
            tmp = np.pad(out, (1, 1), 'edge')
            for y in range(1, H+1):
                for x in range(1, W+1):
                    if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) > 255:
                        out[y-1, x-1] = 255

        return out


    #腐蚀具体操作
    def Morphology_Dilate(self, img, Dil_times):
        H, W = img.shape
        out = img.copy()

        MF = np.array(((0, 1, 0),
                       (1, 0, 1),
                       (0, 1, 0)), dtype=np.int32)

        for i in range(Dil_times):
            tmp = np.pad(out, (1, 1), 'edge')
            for y in range(1, H+1):
                for x in range(1, W+1):
                    if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4:
                        out[y-1, x-1] = 0
        return out


def open_c(img, s_times):

    open_r = Open_Close(img, s_times)
    img_gray = open_r.BGR2GRAY(image)
    out = open_r.otsu_binarization(img_gray)
    dilate_im = open_r.Morphology_Dilate(out, s_times)
    result_im = open_r.Morphology_Erode(dilate_im, s_times)
    cv2.imshow('Result_img', result_im)
    cv2.waitKey(0)
    cv2.destroyWindow()

def close_c(img, t_times):
    close_r = Open_Close(img, Dila_times=t_times)
    img_gray = close_r.BGR2GRAY(img)
    out = close_r.otsu_binarization(img_gray)
    erod = close_r.Morphology_Erode(out, t_times)
    result = close_r.Morphology_Dilate(erod, t_times)

    cv2.imshow('result_img', result)
    cv2.waitKey(0)
    cv2.destroyWindow()

if __name__ == "__main__":
    image = cv2.imread('img.png').astype(np.float32)
    times = 2
    #开运算
    open_c(image, times)

    #闭运算
    #close_c(image, times)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值