libsvm+HOG

#include "stdafx.h"




#include "opencv\cv.h"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\core\core.hpp"
#include "opencv\highgui.h"
#include "opencv2\legacy\compat.hpp"
#include "opencv2\legacy\legacy.hpp"
#include "opencv2\ml\ml.hpp"
#include "opencv2\objdetect\objdetect.hpp"
//#include "featurepyramid.cpp"
using namespace cv;
#include "_latentsvm.h"
#include "FHOG.hpp"
//#include "MxArray.hpp"


#ifdef _DEBUG
#pragma comment(lib, "opencv_calib3d249d.lib")
#pragma comment(lib, "opencv_features2d249d.lib")
#pragma comment(lib, "opencv_flann249d.lib")
#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_imgproc249d.lib")
#pragma comment(lib, "opencv_highgui249d.lib")
#pragma comment(lib, "opencv_legacy249d.lib")
#pragma comment(lib, "opencv_objdetect249d.lib")
#pragma comment(lib, "opencv_video249d.lib")
#pragma comment(lib, "opencv_ml249d.lib")
#pragma comment(lib, "opencv_ts249d.lib")
#else
#pragma comment(lib, "opencv_calib3d249.lib")
#pragma comment(lib, "opencv_features2d249.lib")
#pragma comment(lib, "opencv_flann249.lib")
#pragma comment(lib, "opencv_core249.lib")
#pragma comment(lib, "opencv_imgproc249.lib")
#pragma comment(lib, "opencv_highgui249.lib")
#pragma comment(lib, "opencv_legacy249.lib")
#pragma comment(lib, "opencv_objdetect249.lib")
#pragma comment(lib, "opencv_video249.lib")
#pragma comment(lib, "opencv_ml249.lib")
#pragma comment(lib, "opencv_ts249.lib")
#endif


#if defined(WIN32) || defined(_WIN32)
#include <io.h>
#else
#include <dirent.h>
#endif


using namespace cv;


static int clas;


string main_path = "D:\\workspace\\xx科技\\500例舌象分类2\\舌色\\白舌\\淡白舌\\";
string main_path1 = "";
string main_path2 = "D:\\workspace\\xx科技\\500例舌象分类2\\舌色\\白舌\\淡白舌\\";
    string train_path = main_path + "train_tougue.txt";


static void printavg(float avg[], int isize)
{





FILE *fid=fopen(train_path.c_str(),"a+");
fprintf_s(fid, "%d ", clas);
        for(int i=0;i<isize;i++)
        {  
                fprintf(fid, "%d%s%lf%s", i+1,":",avg[i]," ");
        }
        fprintf(fid,"\n");
     fclose(fid);


        /*printf(clas+" ");


        for(int i=0;i<12;i++)
        {  
                printf(i+1+":"+avg[i]+" ");
        }
        printf("\n");*/
}
float *val = nullptr;
//float *average = nullptr;
static float* calavg(int histogram[], float* average)
    {
//if(average != nullptr)
//{
// //delete[] average;
// //average = nullptr;
//}
//else
//{
// average=new float[10];
//}
        for(int i=0;i<4;i++)
        {
            int sum=0;
            for(int j=i*64;j<i*64+64;j++)
            {
                sum+=histogram[j];
            }
            average[i]=((float)sum)/64;
        }
        
        return average;        
    }
 
static void readDirectory( const string& directoryName, vector<string>& filenames, bool addDirectoryName=true )
{
    filenames.clear();


#if defined(WIN32) | defined(_WIN32)
    struct _finddata_t s_file;
    string str = directoryName + "\\*.*";


    intptr_t h_file = _findfirst( str.c_str(), &s_file );
    if( h_file != static_cast<intptr_t>(-1.0) )
    {
        do
        {
            if( addDirectoryName )
                filenames.push_back(directoryName + "\\" + s_file.name);
            else
                filenames.push_back((string)s_file.name);
        }
        while( _findnext( h_file, &s_file ) == 0 );
    }
    _findclose( h_file );
#else
    DIR* dir = opendir( directoryName.c_str() );
    if( dir != NULL )
    {
        struct dirent* dent;
        while( (dent = readdir(dir)) != NULL )
        {
            if( addDirectoryName )
                filenames.push_back( directoryName + "/" + string(dent->d_name) );
            else
                filenames.push_back( string(dent->d_name) );
        }


        closedir( dir );
    }
#endif


    sort( filenames.begin(), filenames.end() );
}




