예제 #1
0
void computeHOGs( const Size wsize, const vector< Mat > & img_lst, vector< Mat > & gradient_lst, bool use_flip )
{
    HOGDescriptor hog;
    hog.winSize = wsize;
    Mat gray;
    vector< float > descriptors;

    for( size_t i = 0 ; i < img_lst.size(); i++ )
    {
        if ( img_lst[i].cols >= wsize.width && img_lst[i].rows >= wsize.height )
        {
            Rect r = Rect(( img_lst[i].cols - wsize.width ) / 2,
                          ( img_lst[i].rows - wsize.height ) / 2,
                          wsize.width,
                          wsize.height);
            cvtColor( img_lst[i](r), gray, COLOR_BGR2GRAY );
            hog.compute( gray, descriptors, Size( 8, 8 ), Size( 0, 0 ) );
            gradient_lst.push_back( Mat( descriptors ).clone() );
            if ( use_flip )
            {
                flip( gray, gray, 1 );
                hog.compute( gray, descriptors, Size( 8, 8 ), Size( 0, 0 ) );
                gradient_lst.push_back( Mat( descriptors ).clone() );
            }
        }
    }
}
예제 #2
0
vector<float> Hog(Mat image)
{
	vector<float> descriptors;
	HOGDescriptor* hog = new HOGDescriptor(cvSize(60, 60), cvSize(10, 10), cvSize(5, 5), cvSize(5, 5), 9);
	hog->compute(image,descriptors, Size(1, 1), Size(0, 0));
	return descriptors;
}
예제 #3
0
파일: main.cpp 프로젝트: Modasshir/paper
void load_images(const string & filename, int label) {
	HOGDescriptor hog;
	hog.winSize = size;
	string line;
	ifstream file;
	vector<float> descriptors;
	vector<Point> locations;
	file.open(filename.c_str());

	if (!file.is_open()) {
		cout << "file cannot be opened" << endl;
		exit(-1);
	}

	while (true) {

		getline(file, line);
		if (line == "") {
			break;
		}

		Mat img = imread(line.c_str(), 0);
		if (img.empty())
			continue;
		resize(img, img, size);
		hog.compute(img, descriptors, Size(8, 8), Size(0, 0), locations);
		training_list.push_back(Mat(descriptors).clone());
		training_label.push_back(label);
		img.release();

	}
	cout << training_list.size() << endl;
	cout << training_label.size() << endl;

}
예제 #4
0
파일: main.cpp 프로젝트: aaronguo1996/prp
int hog(string name, int i)
{
	int ImgWidht = 120;
	int ImgHeight = 120;

	Mat src;
	Mat trainImg = Mat::zeros(ImgHeight, ImgWidht, CV_8UC3);//需要分析的图片  
	
	src = imread(name.c_str(), 1);
	//cout << "HOG: processing " << name.c_str() << endl;
	resize(src, trainImg, cv::Size(ImgWidht, ImgHeight), 0, 0, INTER_CUBIC);
	HOGDescriptor *hog = new HOGDescriptor(cvSize(ImgWidht, ImgHeight), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);     
	
	vector<float>descriptors;//结果数组     
	hog->compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //调用计算函数开始计算
	if (i == 0)
	{
		//descSize = descriptors.size();
		data_mat = Mat::zeros(nLine, descriptors.size(), CV_32FC1); //根据输入图片大小进行分配空间 
		//fusion_mat = Mat::zeros(nLine, descriptors.size() + MATSIZE + GLCMSIZE, CV_32FC1);
	}
	int n = 0;
	for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
	{
		data_mat.at<float>(i, n) = *iter;
		//fusion_mat.at<float>(i, n) = *iter;
		n++;
	}
	//cout << "HOG: end processing " << name.c_str() << endl;
	delete hog;
	
	return 0;
}
예제 #5
0
/**
 * This is the actual calculation from the (input) image data to the HOG descriptor/feature vector using the hog.compute() function
 * @param imageFilename file path of the image file to read and calculate feature vector from
 * @param descriptorVector the returned calculated feature vector<float> , 
 *      I can't comprehend why openCV implementation returns std::vector<float> instead of cv::MatExpr_<float> (e.g. Mat<float>)
 * @param hog HOGDescriptor containin HOG settings
 */
