python学习随笔03 Opencv提取轮廓,对轮廓识别后,判断轮廓形状

'''
@Author: your name
@Date: 2020-02-13 13:30:07
@LastEditTime: 2020-02-20 16:17:34
@LastEditors: Please set LastEditors
@Description: 高斯平滑展示,边缘检测展示,
              能够通过按键时时控制高斯平滑,高斯选择改变后改变高斯图和边缘检测图
              边缘检测通过右侧两个滑条更改检测阈值
              加入圆形检测
'''
import tkinter
import os
import cv2
import numpy
from PIL import Image,ImageTk
from tkinter import filedialog

# global start 全局变量定义初始化开始
#全局变量参数
carmela_hight = 200
carmela_width = 200
#Tkinter元素句柄参数
Source_Img_Label = None
Gray_Img_Label = None
Canny_Img_Label = None
Circles_Img_Label = None
Gaussian_Button = None
Counters_Button = None
Threshold_Min_Scale = None
Threshold_Max_Scale = None
Hough_Parm1_Scale = None
Hough_Parm2_Scale = None
#开关量参数
Gaussian_Enable = False
Counters_Enable = False
#获取当前文件路径
py_path=os.path.abspath(os.path.dirname(__file__))
#global end 全局变量定义初始化结束
#创建窗口对象以及窗口使用变量
root = tkinter.Tk()
Img_Path_String = tkinter.StringVar()
Img_Path_String.set('尚未选择文件')
Dp_Value = tkinter.StringVar()
Dp_Value.set('1')
#窗口使用变量初始化开始
screen_Width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
width = screen_Width//2
height = screen_height//2+50
root_win_par = '%dx%d+%d+%d'% (width, height, (screen_Width-width)/2, (screen_height-height)/2)   

#窗口使用变量初始化结束
def resizeImage(image, width, height, inter=cv2.INTER_AREA):
    size = image.shape
    h, w = size[0], size[1]
    #长边缩放为min_side
    min_side = min(width,height)
    scale = max(w, h) / float(min_side)
    new_w, new_h = int(w/scale), int(h/scale)
    resize_img = cv2.resize(image, (new_w, new_h))
    # 填充至min_side * min_side
    if new_w % 2 != 0 and new_h % 2 == 0:
        top, bottom, left, right = (min_side-new_h)/2, (min_side-new_h)/2, (min_side-new_w)/2 + 1, (min_side-new_w)/2
    elif new_h % 2 != 0 and new_w % 2 == 0:
        top, bottom, left, right = (min_side-new_h)/2 + 1, (min_side-new_h)/2, (min_side-new_w)/2, (min_side-new_w)/2
    elif new_h % 2 == 0 and new_w % 2 == 0:
        top, bottom, left, right = (min_side-new_h)/2, (min_side-new_h)/2, (min_side-new_w)/2, (min_side-new_w)/2
    else:
        top, bottom, left, right = (min_side-new_h)/2 + 1, (min_side-new_h)/2, (min_side-new_w)/2 + 1, (min_side-new_w)/2
    pad_img = cv2.copyMakeBorder(resize_img, int(top), int(bottom), int(left), int(right), cv2.BORDER_CONSTANT, value=[0,0,0]) #从图像边界向上,下,左,右扩的像素数目

    return pad_img

    #窗口使用变量初始化结束
#图片获取通过图片路径显示标签,返回值为cv2.imread 缩放后
def ImageGet():
    #读取图片参数,通过opencv
    Source_Img = cv2.imread(Img_Path_String.get())
    if (Source_Img is None):
        return None
    else
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值