模糊计算——边缘检测

         图像的边缘是两个均匀区域之间的边界,我们可以通过比较邻近像素的强度来检测边缘。图像处理的模糊逻辑方法允许我们使用隶属函数来定义像素属于边缘或均匀区域的程度。

一、实验原理

1、首先,用加权平均法模糊推理图像中任意一像素点的边缘隶属方向,取其最大隶属度的方向为边缘隶属方向。

2、然后,在边缘隶属方向上根据像素点附近灰度分布的特点模糊推理该点的边缘隶属度,进而实现边缘检测。

二、实验过程

1、确定隶属度函数

 

 2.输入变量模糊化

对于每个输入,利用高斯隶属函数将该点的梯度值映射到0-1之间。

3.模糊算子(ANDOR)在先行词中的应用

如果前提条件不只一个,则应用模糊逻辑运算,将前提解析为01之间的隶属度值。

4.模糊算子(ANDOR)在先行词中的应用

使用整个规则的支持度来塑造输出模糊集。模糊规则的结果将整个模糊集分配给输出。该模糊集由隶属函数表示,该隶属函数被选择以指示后件的质量。如果先行词仅部分为真,(即赋值小于1),则根据蕴涵方法截断输出模糊集。

5.所有规则的结果汇总

得到所有规则的结果汇总后的模糊集。

6.去模糊化

图像进行去模糊化,即可得到最终的输出值。

 

 

 

import numpy as np
from PIL import Image
import math
import os
import sys

import cv2
import numpy as np
import matplotlib.pyplot as plt
from PyQt5.QtGui import QPixmap, QImage, QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QComboBox, QLabel, QPlainTextEdit
# 定义灰度差矩阵
def blurring(arr):
    for i in range(3):
        for j in range(3):
            if arr[i][j] <= 0.6 and arr[i][j] >=  -0.6:
                arr[i][j] = math.exp(-20 * arr[i][j]*arr[i][j])
            else:
                arr[i][j] = 0
    print(arr[0,1])
    w1 = min(arr[0,1], arr[1][2])
    w2 = min(arr[1,2], arr[2][1])
    w3 = min(arr[2,1], arr[1][0])
    w4 = min(arr[1,0], arr[0][1])
    B = min(min(1 - w1, 1 - w2), min(1 - w3, 1 - w4))
    return (w1,w2,w3,w4,B)

# 图片导入与处理




