数字图像处理——自定义算法实现一些图像的操作

一,图像相减

        本文中仅有单通道的灰度图像的相减。

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @author JourWon
# @date 2023/11/29
# @file 图像相减.py

import cv2
import numpy as np
from matplotlib import pyplot as plt
s = ""

class tuxiangxiangjian(object):

    def duqu(self,image1_path,image2_path):

        self.image1 = cv2.imread(image1_path,cv2.IMREAD_GRAYSCALE)
        self.image2 = cv2.imread(image2_path,cv2.IMREAD_GRAYSCALE)  # 读取图像并分别存储为image1,image2

        if self.image1 is None:
            print("Error: Image1 not loaded")

        if self.image2 is None:
            print("Error: Image2 not loaded")  # 检查是否成功加载图像

    def xiangjian(self):
        x = []
        for i in range(0, len(self.image1)):
            y = []
            for l in range(0, len(self.image1[0])):
                z = (self.image1[i][l] - self.image2[i][l])
                y.append(z)
            x.append(y)
        self.result = np.asarray(x)

    def xianshi(self):
        fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
        axs[0].imshow(self.image1, cmap="gray")
        axs[0].set_title("image1")
        axs[1].imshow(self.image2, cmap="gray")
        axs[1].set_title("image2")
        axs[2].imshow(self.result, cmap="gray")
        axs[2].set_title("result")
        plt.show()

    def shibie(self):
        pass

    def save_image(self,save_path):
        # 保存图片
        cv2.imwrite(save_path, self.result)


g = tuxiangxiangjian()
g.duqu("img1.png","img2.png")
g.xiangjian()
g.save_image("xiangjianjieguo.png")
g.xianshi()

二,图像旋转

        此处只有顺时针和逆时针90°的旋转

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @author JourWon
# @date 2023/12/10
# @file 图像旋转.py
import cv2
import numpy as np
from matplotlib import pyplot as plt


class tuxiangxuanzhuan(object):

    def duqu(self,image_path):

        self.image = cv2.imread(image_path,cv2.IMREAD_GRAYSCALE)

        if self.image is None:
            print("Error: Image not loaded")

    def xuanzhuan(self,fangxiang):

        # 旋转方向: 0 逆时针旋转 ;1 顺时针旋转

        if fangxiang == 0:
            x = [[None] * (len(self.image)) for i in range(len(self.image[0]))]
            for i in range(0, len(self.image)):
                for l in range(0, len(self.image[0])):
                    x[len(self.image[0])-l-1][i] = self.image[i][l]

            self.result = np.asarray(x)

        elif fangxiang == 1:
            x = [[None] * (len(self.image)) for i in range(len(self.image[0]))]
            for i in range(0, len(self.image)):
                for l in range(0, len(self.image[0])):
                    x[l][len(self.image)-i-1] = self.image[i][l]

            self.result = np.asarray(x)

        else:
            print("Error:Unknown parameters")

    def xianshi(self):
        fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
        axs[0].imshow(self.image, cmap="gray")
        axs[0].set_title("image")

        axs[1].imshow(self.result, cmap="gray")
        axs[1].set_title("result")
        plt.show()


    def save_image(self,save_path):
        # 保存图片
        cv2.imwrite(save_path, self.result)

g = tuxiangxuanzhuan()
g.duqu("imgp.tif")
g.xuanzhuan(0)
g.xianshi()
g.xuanzhuan(1)
g.xianshi()
g.save_image("xuanzhuanjieguo.tif")

三,图像增强

        采用了三种增强方法:直方图均衡化,中值滤波,均值滤波

        其中直方图均衡化方法中,可以自定义均衡后的宽度(1~255),1表示单色图像,2表示极致图像,255表示原图像。

        中值滤波和均值滤波的核都是口字形的,可自定义大小,1代表周围8个像素点,2代表周围和周围的周围共24个像素点。

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @author JourWon
# @date 2023/12/11
# @file 图像增强.py
import cv2
import numpy as np
from matplotlib import pyplot as plt


