第三次opencv作业(边缘检测,二值图,骨架提取)

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import scipy.signal as signal
import cv2 as cv
import random
import easygui as g
import imutils
import time
import math

array = [0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
         1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,\
         0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
         1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,\
         1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
         1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1,\
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
         0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
         1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,\
         0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,\
         1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,\
         1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
         1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,\
         1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0,\
         1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
#映射表

def Morphology_Dilate(img, Dil_time=1):
    H, W = img.shape

    # kernel
    MF = np.array(((0, 1, 0),
                   (1, 0, 1),
                   (0, 1, 0)), dtype=np.int)

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

    return out

def Morphology_Erode(img, Erode_time=1):
    H, W = img.shape
    out = img.copy()

    # kernel
    MF = np.array(((0, 1, 0),
                   (1, 0, 1),
                   (0, 1, 0)), dtype=np.int)

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

    return out

def Thin(image, array):
    h, w = image.shape
    iThin = image.copy()

    for i in range(h):
        for j in range(w):
            if image[i, j] == 0:
                a = [1] * 9
                for k in range(3):
                    for l in range(3):
                        # 如果3*3矩阵的点不在边界且这些值为零,也就是黑色的点
                        if -1 < (i - 1 + k) < h and -1 < (j - 1 + l) < w and iThin[i - 1 + k, j - 1 + l] == 0:
                            a[k * 3 + l] = 0
                sum = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128
                iThin[i, j] = array[sum] * 255
    return iThin




# 定义像素点周围的8邻域
#                P9 P2 P3
#                P8 P1 P4
#                P7 P6 P5

def neighbours(x, y, image):
    img = image
    x_1, y_1, x1, y1 = x - 1, y - 1, x + 1, y + 1
    return [img[x_1][y], img[x_1][y1], img[x][y1], img[x1][y1],  # P2,P3,P4,P5
            img[x1][y], img[x1][y_1], img[x][y_1], img[x_1][y_1]]  # P6,P7,P8,P9


# 计算邻域像素从0变化到1的次数
def transitions(neighbours):
    n = neighbours + neighbours[0]  # P2,P3,...,P8,P9,P2
    return sum((n1, n2) == (0, 1) for n1, n2 in zip(neighbours, n[1:]))  # (P2,P3),(P3,P4),...,(P8,P9),(P9,P2)


# Zhang-Suen 细化算法
def zhangSuen(image):
    # image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    # image = image.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:

    # rows = image.shape[0]
    # cols = image.shape[1]
    # sum2 =[]
    # for i in range(rows):
    #     for j in range(cols):
    #         sum2.append(int(image[i, j]))
    # sum2.sort()
    # medium_number = sum2[len(sum2) // 2]  # 去中位数的灰度值
    # area = rows * cols
    # for i in range(rows):
    #     for j in range(cols):
    #         if int(image[i, j]) < medium_number:
    #             image[i, j] = 0
    #         else:
    #             image[i, j] = 1
    #
    # suanzi1 = np.fromfunction(func, (3, 3), sigma=2)
    # # Laplace扩展算子
    # laplace = np.array([[1, 1, 1],
    #                     [1, -8, 1],
    #                     [1, 1, 1]])
    #
    # image_laplace = convolution(image=image, kernal_size=suanzi1)      #进行高斯平滑后在进行我们的拉place变换
    # image_laplace = image_laplace * (255 /image_laplace.max())
    # image_laplace = convolution(image=image_laplace,kernal_size=laplace)


    Image_Thinned = image.copy()  # Making copy to protect original image
    Image_Thinned[Image_Thinned == 255] =1
    changing11 = changing22 = 1
    while changing11 or changing22:  # Iterates until no further changes occur in the image
        # Step 1
        changing1 = []
        rows, columns = Image_Thinned.shape
        for x in range(1, rows - 1):
            for y in range(1, columns - 1):
                P2, P3, P4, P5, P6, P7, P8, P9 = n = neighbours(x, y, Image_Thinned)
                if (Image_Thinned[x][y] == 1 and  # Condition 0: Point P1 in the object regions
                        2 <= sum(n) <= 6 and  # Condition 1: 2<= N(P1) <= 6   N(P1)为前景像素点的个数
                        transitions(n) == 1 and  # Condition 2: S(P1)=1        S(P1)为像素值从0到1的变化个数
                        P2 * P4 * P6 == 0 and  # Condition 3
                        P4 * P6 * P8 == 0):  # Condition 4
                    changing1.append((x, y))
        for x, y in changing1:
            Image_Thinned[x][y] = 0
        changing11 = 0
        # Step 2
        changing2 = []
        for x in range(1, rows - 1):
            for y in range(1, columns - 1):
                P2, P3, P4, P5, P6, P7, P8, P9 = n = neighbours(x, y, Image_Thinned)
                if (Image_Thinned[x][y] == 1 and  # Condition 0
                        2 <= sum(n) <= 6 and  # Condition 1         前景点的个数
                        transitions(n) == 1 and  # Condition 2      0到1的变换
                        P2 * P4 * P8 == 0 and  # Condition 3
                        P2 * P6 * P8 == 0):  # Condition 4
                    changing2.append((x, y))
        for x, y in changing2:
            Image_Thinned[x][y] = 0
        changing22 =0
    Image_Thinned[Image_Thinned ==1] =255
    # for i in range(3):
    #     Image_Thinned = cv.dilate(Image_Thinned,None)
    # Image_Thinned=Image_Thinned.astype(np.float32)
    #
    # dst = cv.cornerHarris(Image_Thinned, 2, 3, 0.04)
    # # result is dilated for marking the corners, not important
    # Gauss_img = cv.GaussianBlur(image, (3, 3), 0)
    # adap_binary = cv.adaptiveThreshold(Gauss_img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 55, 2)
    # iThin = Thin(adap_binary, array)
    # dst = cv.dilate(dst, None)
    # # image = image.reshape((Image_Thinned.shape[0],Image_Thinned.shape[1]))
    # # Threshold for an optimal value, it may vary depending on the image.
    # a=[]
    # for i in (dst > 0.05 * dst.max()):
    #     a.append(i)
    #     cv.rectangle(Image_Thinned,i)
    # #                                             # src:数据类型为float32的输入图像
    # #                                             # blockSize:角点检测中要考虑的领域大小
    # #                                             # ksize:Sobel求导中使用的窗口大小
    # #                                             # k:Harris
    # #                                             # 角点检测方程中的自由参数,取值参数为[0, 04,0.06].
    # #                                             # dst:目标图像
    # #                                             # borderType:边界类型
    Image_Thinned = Image_Thinned.astype(np.uint8)
    return Image_Thinned
    # cv.imshow('framework image',Image_Thinned)
    # # cv.imshow('1',iThin)
    # # cv.imshow('marked original image', image)
    # cv.waitKey(0)

def convolution(kernal_size, image):       #默认算子都是3*3
    rows=image.shape[0]
    cols = image.shape[1]
    gray_data = image.copy()
    img_new = []
    for i in range(rows-3):   #减去3的原因是自己定义的模板都是3*3
        line = []             #line记录着每一行的信息
        for j in range(cols-3):
            a = gray_data[i:i+3,j:j+3]
            a=np.array(a)
            number = np.sum(np.multiply(kernal_size, a))
            line.append(number)   #np.multiply表示两个矩阵点乘
        img_new.append(line)   #记录着每一行卷积后的结果
        img_new1 = np.array(img_new)
        img_new1 = img_new1 *(255.0/img_new1.max())
    return img_new1

def erosion(image):    #默认图片已经进行了二值化     #腐蚀操作
    image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    image = image.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:
    kernal_size = np.ones((3,3))
    rows = image.shape[0]
    cols = image.shape[1]
    gray_data = image.copy()
    img_new = []
    for i in range(rows - 3):  # 减去3的原因是自己定义的模板都是3*3
        line = []  # line记录着每一行的信息
        for j in range(cols - 3):
            a = gray_data[i:i + 3, j:j + 3]
            a = np.array(a)
            number = np.sum(np.multiply(kernal_size, a))
            if number == 255 * 9:
                gray_data[i,j] = 255
            else:
                gray_data[i,j] = 0
    return gray_data



def func1(image,x,y,pix_length):  #上下左右四个方向判断
    for i in range(x-1,x+2,1):
        for j in range(y-1,y+2,1):
            if (i,j) in pix_length:
                image[i,j] = 50
    if image[x,y] == image[x+1,y] and image[x,y-1]!=image[x,y+1] and image[x+1,y+1] !=image[x+1,y-1] :
        return True
    elif image[x,y] == image[x-1,y] and image[x,y-1]!=image[x,y+1] and image[x-1,y+1] !=image[x-1,y-1]:
        return True
    elif image[x,y] == image[x,y-1] and image[x-1,y-1]!=image[x+1,y-1] and image[x-1,y] !=image[x+1,y]:
        return True
    elif image[x,y+1] == image[x,y] and image[x-1,y]!=image[x+1,y] and image[x+1,y+1] !=image[x-1,y+1]:
        return True
    else:
        return False

def func2(image,x,y,pix_length):  #方向为斜对角形式
    for i in range(x-1,x+2,1):
        for j in range(y-1,y+2,1):
            if (i,j) in pix_length:
                image[i,j] = 50
    if image[x,y] == image[x+1,y-1] and image[x,y-1]!=image[x+1,y] :
        return True
    if image[x,y] == image[x-1,y-1] or image[x-1,y]!=image[x,y-1] :
        return True
    if image[x,y] == image[x-1,y+1] or image[x-1,y]!=image[x,y+1] :
        return True
    if image[x,y] == image[x+1,y+1] or image[x+1,y]!=image[x,y+1] :
        return True
    else:
        return False
def detection_region(image):
    image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    image = image.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:

    prewitt_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])  # 设置prewitt_x算子
    prewitt_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])  # 设置prewitt_y算子

    image_prewitt_x = convolution(image=image, kernal_size=prewitt_x)
    image_prewitt_y = convolution(image=image, kernal_size=prewitt_y)
    image_prewitt = np.sqrt(image_prewitt_x ** 2 + image_prewitt_y ** 2)
    image_prewitt = (255 / image_prewitt.max()) * image_prewitt
    image_prewitt[image_prewitt > image_prewitt.mean()] = 255
    image_prewitt[image_prewitt < image_prewitt.mean()] = 0

    rows = image_prewitt.shape[0]
    cols = image_prewitt.shape[1]
    # max = 0
    # min = 0
    # for i in range(rows):
    #     for j in range(cols):
    #         if image_prewitt[i, j] == 255:
    #             max += 1
    #         else:
    #             min += 1
    # g.msgbox('白色面积为{},黑色面积为{}'.format(max, min))

    Gauss_img = cv.GaussianBlur(image, (3, 3), 0)
    adap_binary = cv.adaptiveThreshold(Gauss_img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 55, 2)
    # max1 = 0
    # min1 = 0
    iThin = Thin(adap_binary, array)

    dst = cv.cornerHarris(iThin, 2, 3, 0.04)
    # result is dilated for marking the corners, not important
    dst = cv.dilate(dst, None)
    # Threshold for an optimal value, it may vary depending on the image.
    iThin[dst > 0.01 * dst.max()] = [255, 0, 0]
    #                                             # src:数据类型为float32的输入图像
    #                                             # blockSize:角点检测中要考虑的领域大小
    #                                             # ksize:Sobel求导中使用的窗口大小
    #                                             # k:Harris
    #                                             # 角点检测方程中的自由参数,取值参数为[0, 04,0.06].
    #                                             # dst:目标图像
    #                                             # borderType:边界类型
    # for i in range(iThin.shape[0]):
    #     for j in range(iThin.shape[1]):
    #         if iThin[i, j] == 255:
    #             max1 += 1
    # g.msgbox('边界长度为{}'.format(max1))

    # cv.imshow('binary image', image_prewitt)
    cv.imshow('detection image{}', iThin)
    cv.waitKey(0)

