【图像识别】图像特征提取备忘

来源:互联网 发布:银联数据2017offer待遇 编辑:程序博客网 时间:2024/06/10 20:06

提取的是一阶不变距,二阶不变距,外接矩形面积比,外接矩形长宽比,紧凑度五个特征值。
留自己备忘。还不知道可不可以做到识别。

#include <cv.h>#include <highgui.h>#include <stdio.h> #include <iostream>#include <fstream> using   namespace   std; char* filename = "D:\\数据集\\数据集\\1.png";int ithreshold = 0;IplImage *Image1, *Image2;CvMemStorage *stroage;IplImage *contourimage;CvSeq *seq = NULL;int ivalue = 0;int ku = -1;void changebar(int){    ithreshold = ivalue;    cvThreshold(Image1, Image2, 150, 255, CV_THRESH_TRUNC);    int numcontours = cvFindContours(Image2, stroage, &seq, sizeof(CvContour), CV_RETR_LIST);    CvMoments moments;    CvHuMoments hu;    cvMoments(Image2, &moments, 0);    cvGetHuMoments(&moments, &hu);    cout << hu.hu1 << "/" << hu.hu2 << "/" << hu.hu3 << "/" << hu.hu4 << "/" << hu.hu5 << "/" << hu.hu6 << "/" << hu.hu7 << "/" << "/" << endl;    if (ku != numcontours)    {        ku = numcontours;        printf("contournum:::: %d \n", numcontours);    }    CvSeq *c = 0;    int zz = 0;    int totl = 0;    cvSet(contourimage, cvScalar(255, 255, 255));    cvSet(contourimage, cvScalar(125, 125, 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 >= 1000 && testdbLength <= 1000)        {            //点集的最外面(up-right)矩形边界            CvRect testrect = cvBoundingRect(c);            //轮廓最小外界矩形            CvBox2D testbox = cvMinAreaRect2(c);            //在图像上绘制外部和内部轮廓            cvDrawContours(contourimage, c, cvScalar(0, 0, 255), cvScalar(0, 0, 0), 0, 2);            //通过对角线上的两个顶点绘制简单、指定粗细或者带填充的矩形            cvRectangle(contourimage, cvPoint(testrect.x, testrect.y + testrect.height), cvPoint(testrect.x + testrect.width, testrect.y), cvScalar(0, 0, 0), 2);            //计算面积            double sq = testrect.width*testrect.height;            double bili = (double)testrect.height/testrect.width;            //找外界圆            cvMinEnclosingCircle(c, &center, &radius);            //画外接圆            cvCircle(contourimage, cvPointFrom32f(center), (int)radius, cvScalar(0, 0, 0), 2);            //计算输入的二维盒子顶点            cvBoxPoints(testbox, rectpoint);            //cvRectangle(contourimage, cvPoint(testrect.x, testrect.y + testrect.height), cvPoint(testrect.x + testrect.width, testrect.y), cvScalar(0, 0, 0), 2);            for (int i = 0; i<4; i++)            {                //cvLine(contourimage, cvPointFrom32f(rectpoint[i]), cvPointFrom32f(rectpoint[(i + 1) % 4]), CV_RGB(0, 0, 255), 2);            }            cvShowImage("grotto", contourimage);            totl++;            printf("contournum:::: %d   轮廓面积::::  %f   轮廓长度::::  %f    和外接矩形面积比例::::%f   外接矩形长宽比:::%f \n", zz, testdbArea, testdbLength, testdbArea/sq,bili);            float jincou = testdbArea / (testdbLength*testdbLength);            printf("紧凑度:%f\n", jincou);            ofstream myfile("C:/Users/s/Desktop/get.txt", ios::out);            myfile << hu.hu1 << "," << hu.hu2 <<  "," << testdbArea / sq << "," << bili <<","<<jincou<< endl;            cvWaitKey(0);        }        zz++;        //          cvWaitKey(0);       }    printf("totl:::: %d     \n", totl);    cvSaveImage("zzzz.jpg", contourimage);};int main(){    Image1 = cvLoadImage(filename, 0);    Image2 = cvCreateImage(cvGetSize(Image1), 8, 1);    cvNamedWindow("grotto", 1);    contourimage = cvCreateImage(cvGetSize(Image1), 8, 3);    stroage = cvCreateMemStorage();    ivalue = 1;    changebar(10);    int iexit = 0;    while (iexit != 32)    {        iexit = cvWaitKey(0);    }    cvDestroyWindow("grotto");    exit(1);    return 0;}
0 0
原创粉丝点击