class tuxiangzengqiang(object):

    def duqu(self, image_path):

        self.image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

        if self.image is None:
            print("Error: Image not loaded")


    def zhifangtujunhenghua(self,weight):
        x = [0] * 256
        y = [0] * 256
        size = len(self.image) * len(self.image[0])/weight

        for i in range(0, len(self.image)):
            for l in range(0, len(self.image[0])):
                x[self.image[i][l]] += 1

        self.zhifangtu = np.asarray(x)

        tok = 0
        for i in range(0, 256):
            tok = tok + self.zhifangtu[i]
            if tok >= size:
                y[i] = 1
                tok = 0
            else:
                continue
        y[255] = 1


        k = []
        for i in range(0, len(self.image)):
            kx = []
            for l in range(0, len(self.image[0])):
                for n in range(self.image[i][l],256):
                    if y[n] == 1:
                        kx.append(n)
                        break
                    else:
                        continue
            k.append(kx)

        self.result = np.asarray(k)

        result_x = [0] * 256
        for i in range(0, len(self.result)):
            for l in range(0, len(self.result[0])):
                result_x[self.result[i][l]] += 1

        self.result_zhifangtu = np.asarray(result_x)


    def zhongzhilvbo(self,size):

        x = []
        for i in range(0, len(self.image)):
            y = []
            for l in range(0, len(self.image[0])):
                if i < size or i >= len(self.image) - 1 - size or l < size or l >= len(self.image[0]) - 1 - size:
                    y.append(self.image[i][l])
                else:
                    y.append(self.zhongzhi(size,i,l))

            x.append(y)
        self.result = np.asarray(x)


    def zhongzhi(self,size,x,y):
        k = []
        for i in range(0,2 * size + 1):
            for l in range(0,2 * size + 1):
                k.append(self.image[x-size+i][y-size+l])

        if len(k)%2 == 0:
            lon = len(k)/2
        else:
            lon = (len(k)+1)/2


        for i in range(0, int(lon)):
            min = k[i]
            lab = i
            for l in range(i, len(k)):
                if k[l] < min:
                    min = k[l]
                    lab = l
            tx = k[i]
            k[i] = min
            k[lab] = tx
        return min


    def junzhilvbo(self,size):
        x = []
        for i in range(0, len(self.image)):
            y = []
            for l in range(0, len(self.image[0])):
                if i < size or i >= len(self.image) - 1 - size or l < size or l >= len(self.image[0]) - 1 - size:
                    y.append(self.image[i][l])
                else:
                    y.append(self.junzhi(size, i, l))

            x.append(y)
        self.result = np.asarray(x)

    def junzhi(self, size, x, y):
        k = 0
        for i in range(0,2 * size + 1):
            for l in range(0, 2 * size + 1):
                if i == x and l == y:
                    continue
                else:
                    k += self.image[x-size+i][y-size+l]

        return int(k/((2*size+1)*(2*size+1)-1))


    def xianshizhifangtu(self):

        # 创建一个新的图形
        fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(20, 20))

        # 原图像
        ax1.imshow(self.image, cmap='gray')
        ax1.set_title('Original image')

        # 原图像的直方图
        ax2.bar(range(256), self.zhifangtu)
        ax2.set_xlabel('Index')
        ax2.set_ylabel('Value')
        ax2.set_title('Histogram of the original image')

        # 均衡化后的图像
        ax3.imshow(self.result, cmap='gray')
        ax3.set_title('Equalized image')

        # 均衡化后的直方图
        ax4.bar(range(256), self.result_zhifangtu)
        ax4.set_xlabel('Index')
        ax4.set_ylabel('Value')
        ax4.set_title('Histogram after equalization')

        # 显示图形
        plt.show()

    def xianshizhongzhi(self):

        # 创建一个新的图形
        fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))

        # 原图像
        ax1.imshow(self.image, cmap='gray')
        ax1.set_title('Original image')

        # 中值滤波后的图像
        ax2.imshow(self.result, cmap='gray')
        ax2.set_title('Median filtered image')



        # 显示图形
        plt.show()

    def xianshijunzhi(self):

        # 创建一个新的图形
        fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))

        # 原图像
        ax1.imshow(self.image, cmap='gray')
        ax1.set_title('Original image')

        # 中值滤波后的图像
        ax2.imshow(self.result, cmap='gray')
        ax2.set_title('Image after the mean is filtered')

        # 显示图形
        plt.show()

    def save_image(self,save_path):
        # 保存图片
        cv2.imwrite(save_path, self.result)

gg = tuxiangzengqiang()
gg.duqu('img4.png')

gg.junzhilvbo(2)
gg.xianshijunzhi()

gg.zhongzhilvbo(2)
gg.xianshizhongzhi()


