如何通過鼠標採集圖片數據集

  1. #合成圖片
    import numpy as np    
    import cv2
    channel_1 = cv2.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-1.TIF',cv2.IMREAD_GRAYSCALE)
    channel_2 = cv2.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-2.TIF',cv2.IMREAD_GRAYSCALE)
    channel_3 = cv2.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-3.TIF',cv2.IMREAD_GRAYSCALE)
    channel_4 = cv2.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-4.TIF',cv2.IMREAD_GRAYSCALE)
    print('channel1_shape,channel2_shape,channel3_shape,channel4_shape',(channel_1.shape,channel_2.shape,channel_3.shape,channel_4.shape))
    channel_1 = channel_1.reshape([channel_1.shape[0],channel_1.shape[1],1])
    channel_2 = channel_2.reshape([channel_2.shape[0],channel_2.shape[1],1])
    channel_3 = channel_3.reshape([channel_3.shape[0],channel_3.shape[1],1])
    channel_4 = channel_4.reshape([channel_4.shape[0],channel_4.shape[1],1])
    print('channel1_shape,channel2_shape,channel3_shape,channel4_shape',(channel_1.shape,channel_2.shape,channel_3.shape,channel_4.shape))
    total_channel = np.concatenate((channel_3,channel_2,channel_1),axis=2)
    image = total_channel[:,:,:]
    cv2.imwrite('./pic.png',image)
  2. #使用鼠標採集圖片數據生成.npy文件
    import numpy as np    
    from math import *    
    import sys    
    import os    
    import glob    
    import argparse    
    import cv2 as cv   
    import keras
    import random
    from matplotlib import pyplot as plt
    import pickle
    channel_1 = cv.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-1.TIF',cv.IMREAD_GRAYSCALE)
    channel_2 = cv.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-2.TIF',cv.IMREAD_GRAYSCALE)
    channel_3 = cv.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-3.TIF',cv.IMREAD_GRAYSCALE)
    channel_4 = cv.imread('/home/xm/桌面/make_dataset/3201844/HJ1A-CCD1-27-68-20170820-L20003201844-4.TIF',cv.IMREAD_GRAYSCALE)
    channel_1 = channel_1.reshape([channel_1.shape[0],channel_1.shape[1],1])
    channel_2 = channel_2.reshape([channel_2.shape[0],channel_2.shape[1],1])
    channel_3 = channel_3.reshape([channel_3.shape[0],channel_3.shape[1],1])
    channel_4 = channel_4.reshape([channel_4.shape[0],channel_4.shape[1],1])
    all_channel = np.concatenate((channel_1,channel_2,channel_3,channel_4),axis=2)
    image = all_channel[:,:,:]
    data = np.zeros([1,28,28,4])
    print('image.shap....................................................',image.shape)
    def onmouse(event, x, y, flags, param):#鼠標事件響應函數    
        global drag_start, sel, lab, cloudsets, count,data,filenames,sam
        if event == cv.EVENT_LBUTTONDOWN:#左鍵按下時記錄當前初始座標,並初始化矩形sel    
            drag_start = x, y    
            sel = 0,0,0,0   
            sam=0,0,0,0   
        elif event == cv.EVENT_LBUTTONUP:#鼠標左鍵嘆彈起時響應    
            if sel[2] > sel[0] and sel[3] > sel[1]:#判斷右下角座標是否大於左上角   
                mid=int((sel[1]+sel[3])/2),int((sel[0]+sel[2])/2)
                sam_0 = mid[0]-28,mid[1]-28,mid[0]+28,mid[1]+28
                sam_1=mid[0]-28,mid[1],mid[0],mid[1]+28
                sam_2=mid[0]-28,mid[1]-28,mid[0],mid[1]
                sam_3=mid[0],mid[1],mid[0]+28,mid[1]+28
                sam_4=mid[0],mid[1]-28,mid[0]+28,mid[1]
                count+=1
                filenames = './others/'+str(count)+'.png'
                cv2.imwrite(filenames,image[sam_1[0]:sam_1[2],sam_1[1]:sam_1[3],:])
                count+=1
                filenames = './others/'+str(count)+'.png'
                cv2.imwrite(filenames,image[sam_2[0]:sam_2[2],sam_2[1]:sam_2[3],:])
                count+=1
                filenames = './others/'+str(count)+'.png'
                cv2.imwrite(filenames,image[sam_3[0]:sam_3[2],sam_3[1]:sam_3[3],:])
                count+=1
                filenames = './others/'+str(count)+'.png'
                cv2.imwrite(filenames,image[sam_4[0]:sam_4[2],sam_4[1]:sam_4[3],:])
    ##            patch = gray[sam[0]:sam[2],sam[1]:sam[3]]#取矩形區域內像素作爲patch圖像
                patch = []
                patch.append(image[sam_1[0]:sam_1[2],sam_1[1]:sam_1[3],:])#取矩形區域內像素作爲patch圖像  
                patch.append(image[sam_2[0]:sam_2[2],sam_2[1]:sam_2[3],:])#取矩形區域內像素作爲patch圖像
                patch.append(image[sam_3[0]:sam_3[2],sam_3[1]:sam_3[3],:])#取矩形區域內像素作爲patch圖像
                patch.append(image[sam_4[0]:sam_4[2],sam_4[1]:sam_4[3],:])#取矩形區域內像素作爲patch圖像
                #print('patch.shap....................................................',patch.shape)
    ##            target = cv.cvtColor(patch, cv.COLOR_GRAY2BGR)#取矩形區域內原始像素截取圖像 
                #cloudsets.append([target,int(input('lab:'))])
    ##            features = target.reshape([-1,28,28,4])
                patch = np.array(patch)
                features = patch.reshape([-1,28,28,4])
    ##            cloudsets.append([features,lab])
    ##            cloudsets.append(features)
                data = np.concatenate([data,features],axis=0)
                print('data.shape...................................................',data.shape)
                print(features.shape,sam,count,lab,sam[0],sam[1],sam[2],sam[3])
    ##            np.save('cloudsets',cloudsets)
                np.save('./others.npy',data[1:])
                #f = open('/home/xm/桌面/dataset.txt','wb')
                #pickle.dump(data[1:],f)
                #plt.imsave('sample.jpg',target)
                drag_start = None    
        elif drag_start:    
            #print flags    
            if flags & cv.EVENT_FLAG_LBUTTON:#取當前座標與初始座標較小的爲矩形座標左上,較大的爲右下    
                minpos = min(drag_start[0], x), min(drag_start[1], y)    
                maxpos = max(drag_start[0], x), max(drag_start[1], y)    
                sel = minpos[0], minpos[1], maxpos[0], maxpos[1]    
                img = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)    
                cv.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)    
                #print(sel[0],sel[1],sel[2],sel[3])
                cv.imshow("gray", img)    
            else:    
                print ("selection is complete" )
                drag_start = None    
    
    drag_start = None#全局變量取方塊鼠標拖拽時使用    
    sel = (0,0,0,0)#全局變量 長方形左上頜右下定點座標存儲    
    lab=0
      
    ##if os.path.exists('cloudsets.npy'):
    ##    cloudsets=np.load('cloudsets.npy')
    ##    cloudsets=cloudsets.tolist()
    ##else:
    ##    cloudsets=[]
    count=0
    #filename = 'HC421.jpg'   
    filename = './pic.png'    
    cv.namedWindow("gray",2) # 第二個參數 1窗口固定;2窗口可調整   
    cv.setMouseCallback("gray", onmouse)     
    
    ext = os.path.splitext(filename)[1][1:] #get the filename extenstion    
    if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":    
        print (filename )   
    
        img=cv.imread(filename,1)    
        sel = (0,0,0,0)     
        drag_start = None    
        gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)    
        cv.imshow("gray",gray)    
        cv.waitKey(0)
    
    cv.destroyAllWindows()  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章