2016-12-02 18:14:43 chenzhi1992 阅读数 1988
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    40566 人正在学习 去看看 欧阳坚

别人写的博客,记录下,防止以后找不到,顺便留着以后更新写自己的东西

http://blog.csdn.net/samylee/article/details/53436648

2019-09-28 11:20:37 qq_33668060 阅读数 154
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    40566 人正在学习 去看看 欧阳坚

@[TOC]深度学习中以原始数据为基础扩充数据集时,如何方便快捷的为扩充数据打label

在我们为深度学习做数据集的过程中,如果原始图片比较少,往往需要通过我们模仿外界条件为原始图像添加噪声、调整亮度等操作扩充数据集。但对于扩充的数据我们又不想再进行一个一个的去打label,那么我们可以根据标定好的原始数据,通过python代码自动实现为扩充的数据部分打label。

下面的代码实现了对原始图片的添加噪声、改变亮度,并对其进行打label。

源码如下:

import matplotlib.pyplot as plt
import os
import skimage
import random
from skimage import data_dir, io, img_as_float, exposure, filters
from skimage.morphology import disk
import skimage.filters.rank as sfr
from PIL import Image, ImageEnhance
import xml.dom.minidom

def gaussion(image,image_name, output_image_path):

	image = io.imread(image)
	name = os.path.splitext(image_name)[0]
	try:
		'''gaussian noise'''
		variance = round(random.uniform(0.001,0.005),4)
		image_noise = skimage.util.random_noise(image, mode='gaussian', seed=None, clip=True, var = variance)
		io.imsave(output_image_path+name+'_gaunoise.jpg', image_noise)

		dom1 = xml.dom.minidom.parse(xml_path + name +'.xml')
		xmlfile1 = open(output_xml_path+name+'_gaunoise.xml','w')
		xmlfile1.write(dom1.toprettyxml(encoding='utf-8'))
		'''gaussian filter'''
		sigma = round(random.uniform(0.1,1),3)
		image_filter = filters.gaussian(image, sigma)
		io.imsave(output_image_path+name+'_gaufilter.jpg', image_filter)

		dom2 = xml.dom.minidom.parse(xml_path + name +'.xml')
		xmlfile2 = open(output_xml_path+name+'_gaufilter.xml','w')
		xmlfile2.write(dom2.toprettyxml(encoding='utf-8'))
	except ValueError:
		print('cannot save')

def add_noise(image,image_name, index, output_image_path):

	image = io.imread(image)
	name = os.path.splitext(image_name)[0]

	if index == 1:
		'''salt noise'''
		variable = round(random.uniform(0.01,0.05),4)
		image_noise = skimage.util.random_noise(image, mode='salt', seed=None, clip=True, amount=variable)

	elif index == 2:
		'''pepper noise'''
		variable = round(random.uniform(0.005,0.01),4)
		image_noise = skimage.util.random_noise(image, mode='pepper', seed=None, clip=True, amount=variable)
	elif index == 3:
		'''salt + pepper noise'''
		variable = round(random.uniform(0.01,0.03),4)
		image_noise = skimage.util.random_noise(image, mode='s&p', seed=None, clip=True, amount=variable)
	elif index == 4:
		'''poisson noise'''
		image_noise = skimage.util.random_noise(image, mode='poisson', seed=None, clip=True)

	io.imsave(output_image_path+name+'_noise.jpg', image_noise)
	dom = xml.dom.minidom.parse(xml_path + name +'.xml')
	xmlfile = open(output_xml_path+name+'_noise.xml','w')
	xmlfile.write(dom.toprettyxml(encoding='utf-8'))

def add_filter(image, image_name, index, output_image_path):
	image = io.imread(image)
	name = os.path.splitext(image_name)[0]
	if index == 1:
		'''median filter'''
		image_filter = image
		sigma = round(random.uniform(0.3,0.6),3)
		for i in range(3):
			image_filter[:,:,i] = filters.median(image[:,:,i], disk(sigma))
	elif index == 2:
		'''minimum filter'''
		image_filter = image
		sigma = round(random.uniform(0.3,0.6),3)
		for i in range(3):
			image_filter[:,:,i] = sfr.minimum(image[:,:,i],disk(sigma))
	if exposure.is_low_contrast(image_filter) == False:
		io.imsave(output_image_path+name+'_filter.jpg', image_filter)
		dom = xml.dom.minidom.parse(xml_path + name +'.xml')
		xmlfile = open(output_xml_path+name+'_filter.xml','w')
		xmlfile.write(dom.toprettyxml(encoding='utf-8'))

