扩充数据集升级版 --> 提取目标

来源:互联网 发布:jd软件 编辑:程序博客网 时间:2024/06/05 05:42

俺上个程序说的是在深度学习的趋势下扩充数据集的问题,并附了代码和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如下:


   = 

0 0
原创粉丝点击