资源简介

利用Python集成了20多常用的图像处理方法,包括二值化、边缘检测、开闭运算、高斯模糊、直方图等操作。仅需要读取图片便能运行,可在Python3环境下直接运行,无需调整。

资源截图

代码片段和文件信息

import PySpin
import os
import cv2
import numpy as np
import tkinter as tk
import matplotlib.pyplot as plt
from PIL import ImageImageTk
from tkinter.filedialog import *


def acquire_images():
    global file_path
    file_path = tk.filedialog.askopenfilename()
    print(file_path)
    # img = cv2.imread(file_path)
    # area1.create_image(400 400 image = img)

# 二值化处理图像
def Threshold():
    image = cv2.imread(file_path)
    gray = cv2.cvtColor(image cv2.COLOR_BGR2GRAY)

    #此为全局阈值
    #BINARY是执行二值化操作,后面的OTSU是具体执行的二值化阈值方法,可以选择其他
    #更改0255可以指定阈值
    ret binary = cv2.threshold(gray 0 255 cv2.THRESH_BINARY | cv2.THRESH_OTSU)

    #使用局部阈值25为blocksize参数,必须为奇数,10为每块局部设置的参考值,每块像素块的其他值减去均值大于10,
    #则执行置0或者置255操作
    # binary = cv2.adaptiveThreshold(gray 255 cv2.ADAPTIVE_THRESH_GAUSSIAN_C cv2.THRESH_BINARY 2510)

    #显示计算出来的阈值
    print(‘threshold value is : %s‘ %ret)
    #控制显示窗口大小
    cv2.namedWindow(‘Threshold‘ 0)
    cv2.resizeWindow(‘Threshold‘ 600 600)
    cv2.imshow(‘Threshold‘ binary)

    # ret thresh = cv2.threshold(image 0 255 cv2.THRESH_OTSU)
    # plt.imshow(image ‘gray‘)
    # plt.title(‘OTSU‘)
    # plt.xticks([])plt.yticks([])
    # plt.show()

#模糊操作,利用卷积原理
def blur():
    image = cv2.imread(file_path)

    #此为均值模糊
    #(301)为一维卷积核,指在x,y方向偏移多少位
    # dst = cv2.blur(image (30 1))

    #此为中值模糊,常用于去除椒盐噪声
    # dst = cv2.medianBlur(image 15)

    #自定义卷积核,执行模糊操作,也可定义执行锐化操作
    #定义卷积核kernel为5*5的单位矩阵,因为像素值为0-255,所以除以25防止溢出,25为矩阵内元素个数
    # kernel = np.ones([1010] np.float32)/100
    kernel = np.array([[0 -1 0] [-1 5 -1] [0 -1 0]] np.float32)
    dst = cv2.filter2D(image -1 kernel=kernel)

    cv2.namedWindow(‘Blur‘ 0)
    cv2.resizeWindow(‘Blur‘ 600 600)
    cv2.imshow(‘Blur‘ dst)
    cv2.namedWindow(‘input_image‘ 0)
    cv2.resizeWindow(‘input_image‘ 600 600)
    cv2.imshow(‘input_image‘ image)

def clamp(pv):
    if pv > 255:
        return 255
    elif pv < 0:
        return 0
    else:
        return pv

#给图片增加高斯噪声,计算花费很长时间
def gaussian_noise():
    image = cv2.imread(file_path)
    h w c = image.shape
    for row in range(h):
        for col in range(w):
            s = np.random.normal(0 20 3)
            b = image[row col 0]  #blue
            g = image[row col 1]  #green
            r = image[row col 2]  #red
            image[row col 0] = clamp(b + s[0])
            image[row col 1] = clamp(g + s[1])
            image[row col 2] = clamp(r + s[2])

    cv2.namedWindow(‘Gaussian_noise‘ 0)
    cv2.resizeWindow(‘Gaussian_noise‘ 600 600)
    cv2.imshow(‘Gaussian_noise‘ image)

#高斯模糊,能保留图片的主要特征
def gaussian_blur():
    image = cv2.imread(file_path)
    dst = cv2.GaussianBlur(image (0 0) 15)

    cv2.namedWindow(‘Gaussian_blur‘ 0)
    cv2.resizeWindow(‘Gaussian_blur‘ 600 600)
    cv2.imshow(‘Gaussian_blur‘ dst)

#边缘保留滤波
def epf():
    image = cv2.imread(file_pat

评论

共有 条评论