def func(x,y,sigma=1):
    return 100*(1/(2*np.pi*sigma))*np.exp(-((x-1)**2+(y-1)**2)/(2.0*sigma**2))   #创建高斯函数,该函数中心为(1,1),所以创建3*3比较合适

def gause_image(image):             #高斯滤波,第一步也实现了灰度化
    suanzi = np.fromfunction(func,(3,3),sigma=2)  # 创建高斯函数,(1,1)为函数的中心,这里是生成3*3的标准差为2的高斯算子
    # 打开图像并转化成灰度图像
    image2 = convolution(image=image, kernal_size=suanzi)
    # 将结果灰度值转化到0-255
    image2 = (image2 / float(image2.max())) * 255
    return image2
#
def adaptive_thres(img, win, beta=0.95):
    if win % 2 == 0:
        win = win - 1
    # 边界的均值有点麻烦
    # 这里分别计算和和邻居数再相除
    # img = img[:, :, 0] * 0.11 + img[:, :, 1] * 0.59 + img[:, :, 2] * 0.3
    # img = img.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:
    img = gause_image(img)
    kern = np.ones([win, win])
    sums = signal.correlate2d(img, kern, 'same')
    cnts = signal.correlate2d(np.ones_like(img), kern, 'same')
    means = sums // cnts
    # 如果直接采用均值作为阈值,背景会变花
    # 但是相邻背景颜色相差不大
    # 所以乘个系数把它们过滤掉
    img = np.where(img < means * beta, 0, 255)
    img = img.astype(np.uint8)
    img = Thin(img,array)




    return img