static void calculateFeaturesFromInput(const string& imageFilename, vector<float>& featureVector, HOGDescriptor& hog) {
    /** for imread flags from openCV documentation, 
     * @see http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat imread(const string& filename, int flags)
     * @note If you get a compile-time error complaining about following line (esp. imread),
     * you either do not have a current openCV version (>2.0) 
     * or the linking order is incorrect, try g++ -o openCVHogTrainer main.cpp `pkg-config --cflags --libs opencv`
     */
    Mat imageData = imread(imageFilename, 0);
    if (imageData.empty()) {
        featureVector.clear();
        printf("Error: HOG image '%s' is empty, features calculation skipped!\n", imageFilename.c_str());
        return;
    }
    // hack: change dimensions
    //Size size(32,64);
    //resize(imageData, imageData, size);
    
    // Check for mismatching dimensions
    if (imageData.cols != hog.winSize.width || imageData.rows != hog.winSize.height) {
        featureVector.clear();
        printf("Error: Image '%s' dimensions (%u x %u) do not match HOG window size (%u x %u)!\n", imageFilename.c_str(), imageData.cols, imageData.rows, hog.winSize.width, hog.winSize.height);
        return;
    }
    vector<Point> locations;
    hog.compute(imageData, featureVector, winStride, trainingPadding, locations);
    imageData.release(); // Release the image again after features are extracted
}
예제 #6
0
void kNNSearcher::kNNSearchWithHOG(const Mat &inputImage, const QStringList &imPath,
                      Mat &indexes, Mat &weights, int k)
{
    //resize inputImage to the same size of training image
    Mat temp = imread( imPath[0].toLocal8Bit().data(), CV_LOAD_IMAGE_GRAYSCALE );
    Mat inputIm;
    resize( inputImage, inputIm, temp.size() );

    //compute the HOG descriptor of target image
    HOGDescriptor *hogDesr = new HOGDescriptor( cvSize( 640, 480 ), cvSize( 160, 120 ), cvSize( 160,120 ), cvSize( 160, 120 ), 9  );
    std::vector<float> targetDescriptor;
    hogDesr->compute( inputIm, targetDescriptor, Size( 0, 0 ), Size( 0, 0) );
    //###################################################################################

    //load the training descriptors into descriptorMat if there exist a HOGof44blocks.yaml file
    //otherwise, execute the train program
    Mat descriptorMat;

    QString const HOGMatfile = "HOGof44blocks.yaml";
    FileStorage fs;
    fs.open( HOGMatfile.toLocal8Bit().data(), FileStorage::READ );
    if( fs.isOpened() ){
        // the HOGof44blocks.yaml does exist
        fs["HOGMat"] >> descriptorMat;
    }else{
//not using hole traffic ligh as samples,just use the square light
int RecognizeLight(IplImage* srcImg,CvRect iRect)
{
	CvSize cutSize;
	cutSize.width=iRect.width;
	cutSize.height=iRect.height;
	IplImage *tmpCutImg=cvCreateImage(cutSize,srcImg->depth,srcImg->nChannels);
	GetImageRect(srcImg,iRect,tmpCutImg);
#if IS_CUTIMG
	cvShowImage("tmpCutImg",tmpCutImg);
	cvWaitKey(1);
	char tmpName[100];
	static int ppp=0;
	ppp++;
	sprintf_s(tmpName,"ImgCut//%d.jpg",ppp);
	cvSaveImage(tmpName,tmpCutImg);
#endif

	Mat cutMat(tmpCutImg);
	Mat tmpTLRec;
	vector<float> descriptor;

	//识别信号灯类别
	resize(cutMat,tmpTLRec,Size(TLREC_WIDTH,TLREC_HEIGHT));
	TLRecHOG.compute(tmpTLRec,descriptor,Size(8,8));
	int DescriptorDim=descriptor.size();		
	Mat SVMTLRecMat(1,DescriptorDim,CV_32FC1);
	for(int i=0; i<DescriptorDim; i++)
		SVMTLRecMat.at<float>(0,i) = descriptor[i];

	int result=TLRecSVM.predict(SVMTLRecMat);
	cvReleaseImage(&tmpCutImg);
	return result;
}
JNIEXPORT void JNICALL Java_org_opencv_objdetect_HOGDescriptor_compute_10
  (JNIEnv* env, jclass , jlong self, jlong img_nativeObj, jlong descriptors_mat_nativeObj, jdouble winStride_width, jdouble winStride_height, jdouble padding_width, jdouble padding_height, jlong locations_mat_nativeObj)
{
    static const char method_name[] = "objdetect::compute_10()";
    try {
        LOGD("%s", method_name);
        vector<float> descriptors;
        Mat& descriptors_mat = *((Mat*)descriptors_mat_nativeObj);
        vector<Point> locations;
        Mat& locations_mat = *((Mat*)locations_mat_nativeObj);
        Mat_to_vector_Point( locations_mat, locations );
        HOGDescriptor* me = (HOGDescriptor*) self; //TODO: check for NULL
        Mat& img = *((Mat*)img_nativeObj);
        Size winStride((int)winStride_width, (int)winStride_height);
        Size padding((int)padding_width, (int)padding_height);
        me->compute( img, descriptors, winStride, padding, locations );
        vector_float_to_Mat( descriptors, descriptors_mat );
        return;
    } catch(const std::exception &e) {
        throwJavaException(env, &e, method_name);
    } catch (...) {
        throwJavaException(env, 0, method_name);
    }
    return;
}
예제 #9
0
OCL_TEST_P(HOG, GetDescriptors)
{
    HOGDescriptor hog;
    hog.gammaCorrection = true;

    hog.setSVMDetector(hog.getDefaultPeopleDetector());

    std::vector<float> cpu_descriptors;
    std::vector<float> gpu_descriptors;

    OCL_OFF(hog.compute(img, cpu_descriptors, hog.winSize));
    OCL_ON(hog.compute(uimg, gpu_descriptors, hog.winSize));

    Mat cpu_desc(cpu_descriptors), gpu_desc(gpu_descriptors);

    EXPECT_MAT_SIMILAR(cpu_desc, gpu_desc, 1e-1);
}
예제 #10
0
파일: feature.cpp 프로젝트: lostoy/CarRec
bool getFeatureFromImg(Mat img,vector<float> &feature)
{
	HOGDescriptor hog;
	hog.winSize=Size(img.cols,img.rows);
	Size winStride=Size(16,16);
	

	hog.compute(img,feature,winStride);
	return true;
}
예제 #11
0
파일: main.cpp 프로젝트: aaronguo1996/prp
int testHOG(Mat data, Mat res)
{
	CvSVM svm;
	CvSVMParams param;
	CvTermCriteria criteria;
	criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
	param = CvSVMParams(CvSVM::C_SVC, CvSVM::LINEAR, 10.0, 0.1, 0.09, 100.0, 0.5, 1.0, NULL, criteria);//for hog
	svm.train(data, res, Mat(), Mat(), param);
	int ImgWidth = 120;
	int ImgHeight = 120;
	string buf;
	vector<string> img_tst_path;
	ifstream img_tst("SVM_TEST.txt");
	while (img_tst)
	{
		if (getline(img_tst, buf))
		{
			img_tst_path.push_back(buf);
		}
	}
	img_tst.close();

	Mat test;
	Mat trainImg = Mat::zeros(ImgHeight, ImgWidth, CV_8UC3);//需要分析的图片  
	char line[512];
	ofstream predict_txt("SVM_PREDICT_HOG.txt");
	for (string::size_type j = 0; j != img_tst_path.size(); j++)
	{
		test = imread(img_tst_path[j].c_str(), 1);//读入图像   
		resize(test, trainImg, cv::Size(ImgWidth, ImgHeight), 0, 0, INTER_CUBIC);//要搞成同样的大小才可以检测到       
		HOGDescriptor *hog = new HOGDescriptor(cvSize(ImgWidth, ImgHeight), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);
		vector<float>descriptors;//结果数组     
		hog->compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //调用计算函数开始计算 
		cout << "The Detection Result:" << endl;

		Mat SVMtrainMat = Mat::zeros(1, descriptors.size(), CV_32FC1);
		int n = 0;
		for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
		{
			SVMtrainMat.at<float>(0, n) = *iter;
			n++;
		}

		int ret = svm.predict(SVMtrainMat);
		res_hog.push_back(ret);
		std::sprintf(line, "%s\t%d\n", img_tst_path[j].c_str(), ret);
		printf("%s %d\n", img_tst_path[j].c_str(), ret);
		predict_txt << line;
		delete hog;
	}
	predict_txt.close();
	return 0;
}
int isTL(IplImage* srcImg,CvRect iRect,bool isVertical)
{
	CvSize cutSize;
	cutSize.width=iRect.width;
	cutSize.height=iRect.height;
	IplImage *tmpCutImg=cvCreateImage(cutSize,srcImg->depth,srcImg->nChannels);
	GetImageRect(srcImg,iRect,tmpCutImg);

	Mat cutMat(tmpCutImg);
	Mat tmpIsTL;
	vector<float> descriptor;

	//识别信号灯类别
	if (isVertical){
		resize(cutMat, tmpIsTL, Size(HOG_TLVertical_Width, HOG_TLVertical_Height));
		myHOG_vertical.compute(tmpIsTL, descriptor, Size(8, 8));
	}
	else{
		resize(cutMat, tmpIsTL, Size(HOG_TLHorz_Width, HOG_TLHorz_Height));
		myHOG_horz.compute(tmpIsTL, descriptor, Size(8, 8));
	}
		
	int DescriptorDim=descriptor.size();		
	Mat SVMTLRecMat(1,DescriptorDim,CV_32FC1);
	for(int i=0; i<DescriptorDim; i++)
		SVMTLRecMat.at<float>(0,i) = descriptor[i];

	//int result=isTLSVM.predict(SVMTLRecMat);
	int result = 0;
	if (isVertical)
		result = isVerticalTLSVM.predict(SVMTLRecMat);
	else
	{
		result = isHorzTLSVM.predict(SVMTLRecMat);
	}
	cvReleaseImage(&tmpCutImg);
	return result;
}
예제 #13
0
int main()
{
	Mat test;
	char result[300]; //存放預測結果
	//Ptr<ml::SVM> svm = ml::SVM::create();
	//svm->Algorithm::load<ml::SVM>("HOG_SVM_DATA.xml");//加仔訓練好的數字,這裡是10K手寫數字
	Ptr<ml::SVM> svm = ml::SVM::load("HOG_SVM_DATA.xml");
		//檢測樣本
	test = imread("test.bmp", 1); //測試圖片
	if (test.empty())
	{
		cout << "not exist" << endl;
		return -1;
	}
	cout << "load image done" << endl;
	Mat trainTempImg= Mat::zeros(28, 28, CV_32F);
	
	resize(test, trainTempImg, Size(28, 28));
	HOGDescriptor *hog = new HOGDescriptor(cvSize(28, 28), cvSize(14, 14), cvSize(7, 7), cvSize(7, 7), 9);
	vector<float>descriptors;//存放結果
	hog->compute(trainTempImg, descriptors, Size(1, 1), Size(0, 0)); //Hog特徵計算
	cout << "HOG dims: " << descriptors.size() << endl;  //印出Hog特徵維數,這裡是324
	Mat SVMtrainMat(1, descriptors.size(), CV_32F);
	int n = 0;
	for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
	{
		//cvmSet(SVMtrainMat, 0, n, *iter);
		SVMtrainMat.at<float>(n) = *iter;
		n++;
	}
	//int count = svm->getVarCount();
	//cout << count << "   " << SVMtrainMat.cols << endl;
	//count = SVMtrainMat.type();
	//cout << count << endl;
	//cout << svm->getVarCount() << "   " << SVMtrainMat.cols << endl;
	
	int ret = svm->predict(SVMtrainMat);//檢測結果
	sprintf_s(result, "%d\r\n", ret);
	namedWindow("dst", 1);
	imshow("dst", test);
	cout << "result:" << result << endl;
	waitKey();

	return 0;
}
예제 #14
0
void compute_hog(const vector< Mat > & img_lst, vector< Mat > & gradient_lst, const Size & size){
	HOGDescriptor hog;
	hog.winSize = size;
	Mat gray;
	vector< Point > location;
	vector< float > descriptors;

	vector< Mat >::const_iterator img = img_lst.begin();
	vector< Mat >::const_iterator end = img_lst.end();
	for (; img != end; ++img){
		cvtColor(*img, gray, COLOR_BGR2GRAY);
		equalizeHist(gray, gray);
		hog.compute(gray, descriptors, Size(8, 8), Size(0, 0), location);
		Mat tmpImg = Mat(descriptors).clone();
		//gradient_lst.push_back(Mat(descriptors).clone());
		gradient_lst.push_back(tmpImg);
	}
}
예제 #15
0
파일: main.cpp 프로젝트: Modasshir/paper
void load_test_images(const string & filename, int rLabel) {
	HOGDescriptor hog;
	hog.winSize = size;
	string line;
	ifstream file;
	vector<float> descriptors;
	vector<Point> locations;
	file.open(filename.c_str());

	if (!file.is_open()) {
		cout << "file cannot be opened" << endl;
		exit(-1);
	}

	while (true) {

		getline(file, line);

		if (line == "") {
			break;
		}

		Mat img = imread(line.c_str(), 0);
		if (img.empty())
			continue;

		Mat noise = Mat(img.size(), img.type());
		randn(noise, 0, 50);
		img = img + noise;
//		imshow("img",img);
//		waitKey();

		resize(img, img, size);
		hog.compute(img, descriptors, Size(8, 8), Size(0, 0), locations);
		test_list.push_back(Mat(descriptors).clone());
		test_label.push_back(rLabel);
		img.release();

	}
	cout << test_list.size() << endl;
	cout << test_label.size() << endl;

}
예제 #16
0
void computeHOGs( const Size wsize, const vector< Mat > & img_lst, vector< Mat > & gradient_lst )
{
    HOGDescriptor hog;
    hog.winSize = wsize;

    Rect r = Rect( 0, 0, wsize.width, wsize.height );
    r.x += ( img_lst[0].cols - r.width ) / 2;
    r.y += ( img_lst[0].rows - r.height ) / 2;

    Mat gray;
    vector< float > descriptors;

    for( size_t i=0 ; i< img_lst.size(); i++ )
    {
        cvtColor( img_lst[i](r), gray, COLOR_BGR2GRAY );
        hog.compute( gray, descriptors, Size( 8, 8 ), Size( 0, 0 ) );
        gradient_lst.push_back( Mat( descriptors ).clone() );
    }
}
void HOGTrainer::computeHog(const vector<Mat> &img_lst, vector<Mat> &gradient_lst, const Size &size) {
    HOGDescriptor hog;
    hog.winSize = size;
    Mat gray;
    vector<Point> location;
    vector<float> descriptors;

    vector<Mat>::const_iterator img = img_lst.begin();
    vector<Mat>::const_iterator end = img_lst.end();
    for (; img != end; ++img) {
        cvtColor(*img, gray, COLOR_BGR2GRAY);
        hog.compute(gray, descriptors, Size(8, 8), Size(0, 0), location);
        gradient_lst.push_back(Mat(descriptors).clone());
#ifdef _DEBUG
        imshow( "gradient", getHogdescriptorVisu( img->clone(), descriptors, size ) );
        waitKey( 10 );
#endif
    }
}
JNIEXPORT void JNICALL Java_org_opencv_objdetect_HOGDescriptor_compute_11
  (JNIEnv* env, jclass , jlong self, jlong img_nativeObj, jlong descriptors_mat_nativeObj)
{
    static const char method_name[] = "objdetect::compute_11()";
    try {
        LOGD("%s", method_name);
        vector<float> descriptors;
        Mat& descriptors_mat = *((Mat*)descriptors_mat_nativeObj);
        HOGDescriptor* me = (HOGDescriptor*) self; //TODO: check for NULL
        Mat& img = *((Mat*)img_nativeObj);
        me->compute( img, descriptors );
        vector_float_to_Mat( descriptors, descriptors_mat );
        return;
    } catch(const std::exception &e) {
        throwJavaException(env, &e, method_name);
    } catch (...) {
        throwJavaException(env, 0, method_name);
    }
    return;
}
예제 #19
0
int main(int argc)
{
	//讀黨(load file)
	vector<string> img_path;//輸入文件名(path name)
	vector<int> img_catg;
	int nLine = 0;
	string buf;
	ifstream svm_data("t10knums.txt");//訓練樣本所在地(my data place)
	unsigned long n;
	int a2i;
	while (svm_data)//将训练样本文件依次读取进来 (load data)
	{
		if (getline(svm_data, buf))
		{
			nLine++;
			if (nLine % 2 == 0)//基數行圖片,偶數行標籤	(odd is picture,even is label)
			{
				a2i = atoi(buf.c_str());
				img_catg.push_back(a2i);//atoi
			}
			else
			{
				img_path.push_back(buf);//圖像路徑(picture path)
			}
		}
	}
	svm_data.close();//關閉文件(close file)

					 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
					 //處理HOG特徵(get HOG)

	int nImgNum = nLine / 2;
	Mat data_mat=Mat::zeros(nImgNum, 324, CV_32FC1), res_mat=Mat::zeros(nImgNum, 1, CV_32SC1);
	Mat src, trainImg= Mat::zeros(28, 28, CV_32FC1);

	for (string::size_type i = 0; i != img_path.size(); i++)
	{
		src = imread(img_path[i].c_str(), 1);
		if (src.empty())
		{
			cout << " can not load the image: " << img_path[i].c_str() << endl;
			continue;
		}
		cout << "deal with\t" << img_path[i].c_str() << endl;
		resize(src, trainImg, cvSize(28, 28));
		HOGDescriptor *hog = new HOGDescriptor(cvSize(28, 28), cvSize(14, 14), cvSize(7, 7), cvSize(7, 7), 9);
		vector<float>descriptors;//存放结果(storge result)
		hog->compute(trainImg, descriptors, Size(1, 1), Size(0, 0));//HOG特徵計算(get HOG)
		cout << "HOG dims: " << descriptors.size() << endl;
		n = 0;
		for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
		{
			//cvmSet(data_mat, i, n, *iter);//存储HOG特征
			//data_mat.at<float>(28 * i + n) = *iter;
			data_mat.at<float>(i , n) = *iter;

			n++;
		}
		//cvmSet(res_mat, i, 0, img_catg[i]);
		//res_mat.at<float>(i) = img_catg[i];
		res_mat.row(i)= img_catg[i];
		//res_mat.at<float>(i) = i/1000;
		cout << "Done !!!: " << img_path[i].c_str() << " " << img_catg[i] << endl;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//SVM

	Ptr<ml::SVM> svm = ml::SVM::create();
	// edit: the params struct got removed,
	// we use setter/getter now:
	svm->setType(ml::SVM::C_SVC);
	svm->setKernel(ml::SVM::RBF);
	svm->setGamma(0.09);

	if (svm->train(data_mat, ml::ROW_SAMPLE, res_mat)) {
		cout << "train alerady\n";
	}
	svm->save("HOG_SVM_DATA.xml");
	cout << "HOG_SVM_DATA.xml is saved !!! \n exit program" << endl;
	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//樣本測試
	Mat test;
	char result[512];
	/*
	vector<string> img_tst_path;
	ifstream img_tst("D:/SVM_TEST.txt");  //載入需要預測的圖片合集,裡面全部都放路徑,不要放標籤
	while (img_tst)
	{
		if (getline(img_tst, buf))
		{
			img_tst_path.push_back(buf);
		}
	}
	img_tst.close();
	*/
	ofstream predict_txt("SVM_PREDICT.txt");//放入預測結果
	int success = 0, fail = 0;
	for (string::size_type j = 0; j != img_path.size(); j++)//依次預測所有圖片   
	{
		test = imread(img_path[j].c_str(), 1);
		if (test.empty())
		{
			cout << " can not load the image: " << img_path[j].c_str() << endl;
			continue;
		}
		Mat trainTempImg= Mat::zeros(28, 28, 3);
		//cvZero(trainTempImg);
		resize(test, trainTempImg,Size(28,28));
		HOGDescriptor *hog = new HOGDescriptor(cvSize(28, 28), cvSize(14, 14), cvSize(7, 7), cvSize(7, 7), 9);
		vector<float>descriptors;//結果數組   
		hog->compute(trainTempImg, descriptors, Size(1, 1), Size(0, 0));
		//cout << "HOG dims: " << descriptors.size() << endl;
		cout << j << " pictures are done" << endl;
		Mat SVMtrainMat(1, descriptors.size(), CV_32FC1);
		int n = 0;
		for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
		{
			//cvmSet(SVMtrainMat, 0, n, *iter);
			SVMtrainMat.col(n) = *iter;
			//SVMtrainMat.at<float>(0,n) = *iter;
			n++;
		}

		int ret = svm->predict(SVMtrainMat);//檢測結果
		if (ret == img_catg[j]) {
			success++;
		}
		else {
			++fail;
		}
		sprintf(result, "%s  %d\r\n", img_path[j].c_str(), ret);
		predict_txt << result;  //輸出檢測結果到文本 
	}
	double rate = (double)success / ((double)(success + fail));
	cout <<"the success rate is " <<rate << endl;
	predict_txt.close();
	system("pause");
	return 0;
}
예제 #20
0
void computeHog(Mat img, bool flag)
{
    hog.compute(img, featureVector, winStride, Size(0, 0));
    svm.writeFeatureVectorToFile(featureVector, (flag?true:false) );
    (flag ? posCount++ : negCount++);
}
예제 #21
0
파일: main.cpp 프로젝트: aaronguo1996/prp
int testFusion(Mat data,Mat res)
{
	CvSVM svm;
	CvSVMParams param;
	CvTermCriteria criteria;
	criteria = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);
	param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 0.1, 100.0, 0.5, 1.0, NULL, criteria);//for hog
	svm.train(data, res, Mat(), Mat(), param);
	int ImgWidth = 120;
	int ImgHeight = 120;
	string buf;
	vector<string> img_tst_path;
	ifstream img_tst("SVM_TEST.txt");
	while (img_tst)
	{
		if (getline(img_tst, buf))
		{
			img_tst_path.push_back(buf);
		}
	}
	img_tst.close();

	Mat test;
	Mat trainImg = Mat::zeros(ImgHeight, ImgWidth, CV_8UC3);//需要分析的图片  
	char line[512];
	ofstream predict_txt("SVM_PREDICT_FUSION.txt");
	for (string::size_type j = 0; j != img_tst_path.size(); j++)
	{
		test = imread(img_tst_path[j].c_str(), 1);//读入图像   
		resize(test, trainImg, cv::Size(ImgWidth, ImgHeight), 0, 0, INTER_CUBIC);//要搞成同样的大小才可以检测到       
		HOGDescriptor *hog = new HOGDescriptor(cvSize(ImgWidth, ImgHeight), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9);
		vector<float>descriptors;//结果数组     
		hog->compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //调用计算函数开始计算 
		cout << "The Detection Result:" << endl;

		Mat SVMtrainMat = Mat::zeros(1, GLCMSIZE+MATSIZE+descriptors.size(), CV_32FC1);
		int n = 0;
		for (vector<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
		{
			SVMtrainMat.at<float>(0, n) = *iter;
			n++;
		}
		//计算GLCM
		calGLCM(img_tst_path[j].c_str(), GLCM_ANGLE_DIGONAL, j);
		calGLCM(img_tst_path[j].c_str(), GLCM_ANGLE_HORIZATION, j);
		calGLCM(img_tst_path[j].c_str(), GLCM_ANGLE_VERTICAL, j);
		for (int k = 0; k < 12; k++)
			SVMtrainMat.at<float>(0, k + descSize) = (fusion_mat.at<float>(j, k + descSize) - minValue[k]) / (maxValue[k] - minValue[k]);

		//计算颜色直方图
		hsv(img_tst_path[j].c_str(), j);
		for (int k = 0; k < MATSIZE; k++)
		{
			SVMtrainMat.at<float>(0, GLCMSIZE + descSize + k) = fusion_mat.at<float>(j, GLCMSIZE + descSize + k);
		}
		int ret = svm.predict(SVMtrainMat);
		std::sprintf(line, "%s\t%d\n", img_tst_path[j].c_str(), ret);
		printf("%s %d\n", img_tst_path[j].c_str(), ret);
		predict_txt << line;
		delete hog;
	}
	predict_txt.close();
	return 0;
}
예제 #22
0
    void LearnFromImages(CvMat* trainData, CvMat* trainClasses)  
    {  
     Mat img;  
     char file[255];  
     for (int i = 0; i < classes; i++)  
     {  
      for (int j=0; j < train_samples;j++)
      {
       sprintf(file, "%s/%d/%d.png", pathToImages, i, j);  
       img = imread(file, 1);  
       if (!img.data)  
       {  
         cout << "File " << file << " not found\n";  
         exit(1);  
       }  
       
       Mat outfile; 
       switch (descriptor) {
       	case 1:
        {// OpenCV HOG descriptor
          HOGDescriptor hog;
          vector<float> ders;
       	  vector<Point> locs;
          resize(img,outfile,Size(64,128));
          hog.compute(outfile,ders,Size(0,0),Size(0,0),locs);
          //cout<<ders.size()<<"\n";
          //trainData = cvCreateMat(classes * train_samples,ders.size(), CV_32FC1);  
          for (int n = 0; n < ders.size(); n++)  
          {  
           trainData->data.fl[i*train_samples*ders.size()+ j * ders.size() + n] = ders.at(n);  
          }          
          break;
        }
        case 2:
       	{ // Image intesnity as descriptor
       	  PreProcessImage(&img, &outfile, sizex, sizey);
       	  //trainData = cvCreateMat(classes * train_samples,ImageSize, CV_32FC1);  
          for (int n = 0; n < ImageSize; n++)  
          {  
           trainData->data.fl[i*train_samples*ImageSize+ j * ImageSize + n] = outfile.data[n]; 
          } 
       	  break;
       	}
       	case 3:
       	{ // HOG3 descriptor
          resize(img,outfile,Size(sizex,sizey));       	
	  IplImage copy = outfile;
	  IplImage* img2 = &copy;	
          vector<float> ders;       	  
       	  HOG3(img2,ders);
          for (int n = 0; n < ders.size(); n++)  
          {  
           trainData->data.fl[i*train_samples*ders.size()+ j * ders.size() + n] = ders.at(n);  
          }
          break;       	  
       	}
       	case 4:
       	{ // Histogram as descriptor
       	  resize(img,outfile,Size(sizex,sizey));
       	  // Establish the number of bins
	  int histSize = 9;
	  // Set the ranges (for grayscale values)
	  float range[] = { 0, 256 } ;
	  const float* histRange = { range };
	  bool uniform = true; bool accumulate = false;
	  Mat ders; // histogram descriptor
	  // Compute the histograms:
	  calcHist( &outfile, 1, 0, Mat(), ders, 1, &histSize, &histRange, uniform, accumulate );
	  normalize( ders, ders, 0, 1, NORM_MINMAX, -1, Mat() );
	  for (int n = 0; n < ders.rows*ders.cols; n++)  
          {  
           trainData->data.fl[i*train_samples*ImageSize+ j * ImageSize + n] = ders.data[n]; 
          } 
       	  break;	
       	}
       }
        
       trainClasses->data.fl[i*train_samples+j] = i;  
     }  
    }  
   }   
예제 #23
0
    void AnalyseImage(KNearest knearest, CvSVM SVM)  
    {  
      
     //CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1);  
     CvMat* sample2;      
     Mat _image,image, gray, blur, thresh;  
      
     vector < vector<Point> > contours;  
     _image = imread("./images/37.png", 1);  
     //image = imread("./images/all_4.png", 1);  
     
     resize(_image,image,Size(2*sizex,1.2*sizey));
     cvtColor(image, gray, COLOR_BGR2GRAY);  
     GaussianBlur(gray, blur, Size(5, 5), 2, 2);  
     adaptiveThreshold(blur, thresh, 255, 1, 1, 11, 2);  
     findContours(thresh, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);  
     float digits[contours.size()];
     float number;
      
     for (size_t i = 0; i < contours.size(); i++)  
     {  
      vector < Point > cnt = contours[i];  
      if (contourArea(cnt) > 50)  
      {  
       Rect rec = boundingRect(cnt);  
       if (rec.height > 28)  
       {  
        Mat roi = image(rec);  
        Mat stagedImage;  

        // Descriptor    
        switch (descriptor) {
         case 1:
          {// HOG descriptor
           HOGDescriptor hog;
           vector<float> ders;
       	   vector<Point> locs;
           resize(roi,stagedImage,Size(64,128));
           hog.compute(stagedImage,ders,Size(0,0),Size(0,0),locs);
           //cout<<ders.size()<<"\n";
           sample2 = cvCreateMat(1, ders.size(), CV_32FC1);  
           for (int n = 0; n < ders.size(); n++)  
           {  
            sample2->data.fl[n] = ders.at(n);  
           }          
           break;
          }
         case 2:
          { // Image Data descriptor
           sample2 = cvCreateMat(1, ImageSize, CV_32FC1);
           PreProcessImage(&roi, &stagedImage, sizex, sizey);  
           for (int n = 0; n < ImageSize; n++)  
           {  
            sample2->data.fl[n] = stagedImage.data[n];  
           }  
           break;
          }
         case 3:
          {// HOG3 detector
           resize(roi,stagedImage,Size(sizex,sizey));
	   IplImage copy = stagedImage;
	   IplImage* img2 = &copy;	
           vector<float> ders;
       	   HOG3(img2,ders);
           sample2 = cvCreateMat(1, ders.size(), CV_32FC1);  
           for (int n = 0; n < ders.size(); n++)  
           {  
            sample2->data.fl[n] = ders.at(n);  
           }  
           break;       	  
          }
         case 4:
          { // Histogram as descriptor
       	   resize(roi,stagedImage,Size(sizex,sizey));
       	   // Establish the number of bins
	   int histSize = 9;
	   // Set the ranges (for grayscale values)
	   float range[] = { 0, 256 } ;
	   const float* histRange = { range };
	   bool uniform = true; bool accumulate = false;
	   Mat ders; // histogram descriptor
           int ders_size=ders.rows*ders.cols;
           sample2 = cvCreateMat(1, ders_size, CV_32FC1);	   
	   // Compute the histograms:
	   calcHist( &stagedImage, 1, 0, Mat(), ders, 1, &histSize, &histRange, uniform, accumulate );
           normalize( ders, ders, 0, 1, NORM_MINMAX, -1, Mat() );	   
	   for (int n = 0; n < ders_size; n++)  
           {  
            sample2->data.fl[n] = ders.data[n]; 
           } 
       	   break;	
          }          
        }
	
	// Classifier
        float result;
        switch (classifier) {
         case 1:
         {
       	   result = SVM.predict(sample2);
           break;
         }
         case 2:
         {
           result = knearest.find_nearest(sample2, 1);       	 	
       	   break;
         }
        }
        digits[contours.size()-i-1]=result;
        rectangle(image, Point(rec.x, rec.y),  
        Point(rec.x + rec.width, rec.y + rec.height),  
        Scalar(0, 0, 255), 2);  
      
        imshow("all", image);  
        cout << result << "\n";  
      
        imshow("single", stagedImage);  
        waitKey(0);  
       }  
      
      }  
      
     }
     number=digits[0]*10+digits[1];
     cout<< "number is "<<number<<"\n";
    }  
예제 #24
0
   void RunSelfTest(KNearest& knn2, CvSVM& SVM2)  
    {  
     Mat img;  
     //CvMat* sample2 = cvCreateMat(1, ImageSize, CV_32FC1);  
     CvMat* sample2;
     // SelfTest  
     char file[255];  
     int z = 0;  
     while (z++ < 10)  
     {  
      int iSecret = rand() % classes;  
      //cout << iSecret;  
      sprintf(file, "%s/%d/%d.png", pathToImages, iSecret, rand()%train_samples);  
      img = imread(file, 1);  
      Mat stagedImage;  

      switch (descriptor) {
       case 1:
       {// HOG descriptor
         HOGDescriptor hog;
         vector<float> ders;
       	 vector<Point> locs;
         resize(img,stagedImage,Size(64,128));
         hog.compute(stagedImage,ders,Size(0,0),Size(0,0),locs);
         //cout<<ders.size()<<"\n";
         sample2 = cvCreateMat(1, ders.size(), CV_32FC1);  
         for (int n = 0; n < ders.size(); n++)  
         {  
          sample2->data.fl[n] = ders.at(n);  
         }          
         break;
       }
       case 2:
       { // Image data as descriptor
         sample2 = cvCreateMat(1, ImageSize, CV_32FC1);
         PreProcessImage(&img, &stagedImage, sizex, sizey);  
         for (int n = 0; n < ImageSize; n++)  
         {  
          sample2->data.fl[n] = stagedImage.data[n];  
         }  
         break;
       }
       case 3:
       { // HOG3 descriptor
         resize(img,stagedImage,Size(sizex,sizey));
	 IplImage copy = stagedImage;
	 IplImage* img2 = &copy;	
         vector<float> ders;       	  
       	 HOG3(img2,ders);
         sample2 = cvCreateMat(1, ders.size(), CV_32FC1);  
         for (int n = 0; n < ders.size(); n++)  
         {  
          sample2->data.fl[n] = ders.at(n);  
         } 
         break;       	  
       } 
       case 4:
       { // Histogram as descriptor
       	 resize(img,stagedImage,Size(sizex,sizey));
       	 // Establish the number of bins
	 int histSize = 9;
	 // Set the ranges (for grayscale values)
	 float range[] = { 0, 256 } ;
	 const float* histRange = { range };
	 bool uniform = true; bool accumulate = false;
	 Mat ders; // histogram descriptor
	 int ders_size=ders.rows*ders.cols;
         sample2 = cvCreateMat(1, ders_size, CV_32FC1);	 
	 // Compute the histograms:
	 calcHist( &stagedImage, 1, 0, Mat(), ders, 1, &histSize, &histRange, uniform, accumulate );
	 normalize( ders, ders, 0, 1, NORM_MINMAX, -1, Mat() );	 
	 for (int n = 0; n < ders_size; n++)  
         {  
          sample2->data.fl[n] = ders.data[n]; 
         } 
       	 break;	
       }      
      } 
 
      float detectedClass;
      switch (classifier) {
       case 1:
       {
       	 detectedClass = SVM2.predict(sample2);
         break;
       }
       case 2:
       {
         detectedClass = knn2.find_nearest(sample2, 1);       	 	
       	 break;
       }
      }
     
      if (iSecret != (int) ((detectedClass)))  
      {  
       cout << "False " << iSecret << " matched with "  
         << (int) ((detectedClass));  
       exit(1);  
      }  
      cout << "Right " << (int) ((detectedClass)) << "\n";  
      imshow("single", stagedImage);  
      waitKey(0);  
     }  
      
    }