def enh_gamma(image,image_name, output_image_path):
	'''lighter'''
	image = io.imread(image)
	name = os.path.splitext(image_name)[0]
	image = img_as_float(image)
	gam_factor = round(random.uniform(0.6,2),1)
	gam = exposure.adjust_gamma(image,gam_factor)
	io.imsave(output_image_path+name+'_gam.jpg', gam)
	dom = xml.dom.minidom.parse(xml_path + name +'.xml')
	xmlfile = open(output_xml_path+name+'_gam.xml','w')
	xmlfile.write(dom.toprettyxml(encoding='utf-8'))

def hist_unif(image, image_name, output_image_path):
	'''uniform histgram'''
	image = io.imread(image)
	name = os.path.splitext(image_name)[0]
	image_hist = exposure.equalize_hist(image)
	io.imsave(output_image_path+ name +'_hist.jpg', image_hist)
	dom = xml.dom.minidom.parse(xml_path + name +'.xml')
	xmlfile = open(output_xml_path+name+'_hist.xml','w')
	xmlfile.write(dom.toprettyxml(encoding='utf-8'))
	return image_hist

def img_sharpness(image, image_name, output_image_path):
	'''enhance image sharpness'''
	image = Image.open(image)
	name = os.path.splitext(image_name)[0]
	sharpness = round(random.uniform(5,10),2)
	enh_sha = ImageEnhance.Sharpness(image)
	image_sharped = enh_sha.enhance(sharpness)
	image_sharped.save(output_image_path+ name +'_sharp.jpg')

	dom = xml.dom.minidom.parse(xml_path + name +'.xml')
	xmlfile = open(output_xml_path+name+'_sharp.xml','w')
	xmlfile.write(dom.toprettyxml(encoding='utf-8'))
	return image_sharped


image_path = "/home/zwb/桌面/img/"
output_image_path = "/home/zwb/桌面/img/"
xml_path = "/home/zwb/桌面/ano/"
output_xml_path = "/home/zwb/桌面/ano/"

filelist = os.listdir(image_path)
for files in filelist:
	image = image_path + files
	temp_noise = random.randint(1,4)
	temp_filter = random.randint(1,2)
	gaussion(image,files, output_image_path)
	add_noise(image,files, temp_noise, output_image_path)
	add_filter(image, files, temp_filter, output_image_path)
	enh_gamma(image,files, output_image_path)
	#hist_unif(image, files, output_image_path)
	img_sharpness(image, files, output_image_path)

2019-08-29 11:25:34 weixin_42233120 阅读数 86
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    40566 人正在学习 去看看 欧阳坚

最近在自学深度学习图像识别,在进行深度学习数据训练的时候,发现自己的数据量太少,就参考网上代码进行数据集扩充,这里把代码放出来,并给出遇到的错误和解决方案,供大家交流学习。

import cv2
import os
import numpy as np
from imgaug import augmenters as iaa

sometimes = lambda aug: iaa.Sometimes(0.5, aug) #建立lambda表达式,
                                                #这里定义sometimes意味有时候做的操作
                                                # 实际上在深度学习的模型训练中,数据增强不能喧宾夺主,
                                                # 如果对每一张图片都加入高斯模糊的话实际上是毁坏了原来数据的特征,
                                                # 因此,我们需要“有时候”做,给这个操作加一个概率。

# 定义一组变换方法.
seq = iaa.Sequential([
    # 选择0到5种方法做变换
    iaa.SomeOf((0, 5),[
        iaa.Fliplr(0.5),  # 对50%的图片进行水平镜像翻转
        iaa.Flipud(0.5),  # 对50%的图片进行垂直镜像翻转

        #将一些图像转换为它们的超像素表示,每幅图像采样20到200个超像素,
        # 但是不要用它们的平均值替换所有的超像素,
        # 只替换其中的一些(p_replace)。
        sometimes(
                    iaa.Superpixels(
                        p_replace=(0, 1.0),
                        n_segments=(20, 200)
                                    )
                 ),
        #用高斯模糊,均值模糊,中值模糊中的一种增强。注意OneOf的用法
        iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.AverageBlur(k=(2, 7)), # 核大小2~7之间,k=((5, 7), (1, 3))时,核高度5~7,宽度1~3
                    iaa.MedianBlur(k=(3, 11)),
                ]),

        sometimes(iaa.Affine(                          #对一部分图像做仿射变换
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},#图像缩放为80%到120%之间
            translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, #平移±20%之间
            rotate=(-45, 45),   #旋转±45度之间
            shear=(-16, 16),    #剪切变换±16度,(矩形变平行四边形)
        )),
                    ]  ,  random_order=True)
                    ]  ,  random_order=True)