def adaptive_thres1(img, win, beta=0.96):
    if win % 2 == 0:
        win = win - 1
    # 边界的均值有点麻烦
    # 这里分别计算和和邻居数再相除
    img = img[:, :, 0] * 0.11 + img[:, :, 1] * 0.59 + img[:, :, 2] * 0.3
    img = img.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:
    img = gause_image(img)

    # laplace = np.array([[1, 1, 1],
    #                     [1, -8, 1],
    #                     [1, 1, 1]])

    # image_laplace = convolution(image=img, kernal_size=laplace)
    # img = image_laplace.copy()

    kern = np.ones([win, win])
    sums = signal.correlate2d(img, kern, 'same')
    cnts = signal.correlate2d(np.ones_like(img), kern, 'same')
    means = sums // cnts
    # 如果直接采用均值作为阈值,背景会变花
    # 但是相邻背景颜色相差不大
    # 所以乘个系数把它们过滤掉
    img = np.where(img < means * beta, 0, 255)
    img = img.astype(np.uint8)
    # img = Morphology_Erode(img, 1)
    img = Thin(img,array)
    img = Morphology_Erode(img)
    img = Morphology_Dilate(img)
    kernel = np.ones((3,3))
    r = img.shape[0]
    l = img.shape[1]
    pix =[]

    img[img == 0] =254
    img[img ==255] =0  # 进行反相
    for i in range(1,r-1):
        for j in range(1,l-1):
            if np.sum(np.multiply(kernel,img[i-1:i+2,j-1:j+2])) >=4*254:
                pix.append((i,j))
            else:
                pass
    # img[img == 1] =0
    # img[img == 0] =255
    img = cv.cvtColor(img,cv.COLOR_GRAY2RGB)
    for i in pix:
        cv.circle(img,i,3,(0,0,255),1)
        print(i)
    # img = zhangSuen(img)
    cv.imshow('1',img)
    cv.waitKey(0)

