-
#合成图片 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)
-
#使用鼠标采集图片数据生成.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()