# 图片文件相关路径
path = 'D:\\火狐\\火狐下载\\test_pic\\'
y = os.path.exists('D:\\火狐\\火狐下载\\exp_pic\\')
if y == 1:
    savedpath = 'D:\\火狐\\火狐下载\\exp_pic\\'
else:
    os.mkdir('D:\\火狐\\火狐下载\\exp_pic\\')
    savedpath = 'D:\\火狐\\火狐下载\\exp_pic\\'
imglist=[]
filelist = os.listdir(path)  #os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。
# 遍历要增强的文件夹,把所有的图片保存在imglist中
for item in filelist:
    img = cv2.imdecode(np.fromfile(path+item,dtype=np.uint8),-1)

    # print('item is ', item)
    # print('img is ',img.shape)
    imglist.append(img)
    # print('imglist is ' ,imglist)
print('all the picture have been appent to imglist')

#对文件夹中的图片进行增强操作,循环5次
for count in range(5):
    images_aug = seq.augment_images(imglist)
    for index in range(len(images_aug)):
        filename = str(count) + str(index) + '.jpg'
        # 保存图片
        # cv2.imwrite(savedpath + filename, images_aug[index])
        cv2.imencode(".jpg", images_aug[index])[1].tofile(savedpath+filename)
        # '.jpg'表示把当前图片img按照jpg格式编码,按照不同格式编码的结果不一样
        print('image of count%s index%s has been writen' % (count, index))
  1. 遇到的问题
    在读取图片和保存图片的时候,会有这样几个错误

1.1 路径的问题:

windows系统下路径使用双斜线“\”分割,Linux系统使用但斜线“\”。

1.2 cv2不支持中文路径

如果是中文路径的话,OpenCV 函数cv2.imread()、cv2.imwrite()在读取含有中文路径及以中文命名的文件时,会报错,主要原因是因为cv2.imread()、cv2.imwrite()不支持中文。.
在这里插入图片描述此时不会报错但是只返回None
解决方法就是先用先用np.fromfile()读取为np.uint8格式,再使用cv2.imdecode()解码在这里插入图片描述
保存的时候也要修改
在这里插入图片描述

第一次修改

后面在操作的过程出现了这样的错误:
Corrupt JPEG data: 131072 extraneous bytes before marker 0xe2
AttributeError: ‘NoneType’ object has no attribute ‘ndim’
这两个其实属于共同的原因,都是因为这里

img = cv2.imdecode(np.fromfile(path+item,dtype=np.uint8),-1)

返回值为None,因为图片格式出错或者图片有损失导致。 因此在下面加上判断语句,将这样的图片给删除了就好了。其他的不用变

if img is None:
    os.remove(path+item)
    continue
2016-12-02 17:06:53 samylee 阅读数 5385
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    40566 人正在学习 去看看 欧阳坚

俺最近在做深度学习的数据扩充,心得愿与小伙伴们分享大笑

注意喽:有四个程序,我都公布啦吐舌头

c++源程序如下感觉代码效率不高,小伙伴们有任何意见希望不吝赐教):

 

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <fstream>
#include <string>
#include <vector>
#include <io.h>
#include "stdlib.h"
#include <cstdlib>
#include <typeinfo> 

using namespace std;
using namespace cv;