def binary_image(image):
    image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    image = image.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:


    prewitt_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])  # 设置prewitt_x算子
    prewitt_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])  # 设置prewitt_y算子

    image_prewitt_x = convolution(image=image, kernal_size=prewitt_x)
    image_prewitt_y = convolution(image=image, kernal_size=prewitt_y)
    image_prewitt = np.sqrt(image_prewitt_x ** 2 + image_prewitt_y ** 2)
    image_prewitt = (255 / image_prewitt.max()) * image_prewitt
    image_prewitt[image_prewitt > image_prewitt.mean()] = 255
    image_prewitt[image_prewitt < image_prewitt.mean()] = 0

    rows = image_prewitt.shape[0]
    cols = image_prewitt.shape[1]
    max=0
    min=0
    for i in range(rows):
        for j in range(cols):
            if image_prewitt[i,j] == 255:
                max +=1
            else:
                min +=1
    g.msgbox('白色面积为{},黑色面积为{}'.format(max,min))

    # length = 0
    # pix_length = []
    # for i in range(1,rows_sobel-1,1):
    #     for j in range(1,cols_sobel-1,1):
    #         if func1(image_sobel,i,j,pix_length):
    #             length +=1
    #             pix_length.append((i,j))
    #         elif func2(image_sobel,i,j,pix_length):
    #             length +=math.sqrt(2)
    #             pix_length.append((i,j))
    #         else:
    #             pass
    #     print('第{}轮结束,还有{}轮需要计算'.format(i,rows_sobel-1-i))
    # print(length)
    # cv.imshow('binary image', image)
    # image = erosion(image)
    # image = image.astype(np.uint8)
    # Gauss_img = cv.GaussianBlur(image, (3, 3), 0)
    adap_binary = adaptive_thres(image,3)
    max1 = 0
    min1 = 0
    iThin = Thin(adap_binary, array)
    for i in range(iThin.shape[0]):
        for j in range(iThin.shape[1]):
            if iThin[i, j] == 255:
                max1 += 1
    g.msgbox('边界长度为{}'.format(max1))

    cv.imshow('binary image', image_prewitt)
    cv.imshow('detection image{}',iThin)
    cv.waitKey(0)


