python opencv load image and splite train/test set
2017-03-31 16:46
651 查看
from sklearn import cross_validation
import cv2
def load_image_data(): dir_path=r'E:\' # class_1_path=dir_path+r'\no' # class_2_path=dir_path+r'\other' # class_3_path=dir_path+r'\yes' X=[] Y=[] testX=[] testY=[] #print(class_1_path) filepathvec = [] for rt, dirs, files in os.walk(dir_path): # =pathDir # print('rt:',rt) # print('dirs:',dirs) for dir in dirs: # print(dir) # if dir.find('-')<0: # continue for rt1, dirs, filenames in os.walk(rt+'\\'+dir): file_count=0 for filename in filenames: file_count+=1 if file_count>200: break # print (filename) if filename.find('.') >= 0: (shotname, extension) = os.path.splitext(filename) # print shotname,extension if extension == '.png' or extension == '.tif': filepathvec.append(os.path.join('%s\\%s' % (rt+'\\'+dir, filename))) # print (filename) break # exit() print(len(filepathvec)) for file in filepathvec: # if len(X)>3200: # break print(file) img=cv2.imread(file,0) # print(np.shape(img)) #cv2.namedWindow("the window") #cv2.imshow("the window",img) image_width,image_height=np.shape(img) print(image_width) fzoom=20.0/float(image_width) img=cv2.resize(img,(int(float(image_height)*fzoom),20), interpolation=cv2.INTER_CUBIC) img=cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE) img=255-img #反色处理 # cv2.imshow('xx', img) # cv2.waitKey() #print(img) #print(len(img)) #print(np.shape(img)) X.append(img) Y.append(get_label(file)) #cv2.namedWindow("the window") #cv2.imshow("the window",img) #break #多块平移提取处理 # img_src_height=np.shape(img)[0] # img_src_width=np.shape(img)[1] # for i in range(1,3): # img_crop=np.array(img[i:-1][i:-1]) # img_crop = cv2.resize(img_crop, (28, 28), interpolation=cv2.INTER_CUBIC) # img_crop = 255 - img_crop # 反色处理 # X.append(img_crop) # Y.append(get_label(file)) # # for i in range(1,3): # img_crop = img[0:i][0:i] # img_crop = cv2.resize(img_crop, (28, 28), interpolation=cv2.INTER_CUBIC) # img_crop = 255 - img_crop # 反色处理 # X.append(img_crop) # Y.append(get_label(file)) # print(X[0:5]) # print(Y[0:5]) print(len(X),len(Y)) X=np.array(X) Y=np.array(Y) # testX=X # testY=Y X, testX, Y, testY = cross_validation.train_test_split(X, Y, test_size=0.2) print('train set:',len(X)) print('test set:',len(testX)) return X,Y,testX,testY
相关文章推荐
- opencv学习-load image and display
- [Python] caffe.io.load_image与Opencv的imread区别
- Opencv2.4.3_tutorials(学习指南)——load and display an Image
- whats is the difference between train, validation and test set, in neural networks?
- 用Python和OpenCV创建一个图片搜索引擎的完整指南 The complete guide to building an image search engine with Python and
- (cv2.CV_LOAD_IMAGE_GRAYSCALE)Python调用opencv
- Convert URL to image with Python and OpenCV(根据URL下载图片)
- Whats is the difference between train, validation and test set, in neural networks?
- OpenCV自带例子(一)Load and Display an Image
- OpenCV自带例子(二)Load, Modify, and Save an Image
- 16. 26. 3. 装入想要尺寸的图片 Load Image and scale it
- 用ZwSetSystemInformation函数的SystemLoadAndCallImage调用驱动
- iOS: load external image and css to UIWebView
- How to covert QT QImage into OpenCV IplImage and vise-versa.
- Loading Rootkit using SystemLoadAndCallImage
- Test and Set
- 用SystemLoadAndCallImage加载Rootkit
- set up Android development environment and python
- test_and_set_bit()等小函数集合
- (转载)用SystemLoadAndCallImage加载Rootkit