数据增强(每10度进行旋转,进行一次增强,然后对每张图片进行扩充10张patch,最后得到原始图片数*37*10数量的图片)
# -*- coding: utf-8 -*-
"""
Fourmi Editor
This is a temporary script file.
"""
import cv2
import os
import numpy as np
import random
import math
def disOrdeImgs(Imgpath,Labelpath,orgTrainPath,orgTestPath,labelTrainPath,labelTestPath):
if not os.path.exists(orgTrainPath):
os.makedirs(orgTrainPath)
if not os.path.exists(orgTestPath):
os.makedirs(orgTestPath)
if not os.path.exists(labelTrainPath):
os.makedirs(labelTrainPath)
if not os.path.exists(labelTestPath):
os.makedirs(labelTestPath)
count=0
for fn in os.listdir(Imgpath): #fn 表示的是文件名
count = count+1
for index,v in enumerate(np.random.permutation(count)):
print('index:',index)
print('v:',v)
if index<=31911:
OrgTrainPath=os.path.join(Imgpath,str(v)+'.jpg')
Trainimg =cv2.imread(OrgTrainPath,0)
TrainPath=os.path.join(orgTrainPath,str(v)+'.jpg')
cv2.imwrite(TrainPath,Trainimg)
LabelTrainPath=os.path.join(Labelpath,str(v)+'.png')
Trainlabel =cv2.imread(LabelTrainPath,0)
TrainPath=os.path.join(labelTrainPath,str(v)+'.png')
cv2.imwrite(TrainPath,Trainlabel)
else:
OrgTestPath=os.path.join(Imgpath,str(v)+'.jpg')
Testimg =cv2.imread(OrgTestPath,0)
TestPath=os.path.join(orgTestPath,str(v)+'.jpg')
cv2.imwrite(TestPath,Testimg)
LabelTestPath=os.path.join(Labelpath,str(v)+'.png')
Testlabel =cv2.imread(LabelTestPath,0)
TestPath=os.path.join(labelTestPath,str(v)+'.png')
cv2.imwrite(TestPath,Testlabel)
def extract_random(full_imgs,full_masks,patch_h,patch_w,N_patches):
if(N_patches%(len(full_imgs))!=0):
print("N_patches: please enter a multiple of 115")
exit()
patches=np.empty((N_patches,patch_h,patch_w))
patches_masks = np.empty((N_patches,patch_h,patch_w))
img_h=full_imgs[0].shape[0]
img_w=full_imgs[0].shape[1]
patch_per_img=int(N_patches/(full_imgs.shape[0]))
print("patches per full image: "+str(patch_per_img))
iter_tot=0
for i in range(full_imgs.shape[0]):
k=0
while k<patch_per_img:
x_center = random.randint(0+int(patch_w/2),img_w-int(patch_w/2))
y_center = random.randint(0+int(patch_h/2),img_h-int(patch_h/2))
patch=full_imgs[i][y_center-int(patch_h/2):y_center+int(patch_h/2),x_center-int(patch_w/2):x_center+int(patch_w/2)]
patch_mask=full_masks[i][y_center-int(patch_h/2):y_center+int(patch_h/2),x_center-int(patch_w/2):x_center+int(patch_w/2)]
#print(patch_mask.shape)
patches[iter_tot]=patch
patches_masks[iter_tot]=patch_mask
iter_tot+=1
k+=1
return patches,patches_masks
def imagePadding(img):
img_h=img.shape[0]
img_w=img.shape[1]
scale=int(math.sqrt(img_h*img_h+img_w*img_w))
scale=scale*2
size=(int(scale),int(scale))
out=cv2.resize(img,size,interpolation=cv2.INTER_AREA)
return out
def get_data(data_imgs_org,
data_groundTruth,
patch_height,
patch_width,
N_subimgs):
imgs_org,imgs_groundTruth=ReadandProcessImage(data_imgs_org,data_groundTruth)
print('imgs.shape',imgs_org.shape)
print('imgs_groundTruth',imgs_groundTruth.shape)
patches_imgs_train,patches_masks_train=extract_random(imgs_org,
imgs_groundTruth,patch_height,patch_width,N_subimgs)
return patches_imgs_train,patches_masks_train
def ReadandProcessImage(orgImgPath,groundTruthPath):
images=[]
labels=[]
for root, dirs, files in os.walk(orgImgPath, topdown=False):
for file in files:
temp=file[:-4]
ImgPath=os.path.join(root,file)
LabelPath=os.path.join(groundTruthPath,temp+'.png')
myimg=cv2.imread(ImgPath,0)
mylabel=cv2.imread(LabelPath,0)
print('ImgPath:',ImgPath)
print('LabelPath:',LabelPath)
#img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
#mylabel=cv2.cvtColor(mylabel,cv2.COLOR_BGR2GRAY)
assert(len(myimg.shape)==len(mylabel.shape))
assert(myimg.shape[0]==mylabel.shape[0])
assert(myimg.shape[1]==mylabel.shape[1])
img=myimg
#org_h=img.shape[0]
#org_w=img.shape[1]
img=cv2.equalizeHist(img)
img=imagePadding(img)
mylabel=imagePadding(mylabel)
images.append(img)
labels.append(mylabel)
return np.array(images),np.array(labels)
def roatate_img_label_to_file(imgPath,labelPath):
global Iter
Iter=1
def rotateImg(img,label,orgHeight,orgWidth,imgPath,labelPath):
global Iter
(h,w)=img.shape
center=(h/2,w/2)
for i in range(360):
if (i%10!=0):
continue
M = cv2.getRotationMatrix2D(center, i, 1)
imgRotated = cv2.warpAffine(img, M, (h, w))
img0=imgRotated[int(center[0])-int(orgHeight/2):int(center[0])+int(orgHeight/2),
int(center[1])-int(orgWidth/2):int(center[1])+int(orgWidth/2)]
labelRotated = cv2.warpAffine(label, M, (h, w))
label0=labelRotated[int(center[0])-int(orgHeight/2):int(center[0])+int(orgHeight/2),
int(center[1])-int(orgWidth/2):int(center[1])+int(orgWidth/2)]
path0=os.path.join(imgPath,str(Iter+115)+'.jpg')
cv2.imwrite(path0,img0)
path=os.path.join(labelPath,str(Iter+115)+'.png')
cv2.imwrite(path,label0)
Iter=Iter+1
print("ROTATW DONE!!!!")
for root,dirs,files in os.walk(imgPath,topdown=False):
for file in files:
imgpath=os.path.join(root,file)
temp=file[:-4]
labelpath=os.path.join(labelPath,temp+'.png')
img=cv2.imread(imgpath,0)
label=cv2.imread(labelpath,0)
print('imgpath:',imgpath)
print('labelpath:',labelpath)
print('imgshape:',img.shape)
print('labelshape:',label.shape)
assert(len(img.shape)==len(label.shape))
assert(img.shape[0]==label.shape[0])
assert(img.shape[1]==label.shape[1])
org_h=img.shape[0]
org_w=img.shape[1]
img=imagePadding(img)
label=imagePadding(label)
print('imgPadding:',img.shape)
print('labelPadding:',label.shape)
rotateImg(img,label,org_h,org_w,imgPath,labelPath)
data_train_imgs_org="/home/chendali1/Gsj/JX/Image/train/"
data_test_imgs_org="/home/chendali1/Gsj/JX/Image/test/"
data_train_grountTruth="/home/chendali1/Gsj/JX/GT/train/"
data_test_grountTruth="/home/chendali1/Gsj/JX/GT/test/"
patches_path_train='/home/chendali1/Gsj/JX/Patches/Org/train/'
patches_path_test='/home/chendali1/Gsj/JX/Patches/Org/test/'
patches_path_label_train='/home/chendali1/Gsj/JX/Patches/Label/train/'
patches_path_label_test='/home/chendali1/Gsj/JX/Patches/Label/test/'
#rotate_train_imgs_path="/home/chendali1/Gsj/JX/Image/train/"
#rotate_test_imgs_path="/home/chendali1/Gsj/JX/Image/test/"
#rotate_train_label_path="/home/chendali1/Gsj/JX/GT/train/"
#rotate_test_label_path="/home/chendali1/Gsj/JX/GT/test/"
"""
if not os.path.exists(patches_path_train):
os.makedirs(patches_path_train)
if not os.path.exists(patches_path_test):
os.makedirs(patches_path_test)
if not os.path.exists(patches_path_label_train):
os.makedirs(patches_path_label_train)
if not os.path.exists(patches_path_label_test):
os.makedirs(patches_path_label_test)
roatate_img_label_to_file(data_train_imgs_org,data_train_grountTruth)
train_patches,train_groundTruth=get_data(data_train_imgs_org,data_train_grountTruth,224,224,37*115*10)
for i in range(train_patches.shape[0]):
b=np.zeros([train_patches.shape[1],train_patches.shape[2],3])
b[:,:,0]=train_patches[i,:,:]
b[:,:,1]=train_patches[i,:,:]
b[:,:,2]=train_patches[i,:,:]
cv2.imwrite(patches_path_train+str(i)+'.jpg',train_patches[i,:,:])
cv2.imwrite(patches_path_label_train+str(i)+'.png',train_groundTruth[i,:,:])
"""
Imgpath ="/home/chendali1/Gsj/JX/Patches/Org/train/"
Labelpath="/home/chendali1/Gsj/JX/Patches/Label/train/"
orgTrainPath="/home/chendali1/Gsj/DRIVE/images/training/"
orgTestPath="/home/chendali1/Gsj/DRIVE/images/validation/"
labelTrainPath="/home/chendali1/Gsj/DRIVE/annotations/training/"
labelTestPath="/home/chendali1/Gsj/DRIVE/annotations/validation/"
disOrdeImgs(Imgpath,Labelpath,orgTrainPath,orgTestPath,labelTrainPath,labelTestPath)
数据增强(每10度进行旋转,进行一次增强,然后对每张图片进行扩充10张patch,最后得到原始图片数*37*10数量的图片)的更多相关文章
- 剑指Offer - 九度1386 - 旋转数组的最小数字
剑指Offer - 九度1386 - 旋转数组的最小数字2013-11-24 01:57 题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转 ...
- 360度3D 旋转插件
Circlr插件是一款基于jQuery的可以对图片进行360度全方位旋转展示的插件.Circlr通过按一定角度规律拍摄的产品图片,制作出可以使用鼠标拖动.鼠标滚轮和移动触摸来进行图片逐帧旋转的效果.比 ...
- 20145104张家明 《Java程序设计》第10周学习总结
20145104张家明 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是两个或多个设备(程序)之间的数据交换. 识别网络上的每个设备:①IP地址②域名(Dom ...
- html5人物图片360度立体旋转
体验效果:http://hovertree.com/texiao/html5/10.htm 下载:http://hovertree.com/hvtart/bjae/t16oddyt.htm 代码如下: ...
- VirtualBox 5.0.10 中 Fedora 23 在安装了增强工具后无法自动调节虚拟机分辨率的问题(改)
VirtualBox 5.0.10 中安装 Fedora 23,即使在安装了增强工具后,仍然会发现虚拟机无法根据 VirtualBox 的运行窗口大小自动进行分辨率调节.究其原因,主要是因为 Fedo ...
- 7. 进行图片的数据补全和增强(随机亮度,随机饱和度,随机翻转) Image.open(进行图片的读入) 2.ImageEnhance.Brightness(亮度变化) 3.ImageEnhance.Contrast(饱和度变化) 4.enhance_image.transpose(图片随机翻转) 5.enhance_image.save(进行图片保存)
1.Image.open(image_path) 进行图片的打开 参数说明:image_path 表示图片的路径 2. ImageEnhance.Brightness(image) # 进行图片的 ...
- MFC 编辑框输入16进制字符串转换为16进制数或者10进制数据计算
1.编辑框添加变量,并选择变量类型为CString. 2. 使用“_tcstoul”函数将Cstring 类型转换为16进制/10进制数进行计算.
- HTML实现图片360度循环旋转
<style> .header{ -webkit-animation:rotateImg 5s linear infinite;<!--修改旋转周期--> border: 1p ...
- (数据科学学习手札142)dill:Python中增强版的pickle
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,相信不少读者朋友们都在Pyth ...
随机推荐
- 解决 Qt5 报错 This application failed to start because it could not find or load the Qt platform plugin
QT为了简化生成发布版本,特别提供了工具 "windeplayqt.exe",这个工具在 "...\Qt5.8.0\5.8\msvc2015_64\bin"的目 ...
- yum upgrade卡在 清理initial-setup-0.3.9.30-1.el7.centos.x86_64
我安装CENTOS7.2,用yum -y update进行更新 卡在这里了 清理 : initial-setup-0.3.9.30-1.el7.cent 目测是一个系统bug,执行关闭命令解决: sy ...
- Centos7.5 VMtools的安装与卸载
一.安装 1.自带tools: 选择VMware工具栏 => 虚拟机 => 安装VMtools 2.挂载光驱 3.tar -zxvf VMwareTools-10.3.2-9925305 ...
- [Jenkins]CentOS7下Jenkins搭建
最近在倒腾Kubernetes的一些东西,这次需要用到Jenkins来实现自动化构建.来讲一讲搭建的整个过程. Jenkins是什么 Jenkins提供了软件开发的持续集成服务.它运行在Servlet ...
- 【VMware vSphere】vCenter添加主机失败:无法访问指定主机
背景 前一段时间,给一台服务器安装ESXi系统,安装成功之后,通过vCenter在上面安装了一台VDP系统.结果前几天发现服务器掉线,重新连接时出现问题.问题描述如下: 其中错误堆栈具体内容为:在 v ...
- Keepalived详解(二):Keepalived安装与配置【转】
一.Keepalived安装与配置: 1.Keepalived的安装过程: Keepalived的安装非常简单,本实例以源码安装讲解: Keepalived的官方网址:http://www.keepa ...
- VB中的冒号——bug
关于VB中的冒号,给许多人的印象都是:“一行可书写几句语句”.这么说是对的,但是有一种情况是不对的,那就是在条件语句中.这也是做一个VB项目升级的时候遇到,因为这个问题我查了好长时间程序,一直在找VB ...
- hibernate框架学习之数据模型-POJO
Hibernate数据模型用于封装数据,开发时候需要遵从如下规范:1)提供公共无参的构造方法(可使用自动生成的)如果使用投影技术,一定要显式声明公共无参的构造方法2)提供一个标识属性,作为对象的主键, ...
- ffmpeg-201701[10,16,21,23,25]-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 5 屏幕横向放大 20 像素 6 屏幕横向缩小 20 像素 S 下一帧 [ -2秒 ] +2 ...
- 通过flask实现web页面简单的增删改查bootstrap美化版
通过flask实现web页面简单的增删改查bootstrap美化版 项目目录结构 [root@node1 python]# tree -L 2 . ├── animate.css ├── fileut ...