同时控制两张图

时间:2022-12-01 06:07:35
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 26 16:14:05 2014

@author: lxx
"""

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 25 13:06:27 2014

@author: lxx
"""

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 24 15:38:06 2014

@author: lxx
"""
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 24 10:32:59 2014

@author: lxx
"""
import wx
import cv2
import  os

global events
#global image

global startPointx   
global startPointy 
global startPointx2     
global startPointy2  
global flagDraw
global i
global data 


filename_A="video1.BMP"
filename_B="video2.BMP"
txtname1="Pindata_A.txt"
txtname2="Holedata_A.txt"
txtname3="Pindata_B.txt"
txtname4="Holedata_B.txt"

 


class InsertFrame(wx.Frame): 
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "Frame With Button",size=(300, 200))
        panel = wx.Panel(self) #创建画板   
        button1 = wx.Button(panel, label="image_scr", pos=(100, 10), size=(100, 50))
        self.Bind(wx.EVT_BUTTON,self.Image_scr,button1)                  
        button2 = wx.Button(panel, label="Pin_data_A", pos=(30,60), size=(100, 50))
        self.Bind(wx.EVT_BUTTON,self.Pin_data_A,button2)
        button3 = wx.Button(panel, label="Hole_data_A", pos=(160,60), size=(100, 50))
        self.Bind(wx.EVT_BUTTON,self.Hole_data_A,button3)
        button4 = wx.Button(panel, label="Pin_data_B", pos=(30,110), size=(100, 50))
        self.Bind(wx.EVT_BUTTON,self.Pin_data_B,button4)
        button5 = wx.Button(panel, label="Hole_data_B", pos=(160,110), size=(100, 50))
        self.Bind(wx.EVT_BUTTON,self.Hole_data_B,button5)
        
    def Image_scr(self, events):
        global data_A
        global data_B
        global startPointx   
        global startPointy 
        global startPointx2   
        global startPointy2 
        global flagDraw
        global i
#        global image
        data_A=[]
        data_B=[] 
        i=0   
        startPointx = 0  
        startPointy = 0  
        flagDraw = False
        image = cv2.imread(filename_A)
        imagel = cv2.imread(filename_B)
        cv2.namedWindow("rectangle_A",cv2.WINDOW_NORMAL)
        cv2.namedWindow("rectangle_B",cv2.WINDOW_NORMAL)
        #cv2.namedWindow("rectangle_1",cv2.WINDOW_NOR
        cv2.imshow('rectangle_A',image)
        cv2.imshow("rectangle_B",imagel)
         
        def onMouseEvent(event,x,y,flags,param):  
            global startPointx     
            global startPointy
            global startPointx2   
            global startPointy2 
            global flagDraw
            global i
        #鼠标左键按下响应   
            if(event==1):  #第一点
                print "Position is: %d,%d" ,x,y             
                startPointx = x
                startPointy = y  
                i=i+1
                cv2.putText(image,str(i),(startPointx,startPointy),2,1,(249,99,201))
                #标数字是
                flagDraw = True                    
                a = str(x)
                b = str(y)
                data_A.append(a)
                data_A.append(b)
                   
            #鼠标移动响应   
            if(event==0):  #滑动过程
                if(flagDraw==True): 
                    imgc = cv2.copyMakeBorder(image,0,0,0,0,cv2.BORDER_CONSTANT)
                    cv2.rectangle(imgc, (startPointx,startPointy), (x,y), (249,99,201),2)  
                    cv2.imshow('rectangle_A', imgc)                
                    
                    
            #鼠标左键抬起响应  
            if(event==4):   #第二点
                if(flagDraw==True):  
                    cv2.rectangle(image, (startPointx,startPointy), (x,y), (249,99,201),2)                 
                    flagDraw = False
                    print "EndPosition is: %d,%d" ,x,y  
                    c = str(x)
                    d = str(y)
                    data_A.append(c)
                    data_A.append(d)
        def onMouseEvent2(event,x,y,flags,param):  
            global startPointx2     
            global startPointy2   
            global flagDraw
            global i
        #鼠标左键按下响应   
            if(event==1):  #第一点
                print "Position is: %d,%d" ,x,y             
                startPointx2 = x
                startPointy2 = y  
                i=i+1
                cv2.putText(imagel,str(i),(startPointx2,startPointy2),2,1,(249,99,201))
                #标数字
                flagDraw = True                    
                a = str(x)
                b = str(y)
                data_B.append(a)
                data_B.append(b)
                   
            #鼠标移动响应   
            if(event==0):  #滑动过程
                if(flagDraw==True): 
                    imgc = cv2.copyMakeBorder(imagel,0,0,0,0,cv2.BORDER_CONSTANT)
                    #画矩形框
                    cv2.rectangle(imgc, (startPointx2,startPointy2), (x,y), (249,99,201),2)    
                    cv2.imshow('rectangle_B', imgc)                 
                    
                    
            #鼠标左键抬起响应  
            if(event==4):   #第二点
                if(flagDraw==True):  
                    cv2.rectangle(imagel, (startPointx2,startPointy2), (x,y), (249,99,201),2)  
                    #cv2.imshow('rectangle_1', image)                
                    flagDraw = False
                    print "EndPosition is: %d,%d" ,x,y  
                    c = str(x)
                    d = str(y)
                    data_B.append(c)
                    data_B.append(d)

        cv2.setMouseCallback('rectangle_A', onMouseEvent,0)
        cv2.setMouseCallback("rectangle_B",onMouseEvent2)
        cv2.waitKey()

    def Data_pro(self,data,txtname,filename,rectangle):
        l=open(txtname,'w')
        f=open(txtname,'r')
        cv2.destroyWindow(rectangle)
        a=[]
        b=[]
        for eachline in data:
            eachline=eachline.strip()
            eachline=eachline.split(",") 
            for k in eachline:        
                k=str(k)
                b.append(k)
                if len(b)==4:
                    a.append(b)
                    b=[] 
        for z in a:
            for p in z :
                p=str(p)
                l.write(p)
                l.write("   ")
            l.write("\n")        
        l.close()
        f.close()

    
    def Pin_data_A(self, events):
        self.Data_pro(data_A,txtname1,filename_A,"rectangle_A")
    
    def Hole_data_A(self, events):
        self.Data_pro(data_A,txtname2,filename_A,"rectangle_A")
        
    def Pin_data_B(self, events):
        self.Data_pro(data_B,txtname3,filename_B,"rectangle_B")
    
    def Hole_data_B(self, events):
        self.Data_pro(data_B,txtname4,filename_B,"rectangle_B")
    


if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = InsertFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop() 
    
    

1.。在类中,调用自己的类,用self.hanshu 就行,而且每个类中的函数,都要hanshu(self,~。。)