void SpmBowExtractor::extractSpm(const cv::Mat& image, const cv::Mat_<uchar>& mask, cv::Mat_<int>& spm_histogram, feature_type::T feature_types)
{
    const std::size_t histogram_count = BowExtractor::getHistogramCount(pyramid_levels_);
    const cv::Rect roi = cv::Rect(0,0, image.cols, image.rows);

    const std::size_t histo_dim = compute_histogram_length(feature_types, histogram_count);
    spm_histogram.create(1, histo_dim);
    std::size_t last_offset = 0;

    // extract surf
    if (feature_types & feature_type::Surf)
    {
        cv::Mat_<int> surf_histo = getHistogramView(
                spm_histogram,
                0,
                surf_.wordCount(),
                histogram_count);
        extract_feature(surf_extractor_, surf_, image, roi, mask, surf_histo);
        last_offset += surf_histo.total();
    }

    // extract hog
    if (feature_types & feature_type::Hog)
    {
        cv::Mat_<int> hog_histo = getHistogramView(
                spm_histogram,
                last_offset,
                hog_.wordCount(),
                histogram_count);
        extract_feature(hog_extractor_, hog_, image, roi, mask, hog_histo);
        last_offset += hog_histo.total();
    }

    // extract color
    if (feature_types & feature_type::Color)
    {
        cv::Mat_<int> color_histo = getHistogramView(
                spm_histogram,
                last_offset,
                color_.wordCount(),
                histogram_count);
        extract_feature(color_extractor_, color_, image, roi, mask, color_histo);
        last_offset += color_histo.total();
    }

    // extract lbp
    if (feature_types & feature_type::Lbp)
    {
        cv::Mat_<int> lbp_histo = getHistogramView(
                spm_histogram,
                last_offset,
                lbp_.wordCount(),
                histogram_count);
        lbp_.sumPool(
                image,
                roi,
                mask,
                lbp_.wordCount(),
                pyramid_levels_,
                lbp_histo);
        last_offset += lbp_histo.total();
    }

    // extract ssd
    if ((feature_types & feature_type::Ssd) || (feature_types & feature_type::ColorSsd))
    {
        cv::Mat_<int> ssd_histo = getHistogramView(
                spm_histogram,
                last_offset,
                ssd_.wordCount(),
                histogram_count);
        extract_feature(ssd_extractor_, ssd_, image, roi, mask, ssd_histo);
        last_offset += ssd_histo.total();
    }

    // extract holbp
    if (feature_types & feature_type::HoLbp)
    {
        cv::Mat_<int> holbp_histo = getHistogramView(
                spm_histogram,
                last_offset,
                holbp_.wordCount(),
                histogram_count);
        extract_feature(holbp_extractor_, holbp_, image, roi, mask, holbp_histo);
        last_offset += holbp_histo.total();
    }

    // extract dense_surf 
    if (feature_types & feature_type::DenseSurf)
    {
        cv::Mat_<int> dense_surf_histo = getHistogramView(
                spm_histogram,
                last_offset,
                dense_surf_.wordCount(),
                histogram_count);
        extract_feature(dense_surf_extractor_, dense_surf_, image, roi, mask,
												dense_surf_histo);
        last_offset += dense_surf_histo.total();
    }
}
Пример #2
0
void feature_recorder::make_histogram(const class histogram_def &def)
{
    beregex reg(def.pattern,REG_EXTENDED);
    string ifname = fname_counter("");  // source of features

    ifstream f(ifname.c_str());
    if(!f.is_open()){
        std::cerr << "Cannot open histogram input file: " << ifname << "\n";
        return;
    }

    /* Read each line of the feature file and add it to the histogram.
     * If we run out of memory, dump that histogram to a file and start
     * on the next histogram.
     */
    for(int histogram_counter = 0;histogram_counter<max_histogram_files;histogram_counter++){
        HistogramMaker h(def.flags);            /* of seen features, created in pass two */
        try {
            string line;
            while(getline(f,line)){
                if(line.size()==0) continue; // empty line
                if(line[0]=='#') continue;   // comment line
                truncate_at(line,'\r');      // truncate at a \r if there is one.

                /** If there is a string required in the line and it isn't present, don't use this line */
                if(def.require.size()){
                    if(line.find_first_of(def.require)==std::string::npos){
                        continue;
                    }
                }

                string feature = extract_feature(line);
                if(feature.find('\\')!=string::npos){
                    feature = unquote_string(feature);  // reverse \xxx encoding
                }
                /** If there is a pattern to use to prune down the feature, use it */
                if(def.pattern.size()){
                    string new_feature = feature;
                    if(!reg.search(feature,&new_feature,0,0)){
                        // no search match; avoid this feature
                        continue;               
                    }
                    feature = new_feature;
                }
        
                /* Remove what follows after \t if this is a context file */
                size_t tab=feature.find('\t');
                if(tab!=string::npos) feature.erase(tab); // erase from tab to end
                h.add(feature);
            }
            f.close();
        }
        catch (const std::exception &e) {
            std::cerr << "ERROR: " << e.what() << " generating histogram "
                      << name << "\n";
        }
            
        /* Output what we have */
        stringstream real_suffix;

        real_suffix << def.suffix;
        if(histogram_counter>0) real_suffix << histogram_counter;
        string ofname = fname_counter(real_suffix.str()); // histogram name
        ofstream o;
        o.open(ofname.c_str());
        if(!o.is_open()){
            cerr << "Cannot open histogram output file: " << ofname << "\n";
            return;
        }

        HistogramMaker::FrequencyReportVector *fr = h.makeReport();
        if(fr->size()>0){
            banner_stamp(o,histogram_file_header);
            o << *fr;                   // sends the entire histogram
        }

        delete fr;
        o.close();

        if(f.is_open()==false){
            return;     // input file was closed
        }
    }
    std::cerr << "Looped " << max_histogram_files
              << " times on histogram; something seems wrong\n";
}
Пример #3
0
trainner::trainner() {

	/*
	 * reading images, convert them to binary,extract features
	 */
	FileReader reader("pathsF.txt");
	FileWriter writer("out.txt");
	string line = "";
	IplImage* img;
	IplImage*gray_im;
	IplImage*gray_img;
	CvSeq* contour; //pointer to a contour.
	CvMemStorage* space = cvCreateMemStorage(0);
	CvBox2D hand_boundary;
	CvSeq* largest_contour;
	int i = 0;
	int all_time[dataset_size];
	while ((line = reader.readFile()) != "") {
		std::clock_t start;

		start = std::clock();

		//load the img
		img = cvLoadImage(line.c_str());
		cvSmooth(img, img, CV_GAUSSIAN, 5, 5);
		gray_im = cvCloneImage(img);
		cvCvtColor(img, gray_im, CV_BGR2YCrCb);
		gray_img = cvCreateImage(cvGetSize(gray_im), 8, 1);
		cvInRangeS(gray_im, cvScalar(0, 131, 80), cvScalar(255, 185, 135),
				gray_img);
		cvSmooth(gray_img, gray_img, CV_MEDIAN, 5, 5);

		hand = cvCloneImage(gray_img);
		//finding all contours in the image
		cvFindContours(gray_img, space, &contour, sizeof(CvContour),
				CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
		//iterate through each contour
		double max_area = 0;
		double area = 0;
		//finding largest contour
		while (contour) {
			area = cvContourArea(contour);
			if (area > max_area) {
				max_area = area;
				largest_contour = contour;
			}
			contour = contour->h_next;
		}

		if (largest_contour && largest_contour->total > 0) {
			hand_boundary = cvMinAreaRect2(largest_contour);

			float max = hand_boundary.size.width;
			if (max < hand_boundary.size.height)
				max = hand_boundary.size.height;
			//copy the hand in its own image
			CvRect rounded =
					cvRect(hand_boundary.center.x - (max / 2) - 25,
							hand_boundary.center.y - (max / 2) - 25, max + 50,
							max + 50);
			cvSetImageROI(hand, rounded);
			hand = cvCloneImage(hand);
//			cvShowImage("image", hand);
			cvWaitKey(0);
			cvReleaseImage(&gray_img);
			cvClearSeq(largest_contour);
			//			string bin = extract_feature();
			//write to file
			//			writer.writeFile(bin);
			extract_feature(i);

		} else {
			for (int j = 0; j < number_of_features; j++)
				data[i][j] = 0.0;
		}
		int timm = (std::clock() - start) / (double) (CLOCKS_PER_SEC / 1000);
		all_time[i] = timm;
		i++;

	}
	int sum = 0;

	for (int i = 0; i < dataset_size; i++) {
		sum += all_time[i];
	}
	sum = sum / dataset_size;
	cout << sum << endl;
	reader.~FileReader();

	// now train the classifier
	train();

	//print features
	for (int i = 0; i < dataset_size; i++) {
		ostringstream oss;
		if (i < 11)
			oss << "up";
		else if (i < 30)
			oss << "open";
		else if (i < 60)
			oss << "capture";
		else if (i < 83)
			oss << "call";
		else if (i < 101)
			oss << "left";
		else if (i < 125)
			oss << "right";
		else if (i < 136)
			oss << "closed";
		else if (i < 149)
			oss << "start";
		else if (i < 159)
			oss << "Lup";
		else if (i < 173)
			oss << "Lopen";
		else if (i < 190)
			oss << "Lcapture";
		else if (i < 197)
			oss << "Lcall";
		oss << ",";
		for (int j = 0; j < number_of_features; j++) {
			if (data[i][j] == 0.0)
				oss << "0";
			else
				oss << "1";
			oss << ",";
		}
		string name = oss.str();
		writer.writeFile(name);
	}
	writer.~FileWriter();
}