class Qt_Window(QMainWindow):
    def __init__(self):
        super().__init__()

    def init_ui(self):
        self.path = ("detection-result/")
        self.img_list = os.listdir(self.path)

        self.window = QMainWindow()
        self.window.resize(1500, 880)
        self.window.move(35, 60)
        self.window.setWindowTitle('边缘检测')
        self.add_Button = QPushButton('添加图片', self.window)
        self.add_Button.resize(180, 40)
        self.add_Button.move(350, 30)
        self.comboBox = QComboBox(self.window)
        self.comboBox.resize(250, 40)
        self.comboBox.move(800,30)
        self.comboBox.addItems([self.img_list[i] for i in range(len(self.img_list))])

        self.detect_image = QLabel(self.window)
        self.add_Button.clicked.connect(self.show_img)
        self.detect_image.resize(400, 300)
        self.detect_image.move(50, 200)
        self.result_image = QLabel(self.window)
        self.result_image.resize(400, 300)
        self.result_image.move(500, 200)
        self.add_Button.clicked.connect(self.show_result_img)

        self.final_image = QLabel(self.window)
        self.final_image.resize(400, 300)
        self.final_image.move(950, 200)
        # self.add_Button.clicked.connect(self.show_final_img)



        # 标签
        self.orign = QPlainTextEdit(self.window)
        self.orign.setReadOnly(True)
        self.orign.setPlaceholderText("原始图像")
        self.orign.setFont(QFont("宋体", 13))
        self.orign.resize(120, 40)
        self.orign.move(190, 120)

        self.result = QPlainTextEdit(self.window)
        self.result.setReadOnly(True)
        self.result.setPlaceholderText("灰度图像")
        self.result.setFont(QFont("宋体", 13))
        self.result.resize(120, 40)
        self.result.move(640, 120)

        self.standard = QPlainTextEdit(self.window)
        self.standard.setReadOnly(True)
        self.standard.setPlaceholderText("结果图像")
        self.standard.setFont(QFont("宋体", 13))
        self.standard.resize(120, 40)
        self.standard.move(1090, 120)



        self.window.show()


    def show_img(self):
        img = self.comboBox.currentText()
        pix = QPixmap(self.path + "\\" + img)
        self.detect_image.setPixmap(pix)
        self.detect_image.setScaledContents(True)
        # lable = img.split(".")[0]
        # self.number_lable.setText(lable)

    def show_result_img(self):
        img=self.comboBox.currentText()
        img1: Image.Image = Image.open(self.path + "\\" + img)

        img1 = img1.convert("RGB")
        np_array = np.array(img1)

        col = img1.size[0]
        row = img1.size[1]
        print(row, col)
        # print(np_array)

        # 灰度定义
        GrayArray = []
        GrayArrayMax = []
        GrayArrayRed = []
        GrayArrayGreen = []
        GrayArrayBlue = []

        for itemRow in np_array:
            for itemCol in itemRow:
                GrayArray.append(int(((itemCol[0]) * 0.299 + (itemCol[1]) * 0.587 + (itemCol[2]) * 0.114) / 3))
                GrayArrayMax.append(max(itemCol[0], itemCol[1], itemCol[2]))
                GrayArrayRed.append(itemCol[0])
                GrayArrayGreen.append(itemCol[1])
                GrayArrayBlue.append(itemCol[2])

        # print(GrayArray)

        # 选取灰度算法
        np_GrayArrayMax = np.array(GrayArray).reshape(row, col)
        print(np_GrayArrayMax)
        img = Image.fromarray(np_GrayArrayMax).convert("L")  # 实现array到Image的转换
        print(111111111111111)
        qimg=img.toqpixmap()
        pix=QPixmap(qimg)
        self.result_image.setPixmap(pix)
        self.result_image.setScaledContents(True)

        # 数组中最大最小值
        Min = np.min(np_GrayArrayMax)
        Max = np.max(np_GrayArrayMax)

        # 创建归一化数组
        np_EdgeArrayMax = np.zeros((row + 2, col + 2), dtype=float)
        # 数组填充
        for i in range(row):
            for j in range(col):
                np_EdgeArrayMax[i + 1][j + 1] = np_GrayArrayMax[i][j]

        # 展示填充效果
        img = Image.fromarray(np_EdgeArrayMax).convert("L")

        # 数组归一化
        for i in range(row + 2):
            for j in range(col + 2):
                np_EdgeArrayMax[i][j] = (np_EdgeArrayMax[i][j] - 0) / (Max - 0)

        # 创建小矩阵
        ProcessArrayMax = np.zeros((3, 3), dtype=float)
        # 创建处理后的矩阵
        DealArrayMax = np.zeros((row + 2, col + 2), dtype=float)

        for i in range(2, row + 1):
            for j in range(2, col + 1):
                ProcessArrayMax[0][0] = np_EdgeArrayMax[i - 1][j - 1] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[0][1] = np_EdgeArrayMax[i - 1][j] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[0][2] = np_EdgeArrayMax[i - 1][j + 1] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[1][0] = np_EdgeArrayMax[i][j - 1] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[1][1] = np_EdgeArrayMax[i][j] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[1][2] = np_EdgeArrayMax[i][j + 1] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[2][0] = np_EdgeArrayMax[i + 1][j - 1] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[2][1] = np_EdgeArrayMax[i + 1][j] - np_EdgeArrayMax[i][j]
                ProcessArrayMax[2][2] = np_EdgeArrayMax[i + 1][j + 1] - np_EdgeArrayMax[i][j]
                W1, W2, W3, W4, B = blurring(ProcessArrayMax)
                V1 = 0.8 * W1 + 0.2
                V2 = 0.8 * W2 + 0.2
                V3 = 0.8 * W3 + 0.2
                V4 = 0.8 * W4 + 0.2
                V5 = 0.8 - (0.8 * B)
                DealArrayMax[i][j] = ((W1 * V1) + (W2 * V2) + (W3 * V3) + (W4 * V4) + (B * V5)) / (
                            W1 + W2 + W3 + W4 + B)

        print(DealArrayMax)

        for i in range(row + 2):
            for j in range(col + 2):
                DealArrayMax[i][j] = DealArrayMax[i][j] * 255
        img = Image.fromarray(DealArrayMax).convert("L")
        qtimg=img.toqpixmap()
        pix = QPixmap(qtimg)
        self.final_image.setPixmap(pix)
        self.final_image.setScaledContents(True)




if __name__ == "__main__":
    # 图形用户界面
    app = QApplication([])
    s = Qt_Window()
    s.init_ui()
    app.exec_()
    # print(img_input)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九磅十五便士°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值