static void calculateIntensityHistogram(const Mat& mat, int width, int height)
    {

cv::Mat bgr = Mat(height, width, mat.type());
mat.copyTo(bgr);
resize(bgr, bgr, Size(48, 64));
//rgbMat.convertTo(bgr, CV_8UC3);






// Cell size
int cellSize = 4;


// Scale applied
int scale = 1;


// Create object
HogFeature FHOG(cellSize, scale);


// Extract FHOG features
cv::Mat features = FHOG.getFeature(bgr);


int iCount = features.cols;
printf("%d\n", iCount);
float *avg=new float[iCount];
int n=0;
for (int i = 0; i < iCount; i++)
{
avg[n++] = features.at<float>(0, i);
}
printavg(avg, iCount);
delete[] avg;
    }
    
    


int main()
{
//CvSVM cs;
//cs.load("G:\\ageModel.txt");
//Mat mat = imread("G:\\img\\20160115131636824_3.jpg", 0);
//Mat mat1 = Mat(mat.size(), CV_32FC1);
//cvScale(&(CvMat)mat, &(CvMat)mat1);
//bool bl  =true;
//int m = 10000, n = cs.get_var_count();
//float fl = cs.predict(mat1, bl);\


//string main_path = "D:\\workspace\\xx科技\\500例舌象分类2\\舌色\\白舌\\淡白舌\\";
//string main_path1 = "";
int i_index = 0;
int bl = 0;




train_path = main_path + "train_tougue.txt";
FILE *fid=fopen(train_path.c_str(),"w");
     fclose(fid);


     train_path = main_path+"test_tougue.txt";
fid=fopen(train_path.c_str(),"w");
fclose(fid);


{




main_path = "D:\\workspace\\xx科技\\齿痕数据\\齿痕\\";
main_path1 = "";
vector<string> filenames;
readDirectory(main_path, filenames, false);


int icount = filenames.size();


for(int i = 0; i < icount; i ++)
{
 
int ipos = filenames[i].find("png");
int ipos1 = filenames[i].find("jpg");
int ipos2 = filenames[i].find("bmp");
if(ipos == -1 && ipos1 == -1 && ipos2 == -1)
{
continue;
}
main_path1 = main_path + filenames[i];
Mat bgr = imread(main_path1);
cvtColor(bgr,bgr,CV_BGR2RGB);


 


Mat mat = Mat(bgr.size(), CV_32SC3);
bgr.convertTo(mat, CV_32SC3);


int* rgb= new int[mat.rows * mat.cols];
        int x=mat.cols;
        int y=mat.rows;
        int m=0,n,p;  
             
int iemSize = mat.elemSize();
int iii = CV_ELEM_SIZE(DataType<int>::type);
        for( n=0;n<y;n++) 
        {
            for( p=0;p<x;p++)
            {
int r = mat.at<int>(n,p);
int g = mat.at<int>(n,p+1);
int b = mat.at<int>(n,p+2);

rgb[m++] = (r<< 16 + g << 8 + b);
            }
        }


clas = 1;
if(i < icount * 4 / 5)
{
train_path = main_path2 + "train_tougue.txt";
calculateIntensityHistogram(bgr, mat.cols, mat.rows);
}
else
{
train_path = main_path2 + "test_tougue.txt";
calculateIntensityHistogram(bgr, mat.cols, mat.rows);
}
}


}
/
{
clas = 2;




main_path = "D:\\workspace\\xx科技\\齿痕数据\\非齿痕\\";
main_path1 = "";




vector<string> filenames;
readDirectory(main_path, filenames, false);


int icount = filenames.size();


for(int i = 0; i < icount; i ++)
{
int ipos = filenames[i].find("png");
int ipos1 = filenames[i].find("jpg");
int ipos2 = filenames[i].find("bmp");
if(ipos == -1 && ipos1 == -1 && ipos2 == -1)
{
continue;
}
main_path1 = main_path + filenames[i];
Mat bgr = imread(main_path1);
cvtColor(bgr,bgr,CV_BGR2RGB);


 


Mat mat = Mat(bgr.size(), CV_32SC3);
bgr.convertTo(mat, CV_32SC3);


int* rgb= new int[mat.rows * mat.cols];
        int x=mat.cols;
        int y=mat.rows;
        int m=0,n,p;  
             
int iemSize = mat.elemSize();
int iii = CV_ELEM_SIZE(DataType<int>::type);
        for( n=0;n<y;n++) 
        {
            for( p=0;p<x;p++)
            {
int r = mat.at<int>(n,p);
int g = mat.at<int>(n,p+1);
int b = mat.at<int>(n,p+2);

rgb[m++] = (r<< 16 + g << 8 + b);
            }
        }


if(i < icount * 4 / 5)
{
train_path = main_path2 + "train_tougue.txt";
calculateIntensityHistogram(bgr, mat.cols, mat.rows);
}
else
{
train_path = main_path2 + "test_tougue.txt";
calculateIntensityHistogram(bgr, mat.cols, mat.rows);
}
}


}
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值