gg.zhifangtujunhenghua(8)
gg.xianshizhifangtu()






四,大津法图像分割

        这个分P不过是为了完成老师的任务罢了,不过改了路径还是可以直接运行的

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @author JourWon
# @date 2023/12/11
# @file 大津法.py
import numpy as np
import matplotlib.pyplot as plt
import cv2
def otsu_threshold(image):
    # 计算图像直方图
    pixel_counts, bin_edges = np.histogram(image.flatten(), bins=256, range=[0, 256])

    # 归一化直方图,使其成为像素的概率密度函数(pdf)
    pdf = pixel_counts / np.sum(pixel_counts)

    # 累积概率密度函数(cdf)
    cdf = pdf.cumsum()

    # 累积均值
    cum_mean = np.cumsum(pdf * np.arange(256))

    # 整个图像的全局均值
    global_mean = cum_mean[-1]

    # 初始化最大类间方差和其对应的阈值
    best_variance = 0
    best_threshold = 0

    # 遍历所有可能的阈值来最大化类间方差
    for t in range(256):
        # 前景类的概率和均值
        prob_foreground = cdf[t]
        mean_foreground = cum_mean[t]

        # 背景类的概率和均值
        prob_background = 1 - prob_foreground
        mean_background = (global_mean - mean_foreground) / prob_background if prob_background > 0 else 0

        # 计算类间方差
        variance_between = prob_foreground * prob_background * (mean_foreground - mean_background) ** 2

        # 更新最佳阈值
        if variance_between > best_variance:
            best_variance = variance_between
            best_threshold = t

    return best_threshold

# 读取图像并转换为灰度
image = cv2.imread('imgp.tif', cv2.IMREAD_GRAYSCALE)

# 获得通过大津算法确定的阈值
threshold_value = otsu_threshold(image)

# 使用阈值来生成二值图像
binary_image = np.zeros_like(image)
binary_image[image > threshold_value] = 255

# 绘制结果
plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(binary_image, cmap='gray')
plt.title('Otsu Thresholding')
plt.axis('off')

plt.show()

五,自适应阈值的图像分割

        仍然是为了完成老师的任务罢了,同上

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @author JourWon
# @date 2023/12/11
# @file 自适应阈值.py
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt


def local_threshold(image, block_size, C):
    # 图像的高度和宽度
    height, width = image.shape

    # 创建一个空数组(或者是零数组)来存储局部阈值图像
    threshold_image = np.zeros((height, width), dtype=np.uint8)

    # Padded image for edge block processing
    padded_image = np.pad(image, block_size // 2, mode='reflect')

    for y in range(0, height):
        for x in range(0, width):
            # 找到当前块的局部均值
            block = padded_image[y:y + block_size, x:x + block_size]
            local_mean = np.mean(block)

            # 比较当前像素与局部均值-C的关系
            if image[y, x] > local_mean - C:
                threshold_image[y, x] = 255
            else:
                threshold_image[y, x] = 0

    return threshold_image


# Load image and convert to grayscale
image_path = 'imgp.tif'
image = Image.open(image_path).convert('L')
image = np.array(image)

# Apply local threshold
block_size = 15  # Block size for local thresholding
C = 10  # Constant subtracted from the mean to calculate local threshold
binary_image = local_threshold(image, block_size, C)

# Display the result using matplotlib
plt.imshow(binary_image, cmap='gray')
plt.title('Binary Image')
plt.axis('off')  # 关闭坐标轴
plt.show()



六,整体GUI

        界面绘制使用了pyqt5,整合了图像相减,图像旋转,图像增强。

        其中左边是显示区域,右边是操作区域。

        在右边进行对图像的上传或运算操作之后,上传结果和运算结果均会显示到左边。

        需要注意的是,按钮”上传被减数图像“所上传的图像被所有方法读取,即后续图像增强,图像旋转都是操作的这个图像,而按钮”上传减数图像“仅会被图像相减读取。

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @author JourWon
# @date 2023/12/11
# @file Qt.py
import cv2
import numpy as np
from matplotlib import pyplot as plt

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QFileDialog
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap

class tuxiangzengqiang(object):

    def duqu(self, image_path):

        self.image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

        if self.image is None:
            print("Error: Image not loaded")


    def zhifangtujunhenghua(self,weight):
        x = [0] * 256
        y = [0] * 256
        size = len(self.image) * len(self.image[0])/weight

        for i in range(0, len(self.image)):
            for l in range(0, len(self.image[0])):
                x[self.image[i][l]] += 1

        self.zhifangtu = np.asarray(x)

        tok = 0
        for i in range(0, 256):
            tok = tok + self.zhifangtu[i]
            if tok >= size:
                y[i] = 1
                tok = 0
            else:
                continue
        y[255] = 1


        k = []
        for i in range(0, len(self.image)):
            kx = []
            for l in range(0, len(self.image[0])):
                for n in range(self.image[i][l],256):
                    if y[n] == 1:
                        kx.append(n)
                        break
                    else:
                        continue
            k.append(kx)

        self.result = np.asarray(k)

        result_x = [0] * 256
        for i in range(0, len(self.result)):
            for l in range(0, len(self.result[0])):
                result_x[self.result[i][l]] += 1

        self.result_zhifangtu = np.asarray(result_x)


    def zhongzhilvbo(self,size):

        x = []
        for i in range(0, len(self.image)):
            y = []
            for l in range(0, len(self.image[0])):
                if i < size or i >= len(self.image) - 1 - size or l < size or l >= len(self.image[0]) - 1 - size:
                    y.append(self.image[i][l])
                else:
                    y.append(self.zhongzhi(size,i,l))

            x.append(y)
        self.result = np.asarray(x)


    def zhongzhi(self,size,x,y):
        k = []
        for i in range(0,2 * size + 1):
            for l in range(0,2 * size + 1):
                k.append(self.image[x-size+i][y-size+l])

        if len(k)%2 == 0:
            lon = len(k)/2
        else:
            lon = (len(k)+1)/2


        for i in range(0, int(lon)):
            min = k[i]
            lab = i
            for l in range(i, len(k)):
                if k[l] < min:
                    min = k[l]
                    lab = l
            tx = k[i]
            k[i] = min
            k[lab] = tx
        return min


    def junzhilvbo(self,size):
        x = []
        for i in range(0, len(self.image)):
            y = []
            for l in range(0, len(self.image[0])):
                if i < size or i >= len(self.image) - 1 - size or l < size or l >= len(self.image[0]) - 1 - size:
                    y.append(self.image[i][l])
                else:
                    y.append(self.junzhi(size, i, l))

            x.append(y)
        self.result = np.asarray(x)

    def junzhi(self, size, x, y):
        k = 0
        for i in range(0,2 * size + 1):
            for l in range(0, 2 * size + 1):
                if i == x and l == y:
                    continue
                else:
                    k += self.image[x-size+i][y-size+l]

        return int(k/((2*size+1)*(2*size+1)-1))


    def xianshizhifangtu(self):

        # 创建一个新的图形
        fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(20, 20))

        # 原图像
        ax1.imshow(self.image, cmap='gray')
        ax1.set_title('Original image')

        # 原图像的直方图
        ax2.bar(range(256), self.zhifangtu)
        ax2.set_xlabel('Index')
        ax2.set_ylabel('Value')
        ax2.set_title('Histogram of the original image')

        # 均衡化后的图像
        ax3.imshow(self.result, cmap='gray')
        ax3.set_title('Equalized image')

        # 均衡化后的直方图
        ax4.bar(range(256), self.result_zhifangtu)
        ax4.set_xlabel('Index')
        ax4.set_ylabel('Value')
        ax4.set_title('Histogram after equalization')

        # 显示图形
        plt.show()

    def xianshizhongzhi(self):

        # 创建一个新的图形
        fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))

        # 原图像
        ax1.imshow(self.image, cmap='gray')
        ax1.set_title('Original image')

        # 中值滤波后的图像
        ax2.imshow(self.result, cmap='gray')
        ax2.set_title('Median filtered image')



        # 显示图形
        plt.show()

    def xianshijunzhi(self):

        # 创建一个新的图形
        fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))

        # 原图像
        ax1.imshow(self.image, cmap='gray')
        ax1.set_title('Original image')

        # 中值滤波后的图像
        ax2.imshow(self.result, cmap='gray')
        ax2.set_title('Image after the mean is filtered')

        # 显示图形
        plt.show()

    def save_image(self,save_path):
        # 保存图片
        cv2.imwrite(save_path, self.result)

