【OpenCV】批量集合特征提取,本地保存成向量
2016-08-16 17:33
295 查看
把之前集合几篇博客用到的知识结合起来了。
#include<cv.h> #include<highgui.h> #include<io.h> #include <string.h> #include <iostream> #include <fstream> using namespace cv; using namespace std; CvMemStorage *stroage; IplImage *contourimage; CvSeq *seq = NULL; int ku = -1; float pi = 3.14159; char * filePath = "E:\\cnn训练集\\第三种植物"; char * result = "E:\\cnn训练集\\third.txt"; void getFiles(string path, vector<string>& files) { //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do { //如果是目录,迭代之 //如果不是,加入列表 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) getFiles(p.assign(path).append("\\").append(fileinfo.name), files); } else { files.push_back(p.assign(path).append("\\").append(fileinfo.name)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } int main() { vector<string> files; getFiles(filePath, files); char str[30]; int size = files.size(); IplImage *image, *imageresize = 0; for (int i = 0; i < size; i++) { image = cvLoadImage(files[i].c_str()); cv::Mat I(image, false); stroage = cvCreateMemStorage(); contourimage = cvCreateImage(cvGetSize(&(CvMat)I), 8, 1); cvtColor(I, I, CV_BGR2GRAY); Mat contours; Canny(I, contours, 125, 255); threshold(contours, contours, 128, 255, CV_THRESH_TRUNC); int numcontours = cvFindContours(&(CvMat)contours, stroage, &seq, sizeof(CvContour), CV_RETR_LIST); CvMoments moments; CvHuMoments hu; cvMoments(&(CvMat)I, &moments, 0); cvGetHuMoments(&moments, &hu); //if (ku != numcontours) //{ // ku = numcontours; // printf("contournum:::: %d \n", numcontours); //} CvSeq *c = 0; int zz = 0; int totl = 0; cvSet(contourimage, cvScalar(255, 0, 255)); cvSet(contourimage, cvScalar(125, 0, 125)); CvPoint2D32f center; float radius; CvPoint2D32f rectpoint[4]; CvContour *testcontour = 0; //c为轮廓顶点数组 for (c = seq; c != NULL; c = c->h_next) { // 取得轮廓面积 double testdbArea = fabs(cvContourArea(c, CV_WHOLE_SEQ)); //取得轮廓长度 double testdbLength = cvArcLength(c); c->block_max; if (testdbArea >=15 && testdbLength <= 50000) { //点集的最外面(up-right)矩形边界 CvRect testrect = cvBoundingRect(c); //轮廓最小外界矩形 CvBox2D testbox = cvMinAreaRect2(c); //cvDrawContours(&(CvMat)contours, c, cvScalar(0, 255, 255), cvScalar(0, 255, 0), 0, 2); cvRectangle(&(CvMat)contours, cvPoint(testrect.x, testrect.y + testrect.height), cvPoint(testrect.x + testrect.width, testrect.y), cvScalar(255, 0, 255), 1); double width = testrect.width; double height = testrect.height; double juxingmianji = width*height; //找外界圆 //cvMinEnclosingCircle(c, ¢er, &radius); //cout << radius << endl; //cout << center.x << ";" << center.y << endl; //画外接圆 //cvCircle(&(CvMat)contours, cvPointFrom32f(center), (int)radius, cvScalar(255, 0, 255), 2); //特征1矩形度 float mianjibi = testdbArea / juxingmianji; //特征2延长度 float changkuanbi = width / height; //特征3周长比 float zhouchangbi = 2 * (height + width) / testdbLength; //特征4似圆度 float r = 4 * pi*testdbArea / (testdbLength*testdbLength); //特征5形状复杂性 float e = (testdbLength*testdbLength) / testdbArea; //不动点特征 hu.hu1 hu.hu2 ofstream myfile(result, ios::app); myfile << mianjibi << "," << changkuanbi << "," << zhouchangbi << "," << r << "," << e << "," << hu.hu1 << endl; cout << "矩形度:" << mianjibi << endl; cout << "延长度:" << changkuanbi << endl; cout << "周长比:" << zhouchangbi << endl; cout << "似圆度:" << r << endl; cout << "形状复杂性:" << e << endl; cout << "一阶矩:" << hu.hu1 << endl; cout << "二阶矩:" << hu.hu2 << endl; } waitKey(); } } return 0; }
相关文章推荐
- PowerShell 远程管理之 about_Remote_Troubleshooting
- linux 作为服务器之对比windows
- 【Linux】crontab 定时任务
- 1号店交易系统架构如何向「高并发高可用」演进
- CentOS7安装xrdp
- 1年内4次架构调整,谈Nice的服务端架构变迁之路
- linux程序包管理--rpm命令管理方式
- CentOS 6.5上的Tomcat启动报错问题
- 膨胀 腐蚀 开闭运算
- 从单体架构迁移到微服务,8个关键的思考、实践和经验
- Linux动态频率调节系统CPUFreq之一:概述
- spark学习网站--不错的资料
- 如何利用自己的电脑做服务器发布tomcat的WEB项目供外网访问
- shell练习题2
- 构建高并发高可用的电商平台架构实践
- Centos6.6搭建nginx服务及配置文件详解
- 容器的性能监控和日志管理
- 从0开始学CentOS7(1)
- tomcat clean时 eclipse报错
- 【Linux开发】Ubuntu下几个软件的配置记录backup