def static_number(image):   #也进行灰度化
    image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    image = image.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:

    rows = image.shape[0]
    cols = image.shape[1]

    sum2 = []
    for i in range(rows):
        for j in range(cols):
            sum2.append(int(image[i, j]))
    sum2.sort()
    medium_number = sum2[len(sum2)//2]     #去中位数的灰度值
    min_area = 0
    max_area = 0
    area = rows * cols
    for i in range(rows):
        for j in range(cols):
            if int(image[i,j]) < medium_number:
                image[i,j] = 0
                min_area +=1
            else:
                image[i,j] = 255
                max_area +=1

    suanzi1 = np.fromfunction(func, (3, 3), sigma=2)
    # Laplace扩展算子
    laplace = np.array([[1, 1, 1],
                        [1, -8, 1],
                        [1, 1, 1]])

    image_laplace = convolution(image=image, kernal_size=suanzi1)  # 进行高斯平滑后在进行我们的拉place变换
    image_laplace = image_laplace * (255 / image_laplace.max())
    image_laplace = convolution(image=image_laplace, kernal_size=laplace)
    # length = 0
    # pix_length = []
    # for i in range(1,rows_sobel-1,1):
    #     for j in range(1,cols_sobel-1,1):
    #         if func1(image_sobel,i,j,pix_length):
    #             length +=1
    #             pix_length.append((i,j))
    #         elif func2(image_sobel,i,j,pix_length):
    #             length +=math.sqrt(2)
    #             pix_length.append((i,j))
    #         else:
    #             pass
    #     print('第{}轮结束,还有{}轮需要计算'.format(i,rows_sobel-1-i))
    # print(length)
    # cv.imshow('binary image', image)
    # image = erosion(image)
    # image = image.astype(np.uint8)
    image_binary = cv.adaptiveThreshold(image_laplace, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 55, 2)

    image_detection = Thin(image_binary.copy(), array)


    g.msgbox('灰色区域面积为{0},白色面积为{1}'.format(min_area * area /(min_area+max_area),max_area * area/(min_area+max_area) ))
    cv.imshow('binary image2',image_binary)
    cv.imshow('detection_image',image_detection)
    cv.waitKey(0)

def func(x,y,sigma=1):
    return 100*(1/(2*np.pi*sigma))*np.exp(-((x-2)**2+(y-2)**2)/(2.0*sigma**2))

def edge_detection(image):                  #均值滤波     #进来的第一不也是灰度化
    prewitt_x = np.array([[-1,0,1],[-1,0,1],[-1,0,1]]) # 设置prewitt_x算子
    prewitt_y = np.array([[-1, -1, -1], [0, 0, 0], [1,1,1]])  # 设置prewitt_y算子

    sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])  # 设置sobel算子
    sobel_y = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])

    # 生成标准差为5的5*5高斯算子
    suanzi1 = np.fromfunction(func, (3, 3), sigma=2)
    # Laplace扩展算子
    laplace = np.array([[1, 1, 1],
                        [1, -8, 1],
                        [1, 1, 1]])

    # 打开图像并转化成灰度图像
    image = image[:, :, 0] * 0.11 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.3
    image = image.astype(np.uint8)  # GRAY=0.3*R+0.59*G+0.11*B:
    image_prewitt_x = convolution(image=image,kernal_size=prewitt_x)
    image_prewitt_y = convolution(image=image, kernal_size=prewitt_y)
    image_prewitt = np.sqrt(image_prewitt_x**2+image_prewitt_y**2)
    image_prewitt = (255/image_prewitt.max())*image_prewitt


    image_sobel_x = convolution(image=image, kernal_size=sobel_x)
    image_sobel_y = convolution(image=image, kernal_size=sobel_y)
    image_sobel = np.sqrt(image_sobel_x ** 2 + image_sobel_y ** 2)
    image_sobel = (255 / image_sobel.max()) * image_sobel

    image_laplace = convolution(image=image, kernal_size=suanzi1)      #进行高斯平滑后在进行我们的拉place变换
    image_laplace = image_laplace * (255 /image_laplace.max())
    image_laplace = convolution(image=image_laplace,kernal_size=laplace)
    # image_laplace[image_laplace>image_laplace.mean()] = 255
    # image_laplace[image_laplace < image_laplace.mean()] = 0

    # 显示图像

    plt.subplot(1,3,1)
    plt.title('prewitt image')
    plt.imshow(image_prewitt,cmap=cm.gray)
    plt.axis("off")
    plt.subplot(1,3,2)
    plt.title('sobel image')
    plt.imshow(image_sobel,cmap=cm.gray)
    plt.axis("off")
    plt.subplot(1,3,3)
    plt.title('laplace made')
    plt.imshow(image_laplace, cmap=cm.gray)
    plt.axis("off")
    plt.show()

if __name__ == '__main__':
    msg = "请输入您想要完成的任务(建议您第一步先打开图片)"
    title = '第三次作业'
    choice = ('打开图片', '退出')
    a = g.buttonbox(msg=msg, title=title, choices=choice)
    if a == '打开图片':
        filename = g.fileopenbox(msg="请打开一个jpg文件")
        img = cv.imread(filename)
        msg1 = "选择您想要实现的功能"
        title1 = '第三次作业'
        choice1 = ('边缘检测', '二值图', '骨架提取','显示原图','重新选择图片','退出')
        q = 1
        while q:
            b = g.buttonbox(msg=msg1, title=title1, choices=choice1)
            if b == '边缘检测':
               edge_detection(img)

            elif b == '二值图':
              binary_image(img)

            elif b == '骨架提取':
               adaptive_thres1(img,3)
               # detection_region(img)
            elif b == '显示原图':
                cv.imshow('原图',img)
            elif b == '重新选择图片':
                filename = g.fileopenbox(msg="请打开一个jpg文件")
                img = cv.imread(filename)
            else:
                q = 0
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值