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
def onmouse(event, x, y, flags, param):#鼠标事件响应函数
global drag_start, sel, lab, sets, count
if event == cv.EVENT_LBUTTONDOWN:#左键按下时记录当前初始坐标,并初始化矩形sel
drag_start = x, y
sel = 0,0,0,0
sam=0,0,0,0
if count==100:
lab+=1
if count==200:
lab+=1
if count>=300:
print('over all')
count+=1
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=mid[0]-14,mid[1]-14,mid[0]+14,mid[1]+14
patch = gray[sam[0]:sam[2],sam[1]:sam[3]]#取矩形区域内像素作为patch图像
target = cv.cvtColor(patch, cv.COLOR_GRAY2BGR)#取矩形区域内原始像素截取图像
#sets.append([target,int(input('lab:'))])
sets.append([target,lab])
print(target.shape,sam,count,lab,sam[0],sam[1],sam[2],sam[3])
np.save('cloud',sets)
#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
sets=[]
count=0
#filename = 'HC421.jpg'
filename = 'up.jpg'
cv.namedWindow("gray",1) # 第二个参数 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()
v2
# coding:utf-8
import numpy as np
from math import *
import sys
import os
import glob
import argparse
import cv2 as cv
import random
import pickle
from libtiff import TIFF
from scipy import misc
from matplotlib import pyplot as plt
imagename='../../湖泊陆地植被-平原/1182801/HJ1A-CCD1-453-76-20140728-L20001182801-'
image=np.array([TIFF.open(imagename+str(i+1)+'.TIF', mode='r').read_image() for i in range(4)])
image_hwc = np.transpose(image, (1, 2, 0))
data = np.zeros([1,28,28,4])
print('image_hwc.shap....................................................',image_hwc.shape)
filename = 'test_gyy.jpg'
image_hwc=cv.imread(filename,1)
def onmouse(event, x, y, flags, param):#鼠标事件响应函数
global drag_start, sel, times, sets, count
if event == cv.EVENT_LBUTTONDOWN:#左键按下时记录当前初始坐标,并初始化矩形sel
drag_start = x, y
sel = 0,0,0,0
sam=0,0,0,0
count+=1
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=mid[0]-14*times,mid[1]-14*times,mid[0]+14*times,mid[1]+14*times
#patch = gray[sam[0]:sam[2],sam[1]:sam[3]]#取矩形区域内像素作为patch图像
patch = image_hwc[sam[0]:sam[2],sam[1]:sam[3],:]#取矩形区域内像素作为patch图像
for i in range(times):
for j in range(times):
sets.append(patch[i*28:(i+1)*28,j*28:(j+1)*28])
print('patch.shape:',patch.shape,'times:',times,sam,count,sam[0],sam[1],sam[2],sam[3])
#target = cv.cvtColor(patch, cv.COLOR_GRAY2BGR)#取矩形区域内原始像素截取图像
#sets.append([target,int(input('lab:'))])
#sets.append(patch)
cv.imwrite('./dataset_zx/buf_ori.jpg',patch)
np.save('./dataset_zx/thickcloud_2',sets)
#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)#全局变量 长方形左上颌右下定点坐标存储
times=5
sets=[]
count=0
filename = 'test_gyy.jpg'
cv.namedWindow("gray",1) # 第二个参数 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)
print('img.shape:',img.shape)
sel = (0,0,0,0)
drag_start = None
gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("gray",gray)
cv.waitKey(0)
cv.destroyAllWindows()
test
#split patch
for i in range(times):
cv.imwrite('./dataset_zx/' + str(i) + '.png', test[i])
def fill_buf(buf, i, img, shape):
'''fill the ith grid of the buffer matrix with the values from the img
buf : buffer matrix
i : serial of the image in the 2D grid
img : image data
shape : ( height width depth ) of image'''
# grid height is a multiple of individual image height
m = buf.shape[0]/shape[0]
sx = (i%m)*shape[1]
sy = (i//m)*shape[0]
sx = int(sx)
sy = int(sy)
buf[sy:sy+shape[0], sx:sx+shape[1], :] = img
n = np.ceil(np.sqrt(test.shape[0]))
buff = np.zeros((int(n*test.shape[1]), int(n*test.shape[2]), int(test.shape[3])), dtype=np.uint8)
for i, img in enumerate(test):
fill_buf(buff, i, img, test.shape[1:3])
cv.imwrite('./dataset_zx/%s.jpg' % 'buf', buff)