class tuxiangxuanzhuan(object):

    def duqu(self,image_path):

        self.image = cv2.imread(image_path,cv2.IMREAD_GRAYSCALE)

        if self.image is None:
            print("Error: Image not loaded")

    def xuanzhuan(self,fangxiang):

        # 旋转方向: 0 逆时针旋转 ;1 顺时针旋转

        if fangxiang == 0:
            x = [[None] * (len(self.image)) for i in range(len(self.image[0]))]
            for i in range(0, len(self.image)):
                for l in range(0, len(self.image[0])):
                    x[len(self.image[0])-l-1][i] = self.image[i][l]

            self.result = np.asarray(x)

        elif fangxiang == 1:
            x = [[None] * (len(self.image)) for i in range(len(self.image[0]))]
            for i in range(0, len(self.image)):
                for l in range(0, len(self.image[0])):
                    x[l][len(self.image)-i-1] = self.image[i][l]

            self.result = np.asarray(x)

        else:
            print("Error:Unknown parameters")

    def xianshi(self):
        fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
        axs[0].imshow(self.image, cmap="gray")
        axs[0].set_title("image")

        axs[1].imshow(self.result, cmap="gray")
        axs[1].set_title("result")
        plt.show()


    def save_image(self,save_path):
        # 保存图片
        cv2.imwrite(save_path, self.result)

class tuxiangxiangjian(object):

    def duqu(self,image_path):

        self.image = cv2.imread(image_path,cv2.IMREAD_GRAYSCALE) #读取图像并存储为image

        if self.image is None:
            print("Error: Image not loaded")



    def duqu1(self,image1_path):

        self.image1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE)  # 读取图像并存储为image1

        if self.image1 is None:
            print("Error: Image1 not loaded")  # 检查是否成功加载图像

    def xiangjian(self):
        x = []
        for i in range(0, len(self.image)):
            y = []
            for l in range(0, len(self.image[0])):
                z = (self.image[i][l] - self.image1[i][l])
                y.append(z)
            x.append(y)
        self.result = np.asarray(x)

    def xianshi(self):
        fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
        axs[0].imshow(self.image, cmap="gray")
        axs[0].set_title("image")
        axs[1].imshow(self.image1, cmap="gray")
        axs[1].set_title("image1")
        axs[2].imshow(self.result, cmap="gray")
        axs[2].set_title("result")
        plt.show()

    def shibie(self):
        pass

    def save_image(self,save_path):
        # 保存图片
        cv2.imwrite(save_path, self.result)




class ImageProcessingApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("图像处理应用")  # 设置窗口标题
        self.setGeometry(100, 100, 600, 400)  # 设置窗口大小和位置

        self.image_label = QLabel(self)  # 创建一个标签用于显示图像
        self.image_label.setGeometry(20, 20, 300, 300)  # 设置标签的位置和大小

        # 创建各个功能按钮,并连接对应的槽函数
        self.upload_button1 = QPushButton("上传被减数图像", self)
        self.upload_button1.setGeometry(350, 30, 150, 30)
        self.upload_button1.clicked.connect(self.upload_minuend_image)

        self.upload_button2 = QPushButton("上传减数图像", self)
        self.upload_button2.setGeometry(350, 70, 150, 30)
        self.upload_button2.clicked.connect(self.upload_subtrahend_image)

        self.subtract_button = QPushButton("相减", self)
        self.subtract_button.setGeometry(350, 110, 150, 30)
        self.subtract_button.clicked.connect(self.subtract_images)

        self.rotate_clockwise_button = QPushButton("顺时针旋转", self)
        self.rotate_clockwise_button.setGeometry(350, 150, 150, 30)
        self.rotate_clockwise_button.clicked.connect(self.rotate_image_clockwise)

        self.rotate_counterclockwise_button = QPushButton("逆时针旋转", self)
        self.rotate_counterclockwise_button.setGeometry(350, 190, 150, 30)
        self.rotate_counterclockwise_button.clicked.connect(self.rotate_image_counterclockwise)

        self.histogram_equalization_button = QPushButton("直方图均衡化", self)
        self.histogram_equalization_button.setGeometry(350, 230, 150, 30)
        self.histogram_equalization_button.clicked.connect(self.histogram_equalization)

        self.median_filter_button = QPushButton("中值滤波", self)
        self.median_filter_button.setGeometry(350, 270, 150, 30)
        self.median_filter_button.clicked.connect(self.median_filter)

        self.mean_filter_button = QPushButton("均值滤波", self)
        self.mean_filter_button.setGeometry(350, 310, 150, 30)
        self.mean_filter_button.clicked.connect(self.mean_filter)

        self.save_button = QPushButton("保存图像", self)
        self.save_button.setGeometry(350, 350, 150, 30)
        self.save_button.clicked.connect(self.save_image)

        self.xiangjian_class = tuxiangxiangjian()
        self.xuanzhuan_class = tuxiangxuanzhuan()
        self.zhengqiang_class = tuxiangzengqiang()

    # 上传被减数图像
    def upload_minuend_image(self):
        file_dialog = QFileDialog()  # 创建文件对话框
        file_path, _ = file_dialog.getOpenFileName(self, "上传被减数图像")  # 获取被减数图像的文件路径
        file_path = file_path[-8:]
        self.xiangjian_class.duqu(file_path)
        self.xuanzhuan_class.duqu(file_path)
        self.zhengqiang_class.duqu(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像

    #上传减数图像
    def upload_subtrahend_image(self):
        file_dialog = QFileDialog()  # 创建文件对话框
        file_path, _ = file_dialog.getOpenFileName(self, "上传减数图像")  # 获取减数图像的文件路径
        file_path = file_path[-8:]
        self.xiangjian_class.duqu1(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像

    #相减
    def subtract_images(self):
        self.xiangjian_class.xiangjian()
        self.xiangjian_class.xianshi()
        file_path = "xiangjianjieguo.png"
        self.xiangjian_class.save_image(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像
        self.result = self.xiangjian_class.result

    #顺时针旋转
    def rotate_image_clockwise(self):
        self.xuanzhuan_class.xuanzhuan(1)
        self.xuanzhuan_class.xianshi()
        file_path = "xuanzhuanjieguo.tif"
        self.xuanzhuan_class.save_image(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像
        self.xuanzhuan_class.image = self.xuanzhuan_class.result
        self.result = self.xuanzhuan_class.result

    #逆时针旋转
    def rotate_image_counterclockwise(self):
        self.xuanzhuan_class.xuanzhuan(0)
        self.xuanzhuan_class.xianshi()
        file_path = "xuanzhuanjieguo.tif"
        self.xuanzhuan_class.save_image(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像
        self.xuanzhuan_class.image = self.xuanzhuan_class.result
        self.result = self.xuanzhuan_class.result

    #直方图均衡化
    def histogram_equalization(self):
        self.zhengqiang_class.zhifangtujunhenghua(16)
        self.zhengqiang_class.xianshizhifangtu()
        file_path = "zhifangtujunhenghuajieguo.tif"
        self.zhengqiang_class.save_image(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像
        self.result = self.zhengqiang_class.result


    #中值滤波
    def median_filter(self):
        self.zhengqiang_class.zhongzhilvbo(1)
        self.zhengqiang_class.xianshizhongzhi()
        file_path = "zhongzhilvbojieguo.tif"
        self.zhengqiang_class.save_image(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像
        self.result = self.zhengqiang_class.result

    #均值滤波
    def mean_filter(self):
        self.zhengqiang_class.junzhilvbo(1)
        self.zhengqiang_class.xianshijunzhi()
        file_path = "junzhilvbojieguo.tif"
        self.zhengqiang_class.save_image(file_path)
        pixmap = QPixmap(file_path)  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像
        self.result = self.zhengqiang_class.result

    #保存图像
    def save_image(self):
        file_dialog = QFileDialog()  # 创建文件对话框
        save_path, _ = file_dialog.getSaveFileName(self, "保存图像")  # 获取保存图像的文件路径
        cv2.imwrite(save_path, self.minuend_image)  # 保存图像

    def display_image(self, image):
        height, width, channel = image.shape  # 获取图像的高度、宽度和通道数
        bytes_per_line = 3 * width  # 计算每行的字节数
        q_image = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888)  # 创建一个QImage对象用于显示图像
        pixmap = QPixmap.fromImage(q_image.rgbSwapped())  # 创建一个Pixmap对象用于显示图像
        self.image_label.setPixmap(pixmap.scaled(300, 300))  # 在标签上显示图像


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = ImageProcessingApp()  # 创建应用窗口
    window.show()  # 显示窗口
    sys.exit(app.exec_())  # 运行应用事件循环,等待用户交互和响应事件

七,尾言

        作者水平十分有限,如有错误,恳请斧正。

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值