void getFiles(string path, vector<string>& files)
{
	long hFile = 0;
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*.jpg").c_str(), &fileinfo)) != -1)
	{
		do
		{
			files.push_back(p.assign(path).append("\\").append(fileinfo.name));

		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

char * hand_path = "F:\\opencv_extract\\test\\original_images";
char * background_path = "F:\\opencv_extract\\test\\background";

int main()
{	
	vector<string> hand_file;
	vector<string> background_file;

	getFiles(hand_path, hand_file);
	getFiles(background_path, background_file);

	int size_hand_file = hand_file.size();
	int size_background_file = background_file.size();

	for (int i = 0; i < size_hand_file; i++)
	{
		for (int j = 0; j < size_background_file; j++)
		{
			Mat hand_img = imread(hand_file[i]);
			Mat size_hand;
			Size size;
			size.height = 200;
			size.width = hand_img.cols * 200 / hand_img.rows;
			resize(hand_img, size_hand, size);

			Mat background = imread(background_file[j]);
			Mat size_background;
			CvSize back_size;
			back_size.height = 500;
			back_size.width = background.cols * 500 / background.rows;
			resize(background, size_background, back_size);

			////复制图片至另外一张图片////

			for (int row = 0; row < size_hand.rows; row++)
			{
				for (int col = 0; col < size_hand.cols; col++)
				{
					int move_row = row + 150;
					int move_col = col + 150;
					size_background.at<Vec3b>(move_row, move_col)[0] = size_hand.at<Vec3b>(row, col)[0];
					size_background.at<Vec3b>(move_row, move_col)[1] = size_hand.at<Vec3b>(row, col)[1];
					size_background.at<Vec3b>(move_row, move_col)[2] = size_hand.at<Vec3b>(row, col)[2];
				}
			}

			char save_img[40];
			int handfile_size = hand_file[i].size();
			char handfile_name[10];
			for (int x = 0; x < 10; x++)
			{
				int y = x + handfile_size - 9;   //hand1.jpg的长度,根据需要进行修改
				handfile_name[x] = hand_file[i][y];
			}

			IplImage *src;
			src = &IplImage(size_background);
			sprintf(save_img, "F:\\opencv_extract\\test\\img\\%s", handfile_name);
			cvSaveImage(save_img, src);

			////存储xml////

			int min_x = 150;
			int min_y = 150;
			int max_x = 150 + size_hand.cols;
			int max_y = 150 + size_hand.rows;
			int array[] = { min_x, min_y, max_x, max_y };
			
			char save_xml[40];
			char xml_name[6];
			for (int a = 0; a < 6; a++)
			{
				int b = a + handfile_size - 9; 
				xml_name[a] = hand_file[i][b];
				if (b == handfile_size - 4)  //hand1.jpg前hand1的长度,根据需要进行修改
				{
					xml_name[a] = '\0';
					break;
				}
			}
			sprintf(save_xml, "F:\\opencv_extract\\test\\xml\\%s.txt", xml_name);

			ofstream outfile;
			outfile.open(save_xml, 'w');
			outfile << handfile_name << " " << 1 << " " << "[" << endl;
			for (int k = 0; k < 4; k++)
			{
				if (k < 3)
				{
					outfile << array[k] << "," << ' ' << endl;
				}
				else
				{
					outfile << array[k] << "]" << endl;
				}
			}
			outfile.close();
		}
	}
	return 0;
}

 

附件

1、爬数据python工具:链接:http://pan.baidu.com/s/1pLCTzwn 密码:tkpz

2、cpp工程:链接:http://pan.baidu.com/s/1hsAzN7I 密码:5gof

3、voc标签格式程序:链接:http://pan.baidu.com/s/1c19BQbI 密码:v1al

4、yolo标签格式程序:链接:http://pan.baidu.com/s/1nvnpcNn 密码:lpk5

 

效果图如下

 +    = 

 

 

任何问题请加唯一QQ2258205918(名称samylee)!

 

 

 

 

2016-12-05 17:46:37 samylee 阅读数 2152
  • iOS开发教程之OC语言

    Objective-C是扩充C的面向对象编程语言,iOS开发是用的Objective-C语言,本专题更系统的讲解Objective-C语言当中的一些要点,类的封装、基本数据结构(NSString、NSData)、继承、内存管理(retain点语法、MyArray、AutoreleasePool、浅拷贝详述、深拷贝详述)等内容。

    40566 人正在学习 去看看 欧阳坚

俺上个程序说的是在深度学习的趋势下扩充数据集的问题,并附了代码和demo,这次的博客就是上个代码的升级版,提取目标的数据扩充生气

 

话不多说,直接上代码和demo大笑

 

代码如下:

 

#include <cv.h>   
#include <cxcore.h>   
#include <highgui.h>   
#include <vector>
#include "highgui.hpp"
#include <iostream>
#include <fstream>  
#include <string>
#include <io.h>  
#include "stdlib.h"  
#include <cstdlib>  
#include <typeinfo> 

using namespace cv;
using namespace std;

char* hand_path = "C:\\Users\\Administrator\\Desktop\\fore_hand";
char* background_path = "C:\\Users\\Administrator\\Desktop\\back_hand";

void getFiles(string path, vector<string>& files)
{
	long hFile = 0;
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*.png").c_str(), &fileinfo)) != -1)
	{
		do
		{
			files.push_back(p.assign(path).append("\\").append(fileinfo.name));

		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

int count_img = 0;

int main()
{
	vector<string> hand_file;
	vector<string> background_file;

	getFiles(hand_path, hand_file);
	getFiles(background_path, background_file);

	int size_hand_file = hand_file.size();
	int size_background_file = background_file.size();

	for (int i = 0; i < size_hand_file; i++)
	{
		for (int j = 0; j < size_background_file; j++)
		{
			Mat origin_hand = imread(hand_file[i]);
			IplImage* img;
			img = &IplImage(origin_hand);
			IplImage* bin = cvCreateImage(cvGetSize(img), 8, 1);

			cvCvtColor(img, bin, CV_BGR2GRAY);  //灰度
			cvSmooth(bin, bin, CV_MEDIAN, 3, 3, 0); //平滑
			cvCanny(bin, bin, 255, 255, 3); //边缘
			cvThreshold(bin, bin, 200, 255, CV_THRESH_BINARY); //二值化

			int bin_width = bin->width;
			int bin_height = bin->height;

			for (int k = 0; k < bin_height; k++)
			{
				int col = bin_width - 2;
				((uchar *)(bin->imageData + k*bin->widthStep))[col] = 255;
			}

			CvSeq *pContour = NULL;
			CvMemStorage *pStorage = NULL;

			pStorage = cvCreateMemStorage(0);
			cvFindContours(bin, pStorage, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); //轮廓

			cvDrawContours(bin, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 1, CV_FILLED, 8, cvPoint(0, 0)); //画

			cvReleaseMemStorage(&pStorage);
			pStorage = NULL;

			cvErode(bin, bin, NULL, 1); //腐蚀

			IplImage* desc;
			CvSize sz;
			sz.height = 200;
			sz.width = bin->width * 200 / bin->height;
			desc = cvCreateImage(sz, bin->depth, bin->nChannels);
			cvResize(bin, desc, CV_INTER_CUBIC);

			Mat img_mat(img, 1);
			Mat size_img;
			Size size;
			size.height = 200;
			size.width = img_mat.cols * 200 / img_mat.rows;
			resize(img_mat, size_img, size);

			Mat background = imread(background_file[j]);
			Mat size_background;
			Size size_back;
			size_back.height = 500;
			size_back.width = background.cols * 500 / background.rows;
			resize(background, size_background, size_back);

			////复制img////

			for (int row = 0; row < desc->height; row++)
			{
				for (int col = 0; col < desc->width; col++)
				{
					if (((uchar *)(desc->imageData + row*desc->widthStep))[col] == 255)
					{
						int move_row = row + 150;
						int move_col = col + 150;
						size_background.at<Vec3b>(move_row, move_col)[0] = size_img.at<Vec3b>(row, col)[0];
						size_background.at<Vec3b>(move_row, move_col)[1] = size_img.at<Vec3b>(row, col)[1];
						size_background.at<Vec3b>(move_row, move_col)[2] = size_img.at<Vec3b>(row, col)[2];
					}
				}
			}

			////存储img////

			char save_img[100];
			IplImage *src;
			src = &IplImage(size_background);
			sprintf(save_img, "C:\\Users\\Administrator\\Desktop\\result\\hand_%d.png", count_img);
			cvSaveImage(save_img, src);

			printf("save_img: %d ---> DONE!\n",count_img);
			
			////存储xml///

			int min_x = 150;
			int min_y = 150;
			int max_x = 150 + size_img.cols;
			int max_y = 150 + size_img.rows;
			int array[] = { min_x, min_y, max_x, max_y };

			char save_xml[100];
			sprintf(save_xml, "C:\\Users\\Administrator\\Desktop\\xml\\hand_%d.txt", count_img);

			ofstream outfile;
			outfile.open(save_xml, 'w');
			outfile << "hand_" <<count_img<<".txt"<< " " << 1 << " " << "[" << endl;
			for (int k = 0; k < 4; k++)
			{
				if (k < 3)
				{
					outfile << array[k] << "," << ' ' << endl;
				}
				else
				{
					outfile << array[k] << "]" << endl;
				}
			}
			outfile.close();

			printf("save_xml: %d ---> DONE!\n", count_img);

			count_img++;

		}
	}
	system("pause");
	return 0;
}

demo如下:

 


  +  = 

 

 

任何问题请加唯一QQ2258205918(名称samylee)!

 

没有更多推荐了,返回首页