期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序

学习目标:

使用Kivy和OpenCV完成CIS2 期中作业


作业内容:

使用Kivy与OpenCV制作一个简单的图像识别界面
设计功能:

  1. 读取目标图片
  2. 设定结构元素
  3. 形态学处理
  4. 目标计数

目标图片 灰度图1

期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序_第1张图片

OpenCV部分

使用cv2讀取目標圖片

img=cv2.imread("hs.png") img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

使用thresholding转为黑白图

threshold=78 imgb=cv2.threshold(imgb,threshold, 255,cv2.THRESH_BINARY)[1]

我们注意到图片中物体的间隙多为纵向细长矩形,因此我们的结构元素设定的形状为(2,10)

kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,10))

之後 我们采取了一系列的形态学变换,使物体的边界更清晰。
期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序_第2张图片
最後我们调用

result=cv2.findContours(img3, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) result1=result[1] result0=result[0] result1.shape len(result0)

完成计数
期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序_第3张图片但是在notebook上尝试不同的形态学变化较为麻烦。我们尝试用Kivy制作简单的GUI程序。

处理程序直接调用OpenCV的函数,使用Kivy完成界面和按键的绑定。
维护了一个栈存放当前处理的步骤来实现一键回退。

以下为後台源码:

"""
Created on Sun Nov 29 11:28:55 2020

@author: 桂江
"""
import cv2

class CvBackend():
    ImageUrl=''
    currentImage=''
    test='Ready '
    Imagelist=[]
    kernel=cv2.getStructuringElement(cv2.MORPH_RECT, (2,10))
    def loadImage(self,ImageUrl=""):
        if not len(ImageUrl):
            ImageUrl="hs.png"
            
        img=cv2.imread(ImageUrl)
        img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        threshold=78
        img=cv2.threshold(img,threshold,255,cv2.THRESH_BINARY)[1]
        self.Imagelist=[]
        self.Imagelist.append(img)
        self.currentImage=img
        print("image loaded")
        return self.img2URL(img)
    

    def undo(self):
       if self.Imagelist==[]:
           return "empty"
       if len(self.Imagelist)==1:
           return self.img2URL(self.Imagelist[0])
       self.Imagelist.pop()
       self.currentImage=self.Imagelist[-1]
       return self.img2URL(self.currentImage)
   
    
   
    def setKernel(self,shape=cv2.MORPH_RECT,size=(2,10)):
         self.kernel=cv2.getStructuringElement(shape, size)
   
    def useMrphologyEx(self,method):
        temp=self.currentImage
        kernel=self.kernel
        temp=cv2.morphologyEx(temp,method,kernel)
        self.Imagelist.append(temp)
        self.currentImage=temp
        return self.img2URL(temp)
        
        
    def img2URL(self,img):
        tempUrl="cache/temp.png"
        cv2.imwrite(tempUrl, img)
        return tempUrl
        
        
    def count(self):
        if self.currentImage=='':
            return "No Images"
        result=cv2.findContours(self.currentImage, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        print("counting")
        return result
        
    def save(self,num):
        tempUrl=num+".png"
        cv2.imwrite(tempUrl, self.currentImage)
        

此为Kivy界面代码,直接写在一个文件里了。
Kivy有两种方式完成界面的搭建,一种是直接在py文件堆组件(本次使用方式),另一种是在XML文件里注册。

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import  GridLayout
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color,Rectangle
from kivy.uix.image import Image
from kivy.uix.slider import Slider


import os
import cvBackend
import cv2

class MyButton(Button):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.size_hint=[0.15,0.0927]
        self.color=[0,0,0,1]
        
        
class IndexPage(FloatLayout):
    
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.size=[800,800]
        self.cvbackend=cvBackend.CvBackend()
        self.image=''
        self.kernelInfo="Rect:[2,10]"
        with self.canvas:
            Color(0.3,0.2,0.2,1)
            self.rect=Rectangle(pos=self.pos,size=self.size)
            self.bind(pos=self.update_rect,size=self.update_rect)
        
        
        self.Opening=MyButton(text="Opening",pos_hint={"x":0.01,"y":0.01})
        self.Opening.bind(on_press=self.op_pressed)
        self.add_widget(self.Opening)
        
        self.Closing=MyButton(text="Closing",pos_hint={"x":0.17,"y":0.01})
        self.Closing.bind(on_press=self.cl_pressed)
        self.add_widget(self.Closing)
        
        self.Erosion=MyButton(text="Erosion",pos_hint={"x":0.33,"y":0.01})
        self.Erosion.bind(on_press=self.er_pressed)
        self.add_widget(self.Erosion)
        
        self.Dilation=MyButton(text="Dilation",pos_hint={"x":0.49,"y":0.01})
        self.Dilation.bind(on_press=self.di_pressed)
        self.add_widget(self.Dilation)
        
        self.Count=MyButton(text="CountObjects",pos_hint={"x":0.7,"y":0.01})
        self.Count.bind(on_press=self.count_pressed)
        self.add_widget(self.Count)
        
        self.leftImage=Image(source="default.png",pos=[1,50],keep_data=True,keep_ratio=False,nocache=True,allow_stretch=False,size=[200,200])
        self.add_widget(self.leftImage)
        
        self.countResult=Label(text="count\n Result",size_hint=[0.15,0.0927],pos_hint={"x":0.85,"y":0.01},
                               color=[0,0,0,1],bold=True,font_size=30)  
        self.add_widget(self.countResult)
        
        
        self.undo=MyButton(text="undo",pos_hint={"x":0.87,"y":0.6},size_hint=[0.0927,0.1])
        self.undo.bind(on_press=self.undo_pressed)
        self.add_widget(self.undo)
        
        self.save=MyButton(text="save",pos_hint={"x":0.87,"y":0.4},size_hint=[0.0927,0.1])
        self.save.bind(on_press=self.save_pressed)
        self.add_widget(self.save)
        
        
        
        self.loadText=TextInput(text="hs.png",pos_hint={"x":0.01,"y":0.7},size_hint=[0.0927,0.06])
        self.add_widget(self.loadText)
        
        
        
        
        self.load=MyButton(text="loadImage",pos_hint={"x":0.01,"y":0.6},size_hint=[0.0927,0.1])
        self.load.bind(on_press=self.load_pressed)
        self.add_widget(self.load)
        
        
        self.selectKernel=MyButton(text="selectKernel",pos_hint={"x":0.01,"y":0.5},size_hint=[0.0927,0.1])
        self.selectKernel.bind(on_press=self.select_Kernel)
        self.add_widget(self.selectKernel)
        
        
        
        self.info=Label(text=self.cvbackend.test,size_hint=[0.15,0.0927],pos_hint={"x":0.85,"y":0.2},
                               color=[0,0,0,1],bold=True,font_size=30) 
        self.add_widget(self.info)
        
        
        
        self.kinfo=Label(text=self.kernelInfo,size_hint=[0.15,0.0927],pos_hint={"x":0.01,"y":0.43},
                               color=[0,0,0,1],bold=True,font_size=20) 
        self.add_widget(self.kinfo)
        
        self.l1=Label(text="Shape:R E C",size_hint=[0.15,0.0927],pos_hint={"x":0.01,"y":0.35},
                               color=[0,0,0,1],bold=True,font_size=20) 
        self.add_widget(self.l1)
        
        
        self.kernelshape=Slider(min=1,max=3,pos_hint={"x":0.01,"y":0.3},size_hint=[0.0927,0.1],value_track=True,step=1,value_track_color=[1,0,0,1])
        self.add_widget(self.kernelshape)
        
        
        self.l2=Label(text="Hight:1-100",size_hint=[0.15,0.0927],pos_hint={"x":0.01,"y":0.25},
                               color=[0,0,0,1],bold=True,font_size=20) 
        self.add_widget(self.l2)
        
        self.kernelH=Slider(min=1,max=50,pos_hint={"x":0.01,"y":0.2},size_hint=[0.15,0.1],value_track=True,step=1,value_track_color=[1,0,0,1])
        self.add_widget(self.kernelH)
        
        
        
        self.l3=Label(text="Width:1-100",size_hint=[0.15,0.0927],pos_hint={"x":0.01,"y":0.15},
                               color=[0,0,0,1],bold=True,font_size=20) 
        self.add_widget(self.l3)
        
        self.kernelW=Slider(min=1,max=50,pos_hint={"x":0.01,"y":0.1},size_hint=[0.15,0.1],value_track=True,step=1,value_track_color=[1,0,0,1])
        self.add_widget(self.kernelW)
        
        
    def update_rect(self,*args):
        self.rect.pos=self.pos
        self.rect.size=self.size
         
    
    def load_pressed(self,arg):
        self.info.text="load is pressed"
        url=self.loadText.text
      
        if os.path.exists(url) and os.path.splitext(url)[-1] in ['.jgp', '.png', '.jpeg', '.bmp']:
             temp=self.cvbackend.loadImage(url)
             self.leftImage.source=temp
             self.leftImage.reload()
             print(temp)
        else:
            self.info.text=" not a vaild image"
       
        
    def op_pressed(self,arg):
        self.info.text="opening"
        temp=self.cvbackend.useMrphologyEx(cv2.MORPH_OPEN)
        print(temp)
        self.leftImage.reload()
        
    def cl_pressed(self,arg):
        self.info.text="closing"
        temp=self.cvbackend.useMrphologyEx(cv2.MORPH_CLOSE)
        print(temp)
        self.leftImage.reload()
    
    def er_pressed(self,arg):
        self.info.text="erosion"
        temp=self.cvbackend.useMrphologyEx(cv2.MORPH_ERODE)
        print(temp)
        self.leftImage.reload()
        
    def di_pressed(self,arg):
        self.info.text="dilation"
        temp=self.cvbackend.useMrphologyEx(cv2.MORPH_DILATE)
        print(temp)
        self.leftImage.reload()
        
    def undo_pressed(self,arg):
        self.info.text="undo"
        temp=self.cvbackend.undo()
        print(temp)
        self.leftImage.reload()    
    
    def count_pressed(self,arg):
        self.info.text="count"
        temp=self.cvbackend.count()
        num=str(len(temp[0]))
        self.countResult.text=num
        
    def save_pressed(self,arg):
        self.info.text="saved"
        self.cvbackend.save(self.countResult.text)
        
        
    def select_Kernel(self,arg):
        shape=self.kernelshape.value
        H=self.kernelH.value
        W=self.kernelW.value

        if (shape==1):
            temp1="Rect"
            temp2=cv2.MORPH_RECT
        if (shape==2):
            temp1="Ecli"
            temp2=cv2.MORPH_ELLIPSE
        if (shape==3):
            temp1="Cross"
            temp2=cv2.MORPH_CROSS
        self.cvbackend.kernel=self.cvbackend.setKernel(temp2,(W,H))
        temp=temp1+":"+"["+str(W)+","+str(H)+']'
        self.kernelInfo=temp
        self.kinfo.text=self.kernelInfo
        print(temp)
        
class CIS2MidtermApp(App):
    def build(self):
        return IndexPage()

if __name__=="__main__":
    CIS2MidtermApp().run()

运行效果
期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序_第4张图片期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序_第5张图片

期中作业系列 004 Python 图形界面Kivy+ Opencv 简单图像物体计数程序_第6张图片一通变换後点击CountObjects 显示是51个,符合预期。使用GUI可以轻易的尝试各种结构体与形态学变换的组合。

简单复习下形态学变换:

  1. 開运算 先腐蚀后膨胀
  2. 閉运算 先膨胀再腐蚀
  3. 腐蚀可以去掉小的白色物体 使黑色部分扩大 (结构体扫过区域中有0像素即为0)
  4. 膨胀可以去掉小的黑色缝隙,使白色部分扩大 (结构体扫过区域有1像素即为1)

你可能感兴趣的:(笔记)