您的位置:首页 > 运维架构

【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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: