void load()
{
	vector<string> file_name_vec = list_files_in_directory(pose_database_path);

	for (string& name_current : file_name_vec)
	{
		string name_without_num = "";

		for(char& c : name_current)
		{
			string char_str = "";
			char_str += c;

			if (char_str != "0" && atoi(char_str.c_str()) == 0)
				name_without_num += c;
			else
				break;
		}

		vector<string> name_extension_vec = split_string(name_current, ".");

		if (name_extension_vec.size() > 1 && name_extension_vec[1] == "nrocinunerrad")
		{
			const string path = pose_database_path + slash + name_current;
			vector<string> data = read_text_file(path);

			vector<Point> points;
			for (string& str : data)
			{
				vector<string> str_pair = split_string(str, "!");
				int x = atoi(str_pair[0].c_str());
				int y = atoi(str_pair[1].c_str());
				points.push_back(Point(x, y));
			}

			string pose_name_loaded = name_extension_vec[0];

			int num_count = 0;
			for (char& c : pose_name_loaded)
			{
				string char_str = "";
				char_str += c;
				if (atoi(char_str.c_str()) != 0 || char_str == "0")
					++num_count;
			}

			for (int i = 0; i < num_count; ++i)
				pose_name_loaded.pop_back();

			points_collection.push_back(points);
			names_collection.push_back(pose_name_loaded);

			const string label_path = pose_database_path + slash + "labels" + slash + name_current;
			vector<string> label_data = read_text_file(label_path);

			vector<Point> labels;
			for (String& str : label_data)
			{
				vector<string> str_pair = split_string(str, "!");
				labels.push_back(Point(atoi(str_pair[0].c_str()), atoi(str_pair[1].c_str())));
			}
			labels_collection.push_back(labels);
		}
		else if (name_extension_vec.size() > 1 && name_extension_vec[1] == "png")
		{
			const string path = pose_database_path + slash + name_current;
			Mat image_loaded = imread(path);
		}
	}
}
Ejemplo n.º 2
0
bool ObjPatchMatcher::MatchViewPatch(const Mat& cimg, const Mat& dmap_raw) {

	// compute feature map
	Mat gray_img, gray_img_float;
	cvtColor(cimg, gray_img, CV_BGR2GRAY);
	gray_img.convertTo(gray_img_float, CV_32F);
	waitKey(10);

	// gradient
	Mat grad_x, grad_y, grad_mag;
	Sobel(gray_img_float, grad_x, CV_32F, 1, 0);
	Sobel(gray_img_float, grad_y, CV_32F, 0, 1);
	magnitude(grad_x, grad_y, grad_mag);

	// depth

	// do search
	Mat score_map = Mat::zeros(cimg.rows, cimg.cols, CV_32F);
	Mat feat;
	int topK = 1;
	map<float, ImgWin> top_det;

	cout<<"Start match..."<<endl;
	float max_dist = 0;
	double start_t = getTickCount();
	//#pragma omp parallel for
	for(int r=patch_size.height/2; r<gray_img.rows-patch_size.height/2; r++) {
		for(int c=patch_size.width/2; c<gray_img.cols-patch_size.width/2; c++) {

			Rect box(c-patch_size.width/2, r-patch_size.height/2, patch_size.width, patch_size.height);
			MatFeatureSet featset;
			grad_mag(box).copyTo(featset["gradient"]);
			ComputePatchFeat(featset, feat);
			vector<DMatch> matches;
			MatchPatch(feat, topK, matches);

			for(size_t i=0; i<topK; i++) { 
				score_map.at<float>(r,c) += matches[i].distance;
			}
			score_map.at<float>(r,c) /= topK;

			ImgWin best_match = box;
			best_match.score = matches[0].trainIdx;
			top_det[matches[0].distance] = best_match;
			max_dist = MAX(max_dist, score_map.at<float>(r,c));
		}
	}

	cv::imshow("color", cimg);
	ImgVisualizer::DrawFloatImg("scoremap", score_map);
	cv::waitKey(0);

	int cnt = 0;
	Mat disp_img = cimg.clone();
	for(auto pi=top_det.begin(); pi!=top_det.end(); pi++) {
		cout<<pi->first<<endl;
		rectangle(disp_img, pi->second, CV_RGB(255,0,0), 2);
		Mat db_view = imread(patch_meta.objects[pi->second.score].meta_data.img_path);
		resize(db_view, db_view, patch_size);
		Mat test_win;
		resize(cimg(pi->second), test_win, patch_size);
		ImgVisualizer::DrawFloatImg("db grad", patch_meta.objects[pi->second.score].visual_data.img_desc);
		ImgVisualizer::DrawFloatImg("test grad", grad_mag(pi->second));
		imshow("db view", db_view);
		imshow("color", disp_img);
		waitKey(0);
		if(++cnt == 100) break;
	}

	return true;
}
Ejemplo n.º 3
0
void ICFDetector::train(const vector<string>& image_filenames,
                        const vector< vector<Rect> >& labelling,
                        ICFDetectorParams params)
{
    Size model_size(params.model_n_cols, params.model_n_rows);

    vector<Mat> samples; /* positive samples + negative samples */
    Mat sample, resized_sample;
    int pos_count = 0;
    for( size_t i = 0; i < image_filenames.size(); ++i, ++pos_count )
    {
        Mat img = imread(String(image_filenames[i].c_str()));
        for( size_t j = 0; j < labelling[i].size(); ++j )
        {
            Rect r = labelling[i][j];
            if( r.x > img.cols || r.y > img.rows )
                continue;

            sample = img.colRange(max(r.x, 0), min(r.width, img.cols))
                        .rowRange(max(r.y, 0), min(r.height, img.rows));

            resize(sample, resized_sample, model_size);

            samples.push_back(resized_sample);
        }
    }

    int neg_count = 0;
    RNG rng;
    for( size_t i = 0; i < image_filenames.size(); ++i )
    {
        Mat img = imread(String(image_filenames[i].c_str()));
        for( int j = 0; j < (int)(pos_count / image_filenames.size() + 1); )
        {
            Rect r;
            r.x = rng.uniform(0, img.cols);
            r.width = rng.uniform(r.x + 1, img.cols);
            r.y = rng.uniform(0, img.rows);
            r.height = rng.uniform(r.y + 1, img.rows);

            if( !overlap(r, labelling[i]) )
            {
                sample = img.colRange(r.x, r.width).rowRange(r.y, r.height);
                //resize(sample, resized_sample);
                samples.push_back(resized_sample);
                ++neg_count;
                ++j;
            }
        }
    }

    Mat_<int> labels(1, pos_count + neg_count);
    for( int i = 0; i < pos_count; ++i)
        labels(0, i) = 1;
    for( int i = pos_count; i < pos_count + neg_count; ++i )
        labels(0, i) = -1;

    vector<Point3i> features = generateFeatures(model_size);
    Ptr<ACFFeatureEvaluator> feature_evaluator = createACFFeatureEvaluator(features);

    Mat_<int> data((int)features.size(), (int)samples.size());
    Mat_<int> feature_col;

    vector<Mat> channels;
    for( int i = 0; i < (int)samples.size(); ++i )
    {
        computeChannels(samples[i], channels);
        feature_evaluator->setChannels(channels);
        feature_evaluator->evaluateAll(feature_col);
        for( int j = 0; j < feature_col.rows; ++j )
            data(i, j) = feature_col(0, j);
    }

    WaldBoostParams wparams;
    wparams.weak_count = params.weak_count;
    wparams.alpha = 0.001f;

    Ptr<WaldBoost> waldboost = createWaldBoost(wparams);
    waldboost->train(data, labels);
}
const Mat DirectoryImageSource::getImage() const
{
	if (index < 0 || index >= files.size())
		return Mat();
	return imread(files[index].string(), CV_LOAD_IMAGE_COLOR);
}
Ejemplo n.º 5
0
bool ObjPatchMatcher::Match(const Mat& cimg, const Mat& dmap_raw, Mat& mask_map) {

	/*
	 * precompute feature maps
	 */
	// gradient
	Mat gray_img, gray_img_float, edge_map;
	cvtColor(cimg, gray_img, CV_BGR2GRAY);
	gray_img.convertTo(gray_img_float, CV_32F, 1.f/255);
	Canny(gray_img, edge_map, 10, 50);
	cv::imshow("edge", edge_map);
	cv::imshow("color", cimg);
	cv::waitKey(10);

	Mat grad_x, grad_y, grad_mag;
	Sobel(gray_img_float, grad_x, CV_32F, 1, 0);
	Sobel(gray_img_float, grad_y, CV_32F, 0, 1);
	magnitude(grad_x, grad_y, grad_mag);

	// depth
	Mat dmap_float, pts3d, normal_map;
	if( use_depth ) {
		Feature3D feat3d;
		dmap_raw.convertTo(dmap_float, CV_32F);
		Mat cmp_mask;
		compare(dmap_float, 800, cmp_mask, CMP_LT);
		dmap_float.setTo(800, cmp_mask);
		compare(dmap_float, 7000, cmp_mask, CMP_GT);
		dmap_float.setTo(7000, cmp_mask);
		dmap_float = (dmap_float-800)/(7000-800);

		feat3d.ComputeKinect3DMap(dmap_float, pts3d, false);
		feat3d.ComputeNormalMap(pts3d, normal_map);
	}

	/*
	 *	start searching
	 */
	// init searcher
	//searcher.Build(patch_data, BruteForce_L2);	// opencv bfmatcher has size limit: maximum 2^31
	LSHCoder lsh_coder;
	if(use_code) {
		lsh_coder.Load();
	}
	
	Mat score_map = Mat::zeros(edge_map.rows, edge_map.cols, CV_32F);
	Mat mask_vote_map = Mat::zeros(cimg.rows, cimg.cols, CV_32F);
	mask_map = Mat::zeros(cimg.rows, cimg.cols, CV_32F);
	Mat mask_count = Mat::zeros(cimg.rows, cimg.cols, CV_32S);	// number of mask overlapped on each pixel
	Mat feat;
	int topK = 40;
	int total_cnt = countNonZero(edge_map);
	vector<VisualObject> query_patches;
	query_patches.reserve(total_cnt);

	cout<<"Start match..."<<endl;
	
	float max_dist = 0;
	int cnt = 0;
	char str[30];
	double start_t = getTickCount();
//#pragma omp parallel for
	for(int r=patch_size.height/2; r<gray_img.rows-patch_size.height/2; r+=3) {
		for(int c=patch_size.width/2; c<gray_img.cols-patch_size.width/2; c+=3) {

			/*int rand_r = rand()%gray_img.rows;
			int rand_c = rand()%gray_img.cols;
			if(rand_r < patch_size.height/2 || rand_r > gray_img.rows-patch_size.height/2 ||
				rand_c < patch_size.width/2 || rand_c > gray_img.cols-patch_size.width/2) continue;*/
			int rand_r = r, rand_c = c;

			if(edge_map.at<uchar>(rand_r, rand_c) > 0) 
			{
				cnt++;
				destroyAllWindows();

				Rect box(rand_c-patch_size.width/2, rand_r-patch_size.height/2, patch_size.width, patch_size.height);
				MatFeatureSet featset;
				gray_img_float(box).copyTo(featset["gray"]);
				//grad_mag(box).copyTo(featset["gradient"]);
				if(use_depth)
				{ 
					normal_map(box).copyTo(featset["normal"]);
					dmap_float(box).copyTo(featset["depth"]);
				}
				ComputePatchFeat(featset, feat);
				vector<DMatch> matches;
				if(use_code) 
				{
					BinaryCodes codes;
					HashKey key_val;
					lsh_coder.ComputeCodes(feat, codes);
					HashingTools<HashKeyType>::CodesToKey(codes, key_val);
					MatchCode(key_val, topK, matches);
				}
				else
				{
					MatchPatch(feat, topK, matches);
				}
				
				if(matches[0].distance < 0 || matches[0].distance > 1000) {
					cout<<"match dist: "<<matches[0].distance<<endl;
					double minv, maxv;
					cout<<norm(feat, patch_data.row(matches[0].trainIdx), NORM_L2)<<endl;
					minMaxLoc(feat, &minv, &maxv);
					cout<<minv<<" "<<maxv<<endl;
					cout<<feat<<endl<<endl;
					minMaxLoc(patch_data.row(matches[0].trainIdx), &minv, &maxv);
					cout<<minv<<" "<<maxv<<endl;
					cout<<patch_data.row(matches[0].trainIdx)<<endl;
					imshow("cimg", cimg);
					waitKey(0);
				}
				vector<vector<Mat>> pixel_mask_vals(patch_size.height, vector<Mat>(patch_size.width, Mat::zeros(1, topK, CV_32F)));
				VisualObject cur_query;
				cur_query.visual_data.bbox = box;
				cur_query.visual_data.mask = Mat::zeros(patch_size.height, patch_size.width, CV_32F);
				for(size_t i=0; i<topK; i++) { 
					score_map.at<float>(rand_r,rand_c) += matches[i].distance;
					cur_query.visual_data.mask += patch_meta.objects[matches[i].trainIdx].visual_data.mask;
					for(int mr=0; mr<patch_size.height; mr++) for(int mc=0; mc<patch_size.width; mc++) {
						pixel_mask_vals[mr][mc].at<float>(i) = 
							patch_meta.objects[matches[i].trainIdx].visual_data.mask.at<float>(mr, mc);
					}
				}
				score_map.at<float>(rand_r,rand_c) /= topK;
				cur_query.visual_data.mask /= topK;			// average returned mask
				
				// compute mask quality
				Scalar mean_, std_;
				/*ofstream out("pixel_mask_std_100.txt", ios::app);
				for(int mr=0; mr<patch_size.height; mr++) for(int mc=0; mc<patch_size.width; mc++) {
				meanStdDev(pixel_mask_vals[mr][mc], mean_, std_);
				out<<std_.val[0]<<" ";
				}
				out<<endl;*/
				meanStdDev(cur_query.visual_data.mask, mean_, std_);
				cur_query.visual_data.scores.push_back(mean_.val[0]);
				cur_query.visual_data.scores.push_back(std_.val[0]);

				Mat align_mask = Mat::zeros(cimg.rows, cimg.cols, CV_8U);
				int gt_mask_id = patch_meta.objects[matches[0].trainIdx].meta_data.category_id;
				if(gt_mask_id != -1) {
					Mat nn_mask = gt_obj_masks[gt_mask_id];
					//imshow("gt mask", nn_mask*255);
					//waitKey(10);
					Rect gt_box = patch_meta.objects[matches[0].trainIdx].visual_data.bbox;
					Rect align_box = AlignBox(box, gt_box, cimg.cols, cimg.rows);
					vector<ImgWin> boxes; boxes.push_back(align_box);
					//ImgVisualizer::DrawWinsOnImg("alignbox", cimg, boxes);
					//waitKey(10);
					Rect target_box = Rect(box.x-(gt_box.x-align_box.x), box.y-(gt_box.y-align_box.y), align_box.width, align_box.height);
					cout<<target_box<<endl;
					nn_mask(align_box).copyTo(align_mask(target_box));
				}
				align_mask.convertTo(align_mask, CV_32F);
				mask_map += align_mask * matches[0].distance;	//*score_map.at<float>(r,c);
				//mask_count(box) = mask_count(box) + 1;

				//cout<<score_map.at<float>(r,c)<<endl;
				max_dist = MAX(max_dist, matches[0].distance);
				query_patches.push_back(cur_query);

				// vote object regions
				/*Point3f line_ori;
				int obj_pt_sign;
				ComputeDominantLine(cur_query.visual_desc.mask, box.tl(), line_ori, obj_pt_sign);
				for(int rr=0; rr<cimg.rows; rr++) for(int cc=0; cc<cimg.cols; cc++) {
				float line_val = line_ori.x*cc+line_ori.y*rr+line_ori.z;
				if((line_val>0?1:-1)==obj_pt_sign) mask_vote_map.at<float>(rr, cc)++;
				}*/

#ifdef VERBOSE

				// current patch
				Mat disp, patch_gray, patch_grad, patch_normal, patch_depth;
				disp = cimg.clone();
				rectangle(disp, box, CV_RGB(255,0,0), 2);
				resize(gray_img(box), patch_gray, Size(50,50));
				resize(grad_mag(box), patch_grad, Size(50,50));
				Mat cur_mask;
				resize(cur_query.visual_desc.mask, cur_mask, Size(50,50));
				if(use_depth) 
				{
					resize(normal_map(box), patch_normal, Size(50,50));

					normalize(dmap_float(box), patch_depth, 1, 0, NORM_MINMAX);
					patch_depth.convertTo(patch_depth, CV_8U, 255);
					//dmap_float(box).convertTo(patch_depth, CV_8U, 255);
					resize(patch_depth, patch_depth, Size(50,50));
				}

				Mat onormal;
				sprintf_s(str, "query_gray_%d.jpg", cnt);
				imshow(str, patch_gray);
				imwrite(str, patch_gray);

				/*sprintf_s(str, "query_grad_%d.jpg", cnt);
				ImgVisualizer::DrawFloatImg(str, patch_grad, onormal, true);
				imwrite(str, onormal);*/
				
				sprintf_s(str, "query_depth_%d.jpg", cnt);
				imshow(str, patch_depth);
				imwrite(str, patch_depth);
				
				sprintf_s(str, "query_normal_%d.jpg", cnt);
				ImgVisualizer::DrawNormals(str, patch_normal, onormal, true);
				imwrite(str, onormal);

				sprintf_s(str, "query_box_%d.jpg", cnt);
				imshow(str, disp);
				imwrite(str, disp);

				//imshow("align mask", align_mask*255);

				cur_mask.convertTo(cur_mask, CV_8U, 255);
				sprintf_s(str, "query_tmask_%d.jpg", cnt);
				imshow(str, cur_mask);
				imwrite(str, cur_mask);

				// show match results
				vector<Mat> res_imgs(topK);
				vector<Mat> res_gradients(topK);
				vector<Mat> res_normals(topK);
				vector<Mat> res_depth(topK);
				vector<Mat> db_boxes(topK);
				vector<Mat> res_masks(topK);
				for(size_t i=0; i<topK; i++) {
					VisualObject& cur_obj = patch_meta.objects[matches[i].trainIdx];
					// mask
					cur_obj.visual_desc.mask.convertTo(res_masks[i], CV_8U, 255);
					// gray
					cur_obj.visual_desc.extra_features["gray"].convertTo(res_imgs[i], CV_8U, 255);
					// gradient
					//ImgVisualizer::DrawFloatImg("", cur_obj.visual_desc.extra_features["gradient"], res_gradients[i], false);
					// 3D
					if(use_depth) 
					{
						// normal
						tools::ImgVisualizer::DrawNormals("", cur_obj.visual_desc.extra_features["normal"], res_normals[i]);
						// depth
						normalize(cur_obj.visual_desc.extra_features["depth"], res_depth[i], 1, 0, NORM_MINMAX);
						res_depth[i].convertTo(res_depth[i], CV_8U, 255);
						//cur_obj.visual_desc.extra_features["depth"].convertTo(res_depth[i], CV_8U, 255);
					}
					// box on image
					db_boxes[i] = imread(patch_meta.objects[matches[i].trainIdx].imgpath);
					resize(db_boxes[i], db_boxes[i], Size(cimg.cols, cimg.rows));
					rectangle(db_boxes[i], patch_meta.objects[matches[i].trainIdx].visual_desc.box, CV_RGB(255,0,0), 2);
				}
				Mat out_img;
				sprintf_s(str, "res_gray_%d.jpg", cnt);
				ImgVisualizer::DrawImgCollection(str, res_imgs, topK, Size(50,50), out_img);
				imwrite(str, out_img);
				
				sprintf_s(str, "res_normal_%d.jpg", cnt);
				ImgVisualizer::DrawImgCollection(str, res_normals, topK, Size(50,50), out_img);
				imwrite(str, out_img);

				sprintf_s(str, "res_depth_%d.jpg", cnt);
				ImgVisualizer::DrawImgCollection(str, res_depth, topK, Size(50,50), out_img);
				imwrite(str, out_img);

				/*sprintf_s(str, "res_gradient_%d.jpg", cnt);
				tools::ImgVisualizer::DrawImgCollection(str, res_gradients, topK, Size(50,50), out_img);
				imwrite(str, out_img);*/

				sprintf_s(str, "res_mask_%d.jpg", cnt);
				tools::ImgVisualizer::DrawImgCollection(str, res_masks, topK, Size(50,50), out_img);
				imwrite(str, out_img);

				sprintf_s(str, "res_box_%d.jpg", cnt);
				tools::ImgVisualizer::DrawImgCollection(str, db_boxes, topK/2, Size(200, 200), out_img);
				imwrite(str, out_img);

				waitKey(0);
#endif

				cout<<total_cnt--<<endl;
			}
		}
	}
	cout<<"match done. Time cost: "<<(getTickCount()-start_t)/getTickFrequency()<<"s."<<endl;

	//score_map(Rect(patch_size.width/2, patch_size.height/2, score_map.cols-patch_size.width/2, score_map.rows-patch_size.height/2)).copyTo(score_map);
	//score_map.setTo(max_dist, 255-edge_map);
	normalize(score_map, score_map, 1, 0, NORM_MINMAX);
	score_map = 1-score_map;
	//tools::ImgVisualizer::DrawFloatImg("bmap", score_map);

	mask_map /= max_dist;
	cout<<max_dist<<endl;
	normalize(mask_map, mask_map, 1, 0, NORM_MINMAX);
	//tools::ImgVisualizer::DrawFloatImg("maskmap", mask_map);

	//normalize(mask_vote_map, mask_vote_map, 1, 0, NORM_MINMAX);
	//ImgVisualizer::DrawFloatImg("vote map", mask_vote_map);
	//waitKey(0);

	return true;

	// pick top weighted points to see if they are inside objects
	// try graph-cut for region proposal
	// among all retrieved mask patch, select most discriminative one and do graph-cut
	sort(query_patches.begin(), query_patches.end(), [](const VisualObject& a, const VisualObject& b) { 
		return a.visual_data.scores[1] > b.visual_data.scores[1]; });
	for(size_t i=0; i<query_patches.size(); i++) {
		Mat disp_img = cimg.clone();
		rectangle(disp_img, query_patches[i].visual_data.bbox, CV_RGB(255,0,0));
		imshow("max std box", disp_img);
		Mat big_mask;
		resize(query_patches[i].visual_data.mask, big_mask, Size(50,50));
		ImgVisualizer::DrawFloatImg("max std mask", big_mask);
		waitKey(0);
		// use mask to do graph-cut
		Mat fg_mask(cimg.rows, cimg.cols, CV_8U);
		fg_mask.setTo(cv::GC_PR_FGD);
		Mat th_mask;
		threshold(query_patches[i].visual_data.mask, th_mask, query_patches[i].visual_data.scores[0], 1, CV_THRESH_BINARY);
		th_mask.convertTo(th_mask, CV_8U);
		fg_mask(query_patches[i].visual_data.bbox).setTo(cv::GC_FGD, th_mask);
		th_mask = 1-th_mask;
		fg_mask(query_patches[i].visual_data.bbox).setTo(cv::GC_BGD, th_mask);
		cv::grabCut(cimg, fg_mask, Rect(0,0,1,1), Mat(), Mat(), 3, cv::GC_INIT_WITH_MASK);
		fg_mask = fg_mask & 1;
		disp_img.setTo(Vec3b(0,0,0));
		cimg.copyTo(disp_img, fg_mask);
		cv::imshow("cut", disp_img);
		cv::waitKey(0);
	}


	float ths[] = {0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.3f, 0.2f};
	for(size_t i=0; i<8; i++) {
		Mat th_mask;
		threshold(mask_map, th_mask, ths[i], 1, CV_THRESH_BINARY);
		char str[30];
		sprintf_s(str, "%f", ths[i]);
		ImgVisualizer::DrawFloatImg(str, th_mask);
		waitKey(0);
	}

	return true;
}
Ejemplo n.º 6
0
void hogTrain(char* trainFileList, vector<string>& labels) {
    ifstream fs;
    fs.open(trainFileList);
    if (fs == NULL) {
        cerr << "Error: training file list \"" << trainFileList<< "\" does not exist\n";
        exit(-1);
    }
    
    HOGDescriptor hog(hogWinSize, hogBlockSize, hogBlockStride, hogCellSize, hogNBins);
    DEBUGMODE {
        cout << "HOG descriptor length = " << hog.getDescriptorSize() << endl;
    }
    
    string label, imgpath;
    ofstream dataFile;
    string dataFilePath;
    dataFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(trainDataFileName);
    dataFile.open(dataFilePath.c_str());
    cout << "Extracting HOG features ...\n";
    while (fs >> label) {
        int catnum = hasCategory(labels, label);
        if (catnum < 0) {
            labels.push_back(label);
            catnum = (int) labels.size() - 1;
        }
        
        fs >> imgpath;
        DEBUGMODE {
            cout << "Extracting HOG features for " << imgpath << endl;
        }
        Mat img = imread(imgpath);
        assert(img.data);  // image should not be empty
        Mat resizedImg = Mat::zeros(hogWinSize.width, hogWinSize.height, CV_8UC3);
        vector<float> featVec;
        resize(img, resizedImg, hogWinSize, 0, 0, INTER_CUBIC);
        hog.compute(resizedImg, featVec, hogWinStride);  // compute HOG feature
        
        // Store features into files following the libsvm dataset format:
        // <label> 1:<feat_1> 2:<feat_2> ... d:<feat_d>
        // where d is the number of features (i.e. dimensionality).
        dataFile << catnum << " ";  // write label
        for (int i = 0; i < featVec.size(); i++) {   // write data
            dataFile << i+1 << ":" << featVec[i] << " ";
        }
        dataFile << endl;
    }
    
    fs.close();
    dataFile.close();
    
    string svmScaleFilePath, scaledDataFilePath, svmModelFilePath;
    svmScaleFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(svmScaleRangeFileName);
    scaledDataFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(scaledTrainDataFileName);
    svmModelFilePath.append(ROOT_DIR).append(FEATURES_DIR).append(svmModelFileName);
    
    string command;  // UNIX command
    
    // scale data to range [-1 1]
    command.append(ROOT_DIR).append(LIBSVM_DIR).append("svm-scale -l -1 -u 1 -s ").append(svmScaleFilePath).append(" ").append(dataFilePath).append(" > ").append(scaledDataFilePath);
    cout << "Scaling training data ... \n";
    DEBUGMODE {
        cout << command << endl;
    }
    system(command.c_str()); // execute UNIX command
    
    // train SVM classifier
    command.clear();
    svmTrainRoutine.append(svmParam);
    DEBUGMODE {} else svmTrainRoutine.append("-q ");
Ejemplo n.º 7
0
void Model3D::recursiveTextureLoad(const struct aiScene *sc, const struct aiNode* nd) {
	int i;
	unsigned int n = 0, t;
	struct aiMatrix4x4 m = nd->mTransformation;

	// update transform
	aiTransposeMatrix4(&m);
	glPushMatrix();
	glMultMatrixf((float*) &m);

	// draw all meshes assigned to this node
	for (; n < nd->mNumMeshes; ++n) {
		const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
		unsigned int cont = aiGetMaterialTextureCount(sc->mMaterials[mesh->mMaterialIndex], aiTextureType_AMBIENT);
		struct aiString* str = (aiString*) malloc(sizeof(struct aiString));

		if (cont > 0) {
			//aiGetMaterialString(sc->mMaterials[mesh->mMaterialIndex],AI_MATKEY_TEXTURE_DIFFUSE(0),str);
			aiGetMaterialTexture(sc->mMaterials[mesh->mMaterialIndex], aiTextureType_AMBIENT, 0, str, 0, 0, 0, 0, 0, 0);

			// See if another mesh is already using this texture, if so, just copy GLuint instead of remaking entire texture
			bool newTextureToBeLoaded = true;
			for (int x = 0; x < texturesAndPaths.size(); x++) {
				if (texturesAndPaths[x].pathName == *str) {
					TextureAndPath reusedTexture;
					reusedTexture.hTexture = texturesAndPaths[x].hTexture;
					reusedTexture.pathName = *str;
					//printf("%s pathnm \n", reusedTexture.pathName.data);
					texturesAndPaths.push_back(reusedTexture);
					newTextureToBeLoaded = false;

					std::cout << "Texture reused." << std::endl;

					break;
				}
			}

			if (newTextureToBeLoaded) {

				texturename.clear();
				texturename.append(foldername.c_str());
				texturename.append(str->data);

				std::cout << texturename.c_str() << " strname \n";
				Mat imagen=imread(texturename.c_str());
                                int w = imagen.cols;
				int h = imagen.rows;

                                int dataSize=imagen.cols*imagen.rows*3;
                                unsigned char* data=new unsigned char[dataSize];
                                int j=0;
                                for( int y = 0; y < imagen.rows; y++ )
                                { 
                                    for( int x = 0; x < imagen.cols; x++ )
                                             { 
                                                for( int c = 0; c < 3; c++ )
                                                {
                                                     data[j] = imagen.at<Vec3b>(y,x)[c];
                                                     j++;           
                                                }
                                             }
                                }

                                //Now generate the OpenGL texture object
				TextureAndPath newTexture;
				newTexture.pathName = *str;
				glGenTextures(1, &newTexture.hTexture);

				glBindTexture(GL_TEXTURE_2D, newTexture.hTexture);
				//glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_BGR,GL_UNSIGNED_BYTE,(GLvoid*)pixeles );
				//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                                gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imagen.cols, imagen.rows, GL_BGR, GL_UNSIGNED_BYTE, data);
                                
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

				glBindTexture(GL_TEXTURE_2D, newTexture.hTexture);

				GLenum huboError = glGetError();

				if (huboError) {
					std::cout << "There was an error loading the texture" << std::endl;
				}

				std::cout << "texture loaded." << std::endl;

				texturesAndPaths.push_back(newTexture);
			}
		}
	}

	// Get textures from all children
	for (n = 0; n < nd->mNumChildren; ++n)
		recursiveTextureLoad(sc, nd->mChildren[n]);
}
Ejemplo n.º 8
0
#include "opencv2/core/core.hpp"

Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
if(img1.empty() || img2.empty())
{
    printf("Can't read one of the images\n");
    return -1;
}

// detecting keypoints
SurfFeatureDetector detector(400);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);

// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);

// matching descriptors
BruteForceMatcher<L2<float> > matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);

// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
Ejemplo n.º 9
0
ImageFace::ImageFace()
{    
    Mat load_image = imread("img/face_pic_1.jpg");
    load_image.copyTo(image_ROI); 
    
}
Xulyanh::Xulyanh(string filename)
{
    source = imread(filename, CV_LOAD_IMAGE_COLOR);
}
Ejemplo n.º 11
0
void GrabCutMF::Demo(CStr &wkDir, float w1, float w2, float w3, float alpha, float beta, float gama, float mu)
{	
	CStr imgDir = wkDir + "Imgs/", salDir = wkDir + "Sal4N/", iluDir = wkDir + "Ilu4N/";
	vecS namesNE;
	int imgNum = CmFile::GetNamesNE(imgDir + "*.jpg", namesNE);
	CmFile::MkDir(salDir);
	CmFile::MkDir(iluDir);
	printf("w1 = %g, w2 = %g, w3 = %g, alpha = %g, beta = %g, gama = %g, mu = %g\n", w1, w2, w3, alpha, beta, gama, mu);

	// Number of labels
	//const int M = 2;
	CmTimer tm("Time"), tmIni("TimeIni"), tmRef("TimeRef");
	double maxWeight = 2; // 2: 0.958119, 1: 0.953818, 
	tm.Start();
#pragma omp parallel for
	for (int i = 0; i < imgNum; i++){
		printf("Processing %d/%d: %s%s.jpg%20s\r\n", i, imgNum, _S(imgDir), _S(namesNE[i]), "");
		CmFile::Copy(imgDir + namesNE[i] + ".jpg", salDir + namesNE[i] + ".jpg");
		//CmFile::Copy(imgDir + namesNE[i] + ".png", salDir + namesNE[i] + "_GT.png");
		Mat _imMat3u = imread(imgDir + namesNE[i] + ".jpg"), imMat3f, imMat3u, gt1u;
		Mat _gt1u = imread(imgDir + namesNE[i] + ".png", CV_LOAD_IMAGE_GRAYSCALE);
		if(_gt1u.rows == 0 && _gt1u.cols == 0) {
            cout<<"Error: unable to open "<<(imgDir + namesNE[i] + ".png")<<endl;
            continue;
		}
		blur(_gt1u, _gt1u, Size(3,3));
		Mat _res1u = Mat::zeros(_imMat3u.size(), CV_8U);
		Rect wkRect = CmCv::GetMaskRange(_gt1u, 30, 200);
		_imMat3u(wkRect).copyTo(imMat3u);
		_gt1u(wkRect).copyTo(gt1u);
		imMat3u.convertTo(imMat3f, CV_32FC3, 1/255.0);
		Rect rect = CmCv::GetMaskRange(gt1u, 5, 128);


		Mat edge1u; // Use an edge map to expand the background mask in flat (no edge) region
		CmCv::CannySimpleRGB(imMat3u, edge1u, 120, 1200, 5);
		dilate(edge1u, edge1u, Mat(), Point(-1, -1), 3);
		Mat borderMask1u(imMat3u.size(), CV_8U), tmpMask;
		memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows);
		borderMask1u(rect) = Scalar(0);
		getGrabMask(edge1u, borderMask1u);


		//* The Mean field based GrabCut
		//tmIni.Start();
		GrabCutMF cutMF(imMat3f, imMat3u, salDir + namesNE[i], w1, w2, w3, alpha, beta, gama, mu);
		//Mat borderMask1u = CmCv::getGrabMask(imMat3u, rect), tmpMask;
		imwrite(salDir + namesNE[i] + "_BM.png", borderMask1u);
		imwrite(salDir + namesNE[i] + ".jpg", imMat3u);
		//imwrite(salDir + namesNE[i] + "_GT.png", gt1u);
		cutMF.initialize(rect, borderMask1u, (float)maxWeight, true);
		//cutMF.setGrabReg(rect, CmCv::getGrabMask(imMat3u, rect));
		//tmIni.Stop();
		//tmRef.Start();
		cutMF.refine();
		//tmRef.Stop();
		
		Mat res1u = cutMF.drawResult(), invRes1u;

		res1u.copyTo(_res1u(wkRect));
		imwrite(salDir + namesNE[i] + "_GCMF1.png", _res1u);

		//if (sum(res1u).val[0] < EPS){
		//	printf("%s.jpg don't contains a salient object\n", _S(namesNE[i]));
		//	continue;
		//}

		dilate(res1u(rect), tmpMask, Mat(), Point(-1, -1), 10);	
		bitwise_not(tmpMask, borderMask1u(rect));
		getGrabMask(edge1u, borderMask1u);

		//blur(res1u, invRes1u, Size(3, 3));
		//
		//PointSeti hullPnts;
		//convexHullOfMask(invRes1u, hullPnts);
		//fillConvexPoly(invRes1u, hullPnts, 255);
		//bitwise_not(invRes1u, invRes1u);
		//bitwise_or(invRes1u, borderMask1u, borderMask1u);
		imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u);

		
		//double w =  maxWeight - (maxWeight-1)*sum(res1u).val[0]/(borderMask1u.rows*borderMask1u.cols*255 - sum(borderMask1u).val[0]);
		cutMF.initialize(rect, borderMask1u, 2);
		cutMF.refine();

		//printf("weight = %g\n", w);
		//imshow("Result", res1u);
		//imshow("Possible", borderMask1u);
		//imshow("Image", imMat3f);
		//waitKey(0);

		res1u = cutMF.drawResult();

		Rect rectRes = CmCv::GetMaskRange(res1u, 5, 128);
		if (rectRes.width * 1.1 < rect.width || rectRes.height * 1.1 < rect.height){ // Too short result
			printf("%s.jpg contains a small object\n", _S(namesNE[i]));
			memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows);
			borderMask1u(rect) = Scalar(0);
			cutMF.initialize(rect, borderMask1u, 2);
			cutMF.refine();
			res1u = cutMF.drawResult();
			imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u);
			CmFile::Copy2Dir(salDir + namesNE[i] + "*.*", iluDir);
			imwrite(iluDir + namesNE[i] + "_GCMF.png", _res1u);
		}

		res1u.copyTo(_res1u(wkRect));
		imwrite(salDir + namesNE[i] + "_GCMF.png", _res1u);
		
	}
	tm.Stop();
	double avgTime = tm.TimeInSeconds()/imgNum;
	printf("Speed: %gs, %gfps\t\t\n", avgTime, 1/avgTime);
	//tmIni.Report();
	//tmRef.Report();
	//CmEvaluation::EvalueMask(imgDir + "*.png", salDir, ".png", "_GC.png");

	
	char* pDes[] = { "GCMF1", "GCMF"}; //, "CudaG4", "Onecut", "GC", "CudaH", 
	vecS des  = charPointers2StrVec (pDes);
	CStr rootDir = CmFile::GetFatherFolder(wkDir), dbName = CmFile::GetNameNE(wkDir.substr(0, wkDir.size() - 1));
	CmEvaluation::EvalueMask(imgDir + "*.png", salDir, des, wkDir.substr(0, wkDir.size() - 1) + "Res.m", 0.3, false, "", dbName);
}
Ejemplo n.º 12
0
///////////////////////////////////////////////////////
// Panel::CalibrateCamera() Description
///////////////////////////////////////////////////////
void Panel::CalibrateCamera(string sFilePath)
{
	help();

	//! [file_read]
	Settings s;
	const string inputSettingsFile = sFilePath;
	FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings
	if (!fs.isOpened())
	{
		cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl;
//		return -1;
	}
	fs["Settings"] >> s;
	fs.release();                                         // close Settings file
	//! [file_read]

	//FileStorage fout("settings.yml", FileStorage::WRITE); // write config as YAML
	//fout << "Settings" << s;

	if (!s.goodInput)
	{
		cout << "Invalid input detected. Application stopping. " << endl;
//		return -1;
	}

	vector<vector<Point2f> > imagePoints;
	Mat cameraMatrix, distCoeffs;
	Size imageSize;
	int mode = s.inputType == Settings::IMAGE_LIST ? CAPTURING : DETECTION;
	clock_t prevTimestamp = 0;
	const Scalar RED(0, 0, 255), GREEN(0, 255, 0);
	const char ESC_KEY = 27;
	int counter = 1;

	//! [get_input]
	for (;;)
	{
		Mat view;
		bool blinkOutput = false;

		view = s.nextImage();

		//-----  If no more image, or got enough, then stop calibration and show result -------------
		if (mode == CAPTURING && imagePoints.size() >= (size_t)s.nrFrames)
		{
			if (runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints))
				mode = CALIBRATED;
			else
				mode = DETECTION;
		}
		if (view.empty())          // If there are no more images stop the loop
		{
			// if calibration threshold was not reached yet, calibrate now
			if (mode != CALIBRATED && !imagePoints.empty())
				runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints);
			break;
		}
		//! [get_input]

		imageSize = view.size();  // Format input image.
		if (s.flipVertical)    flip(view, view, 0);

		//! [find_pattern]
		vector<Point2f> pointBuf;

		bool found;
		switch (s.calibrationPattern) // Find feature points on the input format
		{
		case Settings::CHESSBOARD:
			found = findChessboardCorners(view, s.boardSize, pointBuf,
				CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);
			break;
		case Settings::CIRCLES_GRID:
			found = findCirclesGrid(view, s.boardSize, pointBuf);
			break;
		case Settings::ASYMMETRIC_CIRCLES_GRID:
			found = findCirclesGrid(view, s.boardSize, pointBuf, CALIB_CB_ASYMMETRIC_GRID);
			break;
		default:
			found = false;
			break;
		}
		//! [find_pattern]
		//! [pattern_found]
		if (found)                // If done with success,
		{
			// improve the found corners' coordinate accuracy for chessboard
			if (s.calibrationPattern == Settings::CHESSBOARD)
			{
				Mat viewGray;
				cvtColor(view, viewGray, COLOR_BGR2GRAY);
				cornerSubPix(viewGray, pointBuf, Size(11, 11),
					Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
			}

			if (mode == CAPTURING &&  // For camera only take new samples after delay time
				(!s.inputCapture.isOpened() || clock() - prevTimestamp > s.delay*1e-3*CLOCKS_PER_SEC))
			{
				imagePoints.push_back(pointBuf);
				prevTimestamp = clock();
				blinkOutput = s.inputCapture.isOpened();
			}

			// Draw the corners.
			drawChessboardCorners(view, s.boardSize, Mat(pointBuf), found);
		}
		//! [pattern_found]
		//----------------------------- Output Text ------------------------------------------------
		//! [output_text]
		string msg = (mode == CAPTURING) ? "100/100" :
			mode == CALIBRATED ? "Calibrated" : "Press 'g' to start";
		int baseLine = 0;
		Size textSize = getTextSize(msg, 1, 1, 1, &baseLine);
		Point textOrigin(view.cols - 2 * textSize.width - 10, view.rows - 2 * baseLine - 10);

		if (mode == CAPTURING)
		{
			if (s.showUndistorsed)
				msg = format("%d/%d Undist", (int)imagePoints.size(), s.nrFrames);
			else
				msg = format("%d/%d", (int)imagePoints.size(), s.nrFrames);
		}

		putText(view, msg, textOrigin, 1, 1, mode == CALIBRATED ? GREEN : RED);

		if (blinkOutput)
			bitwise_not(view, view);
		//! [output_text]
		//------------------------- Video capture  output  undistorted ------------------------------
		//! [output_undistorted]
		if (mode == CALIBRATED && s.showUndistorsed)
		{
			Mat temp = view.clone();
			undistort(temp, view, cameraMatrix, distCoeffs);
		}
		//! [output_undistorted]
		//------------------------------ Show image and check for input commands -------------------
		//! [await_input]
		
		namedWindow("Image View" + to_string(counter), WINDOW_NORMAL);
		resizeWindow("Image View" + to_string(counter), 640, 480);
		imshow("Image View" + to_string(counter), view);
		char key = (char)waitKey(s.inputCapture.isOpened() ? 50 : s.delay);

		cout << "Image " << to_string(counter) << " Completed" << endl;
		counter++;

		if (key == ESC_KEY)
			break;

		if (key == 'u' && mode == CALIBRATED)
			s.showUndistorsed = !s.showUndistorsed;

		if (s.inputCapture.isOpened() && key == 'g')
		{
			mode = CAPTURING;
			imagePoints.clear();
		}
		//! [await_input]
	}

	// -----------------------Show the undistorted image for the image list ------------------------
	//! [show_results]
	if (s.inputType == Settings::IMAGE_LIST && s.showUndistorsed)
	{
		Mat view, rview, map1, map2;
		initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
			getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
			imageSize, CV_16SC2, map1, map2);

		m_mainMap1 = map1;
		m_mainMap2 = map2;

		for (size_t i = 0; i < s.imageList.size(); i++)
		{
			view = imread(s.imageList[i], 1);
			if (view.empty())
				continue;
			remap(view, rview, map1, map2, INTER_LINEAR);
			imshow("Image View", rview);
			char c = (char)waitKey();
			if (c == ESC_KEY || c == 'q' || c == 'Q')
				break;
		}
	}
	//! [show_results]

//	return 0;

}
Ejemplo n.º 13
0
///////////////////////////////////////////////////////
// Panel::PixelsToLength() 
//  Description: Not currently used: Calculates the
// ratio of cm to pixels from one checkerboard image
///////////////////////////////////////////////////////
void Panel::PixelsToLength(string sImgPath)
{
	cout << "Pixels to Length" << endl << endl;
	m_pPanel->m_Image = imread(sImgPath);

	Point2f corner1;
	Point2f corner2;
	Point2f corner3;
	Point2f corner4;

	bool found = false;

	found = findChessboardCorners(m_pPanel->m_Image, Size(9, 6), m_pPanel->corners,
		CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);

	Mat imgGray;
	cvtColor(m_pPanel->m_Image, imgGray, COLOR_BGR2GRAY);
	cornerSubPix(imgGray, m_pPanel->corners, Size(11, 11), Size(-1, -1),
		TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));

	corner1.x = m_pPanel->corners[45].x;
	corner1.y = m_pPanel->corners[45].y;

	corner2.x = m_pPanel->corners[0].x;
	corner2.y = m_pPanel->corners[0].y;

	corner3.x = m_pPanel->corners[8].x;
	corner3.y = m_pPanel->corners[8].y;

	corner4.x = m_pPanel->corners[53].x;
	corner4.y = m_pPanel->corners[53].y;

	// Draw rectangle around checkerboard

	line(m_pPanel->m_Image, corner1, corner2, CV_RGB(0, 0, 255), 2);
	line(m_pPanel->m_Image, corner2, corner3, CV_RGB(0, 0, 255), 2);
	line(m_pPanel->m_Image, corner3, corner4, CV_RGB(0, 0, 255), 2);
	line(m_pPanel->m_Image, corner4, corner1, CV_RGB(0, 0, 255), 2);

	circle(m_pPanel->m_Image, corner1, 10, CV_RGB(0, 0, 255), 2);
	circle(m_pPanel->m_Image, corner2, 10, CV_RGB(0, 255, 0), 2);
	circle(m_pPanel->m_Image, corner3, 10, CV_RGB(255, 0, 0), 2);
	circle(m_pPanel->m_Image, corner4, 10, CV_RGB(100, 100, 100), 2);

	double pixel_length = 0;
	double pixel_width = 0;

	double pixel_width1 = norm(corner1 - corner2);
	double pixel_length1 = norm(corner2 - corner3);
	double pixel_width2 = norm(corner3 - corner4);
	double pixel_length2 = norm(corner4 - corner1);

	if (pixel_length1 >= pixel_length2)
		pixel_length = pixel_length1;
	else 
		pixel_length = pixel_length2;

	if (pixel_width1 >= pixel_width2)
		pixel_width = pixel_width1;
	else
		pixel_width = pixel_width2;


	double ratio = (m_pPanel->m_boardLength - 1.0) / (m_pPanel->m_boardWidth - 1.0);

	if (pixel_length >= (pixel_width * ratio)){
		m_pPanel->m_cmPerPixel = (m_pPanel->m_squareSize * (float)(m_pPanel->m_boardLength - 1)) / pixel_length;
	}
	else
		m_pPanel->m_cmPerPixel = (m_pPanel->m_squareSize * (float)(m_pPanel->m_boardWidth - 1)) / pixel_width;

	cout << "cm per pixel : " << m_pPanel->m_cmPerPixel << endl;

	// Perspective Transform
	//	double ratio = 8.0 / 5.0;
	//	double length = ratio * pixel_width;
	//
	//	vector<Point2f> panel_pts;
	//	vector<Point2f> rect_pts;
	//	panel_pts.push_back(corner1);
	//	panel_pts.push_back(corner2);
	//	panel_pts.push_back(corner3);
	//	panel_pts.push_back(corner4);
	//	rect_pts.push_back(Point2f(0, 0));
	//	rect_pts.push_back(Point2f((float)width, 0));
	//	rect_pts.push_back(Point2f((float)width, (float)length));
	//	rect_pts.push_back(Point2f(0, (float)length));
	//
	//	// Draw new rectangle
	//	line(m_pPanel->m_Image, rect_pts[0], rect_pts[1], CV_RGB(255, 0, 0), 2);
	//	line(m_pPanel->m_Image, rect_pts[1], rect_pts[2], CV_RGB(255, 0, 0), 2);
	//	line(m_pPanel->m_Image, rect_pts[2], rect_pts[3], CV_RGB(255, 0, 0), 2);
	//	line(m_pPanel->m_Image, rect_pts[3], rect_pts[0], CV_RGB(255, 0, 0), 2);
	//
	//	// Perspective Transorm
	//	Mat transmtx = getPerspectiveTransform(panel_pts, rect_pts);
	//	int offsetSize = 500;
	//	Mat transformed = Mat::zeros(m_pPanel->m_Image.cols + offsetSize, m_pPanel->m_Image.rows + offsetSize, CV_8UC3);
	//	warpPerspective(m_pPanel->m_Image, transformed, transmtx, transformed.size());
	//
	////	Mat subImg(transformed, Rect(corner1.x, corner1.y, width, length));
	//
	//	namedWindow("Original", WINDOW_NORMAL);
	//	imshow("Original", m_pPanel->m_Image);
	//	namedWindow("Warped", WINDOW_AUTOSIZE);
	//	imshow("Warped", transformed);
}
Ejemplo n.º 14
0
//./Sp_demo -i <img_filename> -n <nPixels_on_side> --i_std <i_std>
int main( int argc, char** argv )
{
    
    // get the image filename, nPixels_in_square_side and i_std
    // the defaults
    String filename = "image/2.jpg";
    int nPixels_in_square_side = 15;
    int i_std = 20;

    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        if ((arg == "-h") || (arg == "--help")) {
            show_usage(argv[0]);
            return 0;
        } 
        else if ((arg == "-i") || (arg == "--img_filename")) {
            if (i + 1 < argc) { 
                i++;
                filename = argv[i];
            } else {
                std::cerr << "--img_filename option requires one argument." << std::endl;
                return 1;
            }  
        } 
        else if ((arg == "-n") || (arg == "--nPixels_on_side")) {
            if (i + 1 < argc) { 
                i++;
                nPixels_in_square_side = atoi (argv[i]);
                if (nPixels_in_square_side<3) {
                    std::cerr << "--nPixels_in_square_side option requires nPixels_in_square_side >= 3." << std::endl;
                    return 1;
                }

            } else {
                std::cerr << "--nPixels_on_side option requires one argument." << std::endl;
                return 1;
            }  
        }
        else if (arg == "--i_std") {
            if (i + 1 < argc) { 
                i++;
                i_std = atoi (argv[i]);
                
                if (i_std<5 || i_std>40) {
                    std::cerr << "--i_std option requires 5<= value <=40." << std::endl;
                    return 1;
                }
            } else {
                std::cerr << "--i_std option requires a number." << std::endl;
                return 1;
            }  
        }else{  

        }
    }

    cout << "finish reading the arguments" << endl;

    Mat image = imread(filename, CV_LOAD_IMAGE_COLOR);
    // Check for invalid input
    if(! image.data ){
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    cout << "finish reading the image" << endl;
    //Part 1: Specify the parameters:
    superpixel_options spoptions = get_sp_options(nPixels_in_square_side, i_std);

    // Part 2 : prepare for segmentation
    int dimy = image.rows;
    int dimx = image.cols;
    Superpixels sp = Superpixels(dimx, dimy, spoptions);
    //cout << "finish init sp" << endl;
    cudaDeviceSynchronize();
    sp.load_img((unsigned char*)(image.data));

   //cout << "finish loading the image" << endl;

    // Part 3: Do the superpixel segmentation
    clock_t start,finish;
    start = clock();
    sp.calc_seg();
    cudaDeviceSynchronize();

    sp.gpu2cpu();
    
    cudaError_t err_t = cudaDeviceSynchronize();
    if (err_t){
        std::cerr << "CUDA error after cudaDeviceSynchronize. " << std::endl;
        return 0;
    }
    finish = clock();
    cout<< "Segmentation takes " << ((double)(finish-start)/CLOCKS_PER_SEC) << " sec" << endl;

    // Part 4: Save the mean/boundary image 
    
    Mat border_img = sp.get_img_overlaid();
    String fname_res_border = "image/result/img_border.png";
    imwrite(fname_res_border, border_img);
    cout << "saved " << fname_res_border << endl;
    

    Mat mean_img = sp.get_img_cartoon();
    String fname_res_mean = "image/result/img_mean.png";
    imwrite(fname_res_mean, mean_img);
    cout << "saving " << fname_res_mean << endl;

                
    return 0;
}
Ejemplo n.º 15
0
int Detector::batchDetect(const string &src_image_folder, const string &dst_image_folder)
{
	if (access(src_image_folder.c_str(), 0) == -1)
	{
		return 0;
	}

	if (access(dst_image_folder.c_str(), 0) == -1)
	{
		_mkdir(dst_image_folder.c_str());
	}


	const int MAX_DETECTION = 500;
	CB_RectT rects[MAX_DETECTION];

	_chdir(src_image_folder.c_str());
	CFileFind file_finder;
    bool is_working = file_finder.FindFile();
	int count = 0;
	while (is_working)
	{
		is_working = file_finder.FindNextFile();
		string file_name = file_finder.GetFileName();
		string file_path = file_finder.GetFilePath();

		if (file_name == "." || file_name == ".."|| file_name == "Thumbs.db")
		{
			continue;
		}

		printf("%s\n", file_path.c_str());

		Mat image = imread(file_path, CV_LOAD_IMAGE_GRAYSCALE);
		if (image.data == NULL)
		{
			continue;
		}
		double ratio = 1.0;
		if (param.resize_w > 0 && param.resize_h > 0)
		{
			int w = param.resize_w;
			int h = image.rows * w / image.cols;
			ratio = (double)image.cols / w;
			Size size(w, h);
			resize(image, image, size);
		}

		param.hot_rect.top *= (image.rows / 100);
		param.hot_rect.bottom *= (image.rows / 100);
		param.hot_rect.left *= (image.cols / 100);
		param.hot_rect.right = (image.cols / 100);

		int subwin_count;
		int num = detect(image, MAX_DETECTION, rects, subwin_count);

		Mat dst_image = imread(file_path, CV_LOAD_IMAGE_COLOR);
		file_path = dst_image_folder + "\\" + file_name;
		drawRects(dst_image, num, rects, Scalar(0, 255, 0), 3, ratio);
		
		num = merger.merge(num, rects);
		drawRects(dst_image, num, rects, Scalar(0, 0, 255), 7, ratio);

		imwrite(file_path, dst_image);
		count++;
 	}
	return 1;
}
Ejemplo n.º 16
0
void Build_Vocabulary::extract(String filename, double threshold ,double thresholdCorner){
    
    clock_t start, finish;   
    double elapsed_time;   

    string dir = "Validation/" + filename, filepath;
	DIR *dp;
	struct dirent *dirp;
	struct stat filestat;
	
	dp = opendir( dir.c_str() );
	
	// detecting keypoints
	SiftFeatureDetector detector(threshold, thresholdCorner);
	vector<KeyPoint> keypoints;	
	
	// computing descriptors
    //SiftDescriptorExtractor() extractor;
    Ptr<DescriptorExtractor > extractor(
                                        new OpponentColorDescriptorExtractor(
                                                                             Ptr<DescriptorExtractor>(new SiftDescriptorExtractor())
                                                                             )
                                        );
	Mat descriptors;
	Mat training_descriptors;
	Mat img;
	
	cout << "------- Build Vocabulary ---------\n";
	int count = 0;
	cout << "Extract descriptors.."<<endl;
    start=time(NULL);       
	while ((dirp = readdir( dp )))
    {
		filepath = dir + "/" + dirp->d_name;
		
		// If the file is a directory (or is in some way invalid) we'll skip it 
		if (stat( filepath.c_str(), &filestat )) continue;
		if (S_ISDIR( filestat.st_mode ))         continue;
		
		img = imread(filepath);
		if (!img.data) {
			continue;
		}

		detector.detect(img, keypoints);
        {
            Mat out; img.copyTo(out);
            drawKeypoints(img, keypoints, out, Scalar(255));
            imshow("fg",img);
            imshow("keypoints", out);
            finish=time(NULL);
            elapsed_time = finish-start;
            int hours = (int) elapsed_time / 3600;
            int minutes = (int) (elapsed_time - hours * 3600) / 60;
            int seconds = (int) elapsed_time - hours * 3600 - minutes * 60;
            cout << "Elapsed Time: " << hours << ":" << minutes << ":" << seconds << endl;
            waitKey(0);
        }
		extractor->compute(img, keypoints, descriptors);
		training_descriptors.push_back(descriptors);
		cout << ".";
        count++;
    }
	cout << endl;
	closedir( dp );

    cout << "Total Images Input: " << count << endl;
	cout << "Total descriptors: " << training_descriptors.rows << endl;
    

    finish=time(NULL);
    elapsed_time = finish-start;
    int hours = (int) elapsed_time / 3600;
    int minutes = (int) (elapsed_time - hours * 3600) / 60;
    int seconds = (int) elapsed_time - hours * 3600 - minutes * 60;
    cout << "Elapsed Time for Extracting: " << hours << ":" << minutes << ":" << seconds << endl << endl;
	
	FileStorage fs("training_descriptors_" + filename + "_color.txt", FileStorage::WRITE);
	fs << "training_descriptors" << training_descriptors;
	fs.release();
}
Ejemplo n.º 17
0
void execute(string fname)
{
	const unsigned mem_size = 4096;
	int v, u, t;
	char next_dir, dir = 'r';
	unsigned char mem[mem_size];
	unsigned char * pnt = mem;
	Mat src;
	Vec3b color, next_color;

	memset(mem, 0, sizeof(unsigned char) * mem_size);

	src = imread(fname);
	throw_null(src.data);

	v = 0;
	u = 0;
	
	do
	{
		color = src.at<Vec3b>(v, u);

		if((color[0] == 0x00) && (color[1] == 0xFF) && (color[2] == 0x00))
			(* pnt)++;
		else if((color[0] == 0x00) && (color[1] == 0x80) && (color[2] == 0x00))
			(* pnt)--;
		else if((color[0] == 0xFF) && (color[1] == 0x00) && (color[2] == 0x00))
		{
			pnt++;

			throw_if((mem + mem_size <= pnt))
		}
		else if((color[0] == 0x80) && (color[1] == 0x00) && (color[2] == 0x00))
		{
			pnt--;

			throw_if((pnt < mem));
		}
		else if((color[0] == 0xFF) && (color[1] == 0xFF) && (color[2] == 0x00))
		{
			if((* pnt) <= 0)
			{
				t = 0;
				
				next_color[0] = 0x80;
				next_color[1] = 0x80;
				next_color[2] = 0x00;

				do
				{
					throw_if(step(dir, v, u, src.size()));

					if(src.at<Vec3b>(v, u) == color)
						t++;
					else if(src.at<Vec3b>(v, u) == next_color)
						t--;
				}
				while(t >= 0);
			}
		}
		else if((color[0] == 0x80) && (color[1] == 0x80) && (color[2] == 0x00))
		{
			switch(dir)
			{
				case 'r':
				{
					next_dir = 'l';

					break;
				}
				case 'l':
				{
					next_dir = 'r';

					break;
				}
				case 't':
				{
					next_dir = 'b';

					break;
				}
				case 'b':
				{
					next_dir = 't';

					break;
				}
			}

			t = 0;
				
			next_color[0] = 0xFF;
			next_color[1] = 0xFF;
			next_color[2] = 0x00;

			do
			{
				throw_if(step(next_dir, v, u, src.size()));

				if(src.at<Vec3b>(v, u) == color)
					t++;
				else if(src.at<Vec3b>(v, u) == next_color)
					t--;
			}
			while(t >= 0);

			step(next_dir, v, u, src.size());
		}
		else if((color[0] == 0x00) && (color[1] == 0x00) && (color[2] == 0xFF))
			printf("%c", * pnt);
		else if((color[0] == 0x00) && (color[1] == 0x00) && (color[2] == 0x80))
			scanf("%c\n", pnt);
		else if((color[0] == 0x00) && (color[1] == 0xFF) && (color[2] == 0xFF))
		{
			// Поворот против часовой стрелки

			switch(dir)
			{
				case 'r':
				{
					next_dir = 't';

					break;
				}
				case 'l':
				{
					next_dir = 'b';

					break;
				}
				case 't':
				{
					next_dir = 'l';

					break;
				}
				case 'b':
				{
					next_dir = 'r';

					break;
				}
			}

			dir = next_dir;
		}
		else if((color[0] == 0x00) && (color[1] == 0x80) && (color[2] == 0x80))
		{
			// Поворот по часовой стрелке

			switch(dir)
			{
				case 'r':
				{
					next_dir = 't';

					break;
				}
				case 'l':
				{
					next_dir = 't';

					break;
				}
				case 't':
				{
					next_dir = 'r';

					break;
				}
				case 'b':
				{
					next_dir = 'l';

					break;
				}
			}

			dir = next_dir;
		}
		else
			throw_if(1);
	}
Ejemplo n.º 18
0
Archivo: Calib.cpp Proyecto: ALX5/PJS
Calib::Calib()
{
    Size boardSize(6,5); // Chessboard' size in corners with both color (nb of squares -1)
    int widthSquare = 40; // Width of a square in mm
    int heightSquare = 27;
    vector <Mat> images;

    // Getting the four images of the chessboard
    string imageFileName = "../src/mire1.jpg";
    images.push_back(imread(imageFileName, 1));

    imageFileName = "../src/mire2.jpg";
    images.push_back(imread(imageFileName, 1));

    imageFileName = "../src/mire3.jpg";
    images.push_back(imread(imageFileName, 1));

    imageFileName = "../src/mire4.jpg";
    images.push_back(imread(imageFileName, 1));

    Size imageSize = images.at(0).size();

    // Find chessboard's corners in the scene for the 4 images
    vector<vector<Point2f> > cornersScene(1);
    vector<Mat> imagesGray;

    imagesGray.resize(4);

    for (int i=0; i<4; i++)
    {
        if(images.at(i).empty())
        {
            cerr << "Image not read correctly!" << endl;
            exit(-1);
        }

        bool patternFound = findChessboardCorners(images.at(i), boardSize, cornersScene[0]);
        if(!patternFound)
        {
            cerr << "Could not find chess board!" << endl;
            exit(-1);
        }

        // Improve corner's coordinate accuracy
        cvtColor(images.at(i), imagesGray.at(i), CV_RGB2GRAY);
        cornerSubPix(imagesGray.at(i), cornersScene[0], Size(3,2), Size(-1,-1), TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1));

        // Drawing the corners
        drawChessboardCorners(images.at(i), boardSize, Mat(cornersScene[0]), patternFound );

        imshow("Corners find", images.at(i));

        int keyPressed;
        /*do
        {
            keyPressed = waitKey(0);
        } while (keyPressed != 27);*/
    }

    // Getting the chessboard's corners on the mire's image
    vector<vector<Point3f> > cornersMire(1);

    for( int y = 0; y < boardSize.height; y++ )
    {
        for( int x = 0; x < boardSize.width; x++ )
        {
            cornersMire[0].push_back(cv::Point3f(float(x*widthSquare),
                                                 float(y*heightSquare), 0));
        }
    }

    // Getting the camera's parameters

    Mat distortionCoefficients = Mat::zeros(8, 1, CV_64F);
    Mat cameraMatrix = Mat::eye(3, 3, CV_64F);

    calibrateCamera(cornersMire, cornersScene, imageSize, cameraMatrix,
                    distortionCoefficients, rotationVectors, translationVectors);


    //cout << "Camera matrix: " << cameraMatrix << endl;
    //cout << "Distortion _coefficients: " << distortionCoefficients << endl;
    cout << rotationVectors.at(0) << endl;
    cout << translationVectors.at(0) << endl;

}
Ejemplo n.º 19
0
void extractImages::processTask(extractImages& self,const vector<string>&  sourcesImages,const vector<string>& templatesImages)
{
    utils & u = utils::i();
    fileOp *  op = new fileOp();

    while(self.leftToProcess)
    {
        mtxExtract.lock();
        if(self.leftToProcess==0) break;
        self.leftToProcess--;
        string sourceImage = sourcesImages[self.currentToProcess++];
        mtxExtract.unlock();
   
        Mat img = imread(sourceImage);

        computeImages * ll = new computeImages(img, u.VERBOSE);
        string pageCourante;
        int imagesCount = 0;
        stringstream output;

        
        try{
            pageCourante = op->getFilename(sourceImage);
//            if(u.VERBOSE)output << "Traitement de la source : "<<sourceImage  << " ( " << pageCourante << " )" <<endl;
            if(u.VERBOSE)output << pageCourante << " ::";
            
            
            vector<templateArea> foundTemplate;
            
            //On recupere en premier lieu les zones matchées par les templates afin de les ordonner
            for(int j=0 ; j < templatesImages.size() ; j++)
            {
                if(imagesCount ==MAXIMAGETTE)break;
                
                string templateCourante = op->getFilename(templatesImages[j]);
                stringstream currentNameStream ; currentNameStream <<pageCourante << "-" << templateCourante << "-" << imagesCount;
                string s = currentNameStream.str();s.resize(17, ' ');
//                if(u.VERBOSE)output << "\t template ( " << s << " ) : ";
                
                Mat templ = imread(templatesImages[j]);
                if(ll->findTemplArea(templ, currentNameStream.str(), false))
                {
                    //on enregistre les zones a traiter
                    templateArea curr;
                    curr.image = ll->getTemplArea().clone();;
                    curr.position = ll->getPositionY();
                    curr.name = templateCourante;
                    
                    
                    foundTemplate.push_back(curr);
                    imagesCount++;
                    //on supprime la zone deja matchée...
                    ll->removeZone();
                    //if(VERBOSE)ll->showFinalImage(currentNameStream.str());
                    //on recommence avec le meme template afin de voir s'il existe le meme template à une autre position
                    j--;
                    
                }
//                if(u.VERBOSE) cout << endl ;
                
            }
            
          
            
            int totalImagettes =0;
            if(foundTemplate.size() >0){
//                if(u.VERBOSE) output << "saving... " <<endl;;
                stringstream ss;
                // on reordonne les zones matchées
                sort(foundTemplate.begin(), foundTemplate.end() , compareStruct);
                // pour chaque zone
                for(int ligne=0 ; ligne < foundTemplate.size() ; ligne++)
                {
                    //if(VERBOSE) cout << "recuperation de ligne potentielles... pour  " << foundTemplate[ligne].name<<endl;;
                    
                    //on récupère les lignes étant potentiellement nos imagettes à enregistrer
                    vector<Vec4i> res =  ll->findLines(foundTemplate[ligne].image);
                    
                    //if(VERBOSE) cout << "recuperation des imagettes... " <<endl;;
                    
                    //on récupère les imagettes
                    vector<Mat> result =  ll->findImages(res, foundTemplate[ligne].image);
                    
                    //pour chaque image on enregistre le .jpg + txt avec les informations correspondantes
                    for (int col =0 ; col < result.size(); col++) {
                        op->writeTxtFile(foundTemplate[ligne].name, pageCourante, ligne, col, result[col]);
                        
                    }
                    if(u.VERBOSE){
                        stringstream sgs; sgs <<result.size() << "/5";
                        ss<< "\t "<< ligne+1 <<" : " << niceOutput(sgs.str(), result.size() == 5) ;;
                    }
                    totalImagettes += result.size();
                }
//                if(u.VERBOSE) output << endl;
                
                if(u.VERBOSE){
                    stringstream sgs; sgs <<totalImagettes << "/35";
                    output << " found :  " << imagesCount << "/7 templates => imagettes : "
                    << ss.str() << " \t total : " << niceOutput(sgs.str(), totalImagettes == 35)  << endl;
                }
            }else{
                
                if(u.VERBOSE)cout << niceOutput(" Error, image rejected", false) << endl; //sort templ avec position
                self.nErroImg++;
                
            }
            self.nTotalImg += totalImagettes;
            
        }catch(Exception e){
            output << "error with image : " << pageCourante << endl;;
            output << " " << e.what()<< endl;;
        }
        delete ll;
        
        mtxOutput.lock();
//        if(u.RESULT)cout << "End : " << sourceImage <<endl;
        if(u.VERBOSE )cout << output.str()<<endl;
        cout<< flush;
        mtxOutput.unlock();
    }
    
    
    delete op;

}
Ejemplo n.º 20
0
int Classifier::loadData(string testConf, string pathToSil)
{
     // Open file with testing configuration
    ifstream tcStream(testConf.c_str());
    if (!tcStream.is_open())
    {
        fprintf (stderr, "Could not open %s\n", testConf.c_str());
        return 1;
    }

    string classId;
    string folderName;
    set<string> testImgsNames;
    while (tcStream.good())
    {
        // Skip comments and empty lines
        string line;
        getline(tcStream, line);
        if (!hasData(line)) 
            continue;
        else 
        {
            folderName = line;                  // Get folder name
            classId = getClassId(folderName);   // Get classId

            testImgsNames.clear();
            while (tcStream.good())             // Get names of test images
            {
                getline(tcStream, line);
                if(!hasData(line)) break;
                
                testImgsNames.insert(line);
            }
        }

        // Get pictures from folder
	string dirPath = pathToSil + folderName;
	const vector<string>& filenames = getFilesFromFolder(dirPath);
       
#ifdef WIN32

        dirPath += "\\";
#else
        dirPath += "/";

#endif

        for (int i = 0; i < (int) filenames.size(); i++) 
        {
            // Store image to corresponding set (test or learning)
            string imgName = filenames[i];
            string imgPath = dirPath + imgName;

            Mat imgMat = imread(imgPath, 1);
            if (inSet(imgName, testImgsNames))
            {
                testData[classId].push_back(imgMat);
            }
            else
            {
                learningData[classId].push_back(imgMat);
            }
        }

        if (testData[classId].size() != testImgsNames.size())
            fprintf(stderr, "Warning: Test set for %s is badly defined!\n", classId.c_str());
    }

    // Everything went ok
    return 0;
}
Mat Puppets(){		//this is where the magic happens! Sort of. 



	if(!clmModel._clm._plocal.empty()){		//Just in case there's nothing there...

		Mat localShape;
		Mat newshape;
		Mat globalShape;

		double mouth = 100.0;
		double eyebrows = 100.0;
		double smile = 100.0;                 //weight of expression parameters

		double headmovement = 1.0;

		Mat oldshape;
		clmModel._clm._plocal.copyTo(localShape);
		clmModel._clm._pglobl.copyTo(globalShape);
		globalShape.db(1,0) *= headmovement;
		globalShape.db(2,0) *= headmovement;
		globalShape.db(3,0) *= headmovement;
		clmModel._clm._pdm.CalcShape2D(oldshape, localShape, globalShape);		//calculate old shape
		localShape.db(0,0) *= (mouth/100.0);
		localShape.db(1,0) *= (eyebrows/100.0);
		localShape.db(2,0) *= (smile/100.0);
		clmModel._clm._pdm.CalcShape2D(newshape, localShape, globalShape); //calculate new shape

		ParseToPAW(newshape, localShape, globalShape);
		Mat teethimg;
		cvtColor(faceimg, teethimg, CV_RGB2BGR);
		cv::Mat neutralshape(newshape.rows, 1, CV_64FC1);

		Vec3d orientation;
		orientation(0) = globalShape.at<double>(1);
		orientation(1) = globalShape.at<double>(2);
		orientation(2) = globalShape.at<double>(3);

		int viewid = clmModel._det_valid.GetViewId(orientation);

		bool toggleERI = ERIon;

		if(viewid != 0){
			toggleERI = 1;
		}


		sendOptions(writeToFile,toggleERI, choiceavatar);	//send writeto and usesave to avatar

		clmModel._det_valid._fcheck[viewid]._paw.WarpToNeutral(faceimg, newshape, neutralshape);		//warp to neutral (in CLM/PAW.cc)



		//***************//	
		if(avatarWarpedHead2.empty()){


			string imagefileloc, filelocleft, filelocright;

			if(avatarfile2.empty()){
				avatarfile2 = "../images/shape_central.yml";
			}

			cout << "avatar file " << avatarfile2 << endl;
			FileStorage fs(avatarfile2, FileStorage::READ);	
			fs["shape"] >> avatarS2;
			fs["filename"] >> imagefileloc;
			fs["fileleft"] >> filelocleft;
			fs["fileright"] >> filelocright;
			fs.release();

			avatarWarpedHead2 = imread( imagefileloc);


			cvtColor(avatarWarpedHead2, avatarWarpedHead2, CV_RGB2BGR);

			//warp to neutral on read of a new face, not every loop: so you only have to do it once
			clmModel._det_valid._fcheck[0]._paw.WarpToNeutral(avatarWarpedHead2, avatarS2, neutralshape);

		}
void *affine_loop(void *number)
{

/*
	if(set_single_core_affinity()!=EXIT_SUCCESS)
	{
		perror("Core Affinity");
	}
*/

	int block_size;
	int max_files;
	int i=0;
	int section=0;
	float pos[8]={0,0,1,0,0,1,1,1};

	Point2f srcTri[4];
	Point2f dstTri[4];

	max_files=3601;
	block_size=max_files/4;

	Mat rot_mat( 2, 3, CV_32FC1 );
	Mat warp_mat( 2, 3, CV_32FC1 );


	// Output variables
	Mat src, warp_dst, warp_rotate_dst;

	struct thread_limits *ptr_obj = (struct thread_limits *) number;
	int start_loop = ptr_obj->start_no;
	int stop_loop  = ptr_obj->stop_no;

	/*------------------------- Starting the loop --------------------------*/

	for (i=start_loop; i<=stop_loop; i++)
	{

		/*------------------------- Loading the Image --------------------------*/

		if(option==1)
		{
			// Select the right frame
			sprintf(frame_name2,"Sobel_frame_no_%05u.ppm",i);
			// Load the Image
			src = imread( frame_name2, 1 );
		}

		else
		{
			sprintf(frame_name,"Frame_no_%05u.ppm",i);
			src = imread( frame_name, 1 );
		}

		/*---------------------- Affine Transform : Warp -----------------------*/

		// Setting up the output image parameters

		warp_dst = Mat::zeros( src.rows, src.cols, src.type() );


		/*---------------------- Change the parameter values ----------------------*/

	
	
		switch(section)
		{

			case 0:
			{

				pos[1]=pos[1]+0.001;
				pos[2]=pos[2]-0.001;
				pos[4]=pos[4]+0.001;
				pos[7]=pos[7]-0.001;
		
			
				// Setting parameters for matrix computation

				srcTri[0] = Point2f( 0,0 );
				srcTri[1] = Point2f( src.cols - 1, 0 );
				srcTri[2] = Point2f( 0, src.rows - 1 );
				srcTri[3] = Point2f( src.cols - 1, src.rows - 1 );

				dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] );
				dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] );
				dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] );
				dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] );
			
				section=i/block_size;

				//printf("Case 0: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);

				break;
			}

			case 1:
			{

				pos[0]=pos[0]+0.001;
				pos[3]=pos[3]+0.001;
				pos[5]=pos[5]-0.001;
				pos[6]=pos[6]-0.001;
		
			
				// Setting parameters for matrix computation

				srcTri[0] = Point2f( 0,0 );
				srcTri[1] = Point2f( src.cols - 1, 0 );
				srcTri[2] = Point2f( 0, src.rows - 1 );
				srcTri[3] = Point2f( src.cols - 1, src.rows - 1 );

				dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] );
				dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] );
				dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] );
				dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] );
			
				section=i/block_size;

				//printf("Case 1: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);

				break;
			}
		
			case 2:
			{
			
				pos[1]=pos[1]-0.001;
				pos[2]=pos[2]+0.001;
				pos[4]=pos[4]-0.001;
				pos[7]=pos[7]+0.001;
		
			
				// Setting parameters for matrix computation

				srcTri[0] = Point2f( 0,0 );
				srcTri[1] = Point2f( src.cols - 1, 0 );
				srcTri[2] = Point2f( 0, src.rows - 1 );
				srcTri[3] = Point2f( src.cols - 1, src.rows - 1 );

				dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] );
				dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] );
				dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] );
				dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] );
			
				section=i/block_size;

				//printf("Case 2: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);

				break;
			}
		

			case 3:
			{

				pos[0]=pos[0]-0.001;
				pos[3]=pos[3]-0.001;
				pos[5]=pos[5]+0.001;
				pos[6]=pos[6]+0.001;
		
			
				// Setting parameters for matrix computation

				srcTri[0] = Point2f( 0,0 );
				srcTri[1] = Point2f( src.cols - 1, 0 );
				srcTri[2] = Point2f( 0, src.rows - 1 );
				srcTri[3] = Point2f( src.cols - 1, src.rows - 1 );

				dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] );
				dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] );
				dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] );
				dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] );

			
				section=i/block_size;

				//printf("Case 3: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);

				break;
			}

			default:
			{
				//printf("Value: %d\n",section);
				//perror("Default switch() case");
				break;
			}
		}
		



		// Calculate the Affine Transform matrix

		warp_mat = getAffineTransform( srcTri, dstTri );


		// Applying the Affine Transform to the src image

		warpAffine( src, warp_dst, warp_mat, warp_dst.size() );



		/*-------------------- Affine Transform : Rotate -----------------------*/

		// Compute the Rotation Matrix Parameters

		Point center = Point( warp_dst.cols/2, warp_dst.rows/2 );
		double angle = ROTATION_ANGLE;
		double scale = ISOTROPIC_SCALE_FACTOR;

		// Generate the Rotation Matrix

		rot_mat = getRotationMatrix2D( center, angle, scale );

		// Rotate the Image

		warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() );


		/*------------------------- Storing the Image ---------------------------*/


		sprintf(frame_name3,"Affine_frame_no_%05u.ppm",i);

		// Storing the Image

		imwrite(frame_name3, warp_dst);

	}
	// End of 'for' loop

	return NULL;
}
Ejemplo n.º 23
0
int main(int argc, char** argv)
{
long totaltime, intTime, intTime_o, colTime, colTime_o , orTime, orTime_o;
if(argc != 2)
{
	cout << "No image"<<endl;
	return -1;
}
cout<<"Loading Image: ";
cout<< argv[1]<<endl;
Mat inputImage = imread(argv[1], CV_LOAD_IMAGE_COLOR);

if(!inputImage.data)
{
	cout <<"Invalid Image"<<endl;
}

Mat IntensityImg, finalImage;
for(int counter = 0; counter < 1; counter++)
{
totaltime = timestamp();

intTime = timestamp();
IntensityImg = Get_Intensity_Image(inputImage);
vector<Mat> Intensity_Maps = Pyr_CenSur(IntensityImg);
Mat AggInt = aggregateMaps(Intensity_Maps);
normalize(AggInt, AggInt, 0, 255, NORM_MINMAX, -1);
intTime_o = timestamp() - intTime;

colTime = timestamp();
vector<Mat> color_map;
color_map = Normalize_color(inputImage, IntensityImg);
vector<Mat> RGBYMap(6); 
for(int i = 0; i<6; i++)
addWeighted(color_map[i], 0.5, color_map[i+6], 0.5, 0, RGBYMap[i], -1);
Mat AggColor = aggregateMaps(RGBYMap);
normalize(AggColor, AggColor, 0, 255, NORM_MINMAX, -1);
colTime_o = timestamp() - colTime;

orTime = timestamp();
Mat AggOr;
AggOr = getGaborImage(IntensityImg);
normalize(AggOr, AggOr, 0, 255, NORM_MINMAX, -1);
orTime_o = timestamp() - orTime;

finalImage = (AggInt + AggColor + AggOr) /3;
normalize(finalImage, finalImage, 0, 255, NORM_MINMAX, -1);

for(int bCtr = 0; bCtr<4; bCtr++)
{
	pyrUp(finalImage, finalImage);
}

cout <<"Intensity Time: "<< (intTime_o) << "\n";
cout <<"Color Time: "<< (colTime_o) << "\n";
cout <<"Orientation Time: "<< (orTime_o) << "\n";
cout <<"Total Time: "<< (timestamp() - totaltime) << "\n";
}



Mat contImg;
inRange(finalImage, 160, 230, contImg);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours(contImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for(int i = 0; i>=0; i =hierarchy[i][0])
{
	Scalar color(rand()&255, rand()&255, rand()&255);
	drawContours(inputImage, contours, i, color, 3, 8, hierarchy);
}

imwrite("Salient_Image.jpg" , inputImage);

waitKey(0);
return 0;
}
void *sobel_loop(void *number)
{

/*	if(set_single_core_affinity()!=EXIT_SUCCESS)
	{
		perror("Core Affinity");
	}
*/

	int i=0;
	Mat sobel_src, src_gray, s_src;
	Mat grad;

	struct thread_limits *ptr_obj = (struct thread_limits *) number;
	int start_loop = ptr_obj->start_no;
	int stop_loop  = ptr_obj->stop_no;

	for (i=start_loop; i<=stop_loop; i++)
	{

		/*------------------------- Loading the Image --------------------------*/


		// Select the right frame

		sprintf(frame_name,"Frame_no_%05u.ppm",i);

		// Load the Image

		sobel_src = imread( frame_name,1 );  

		if( !sobel_src.data )
		{
			perror("Reading from file"); 
		}


		// Apply Gaussian Blur function

		GaussianBlur( sobel_src, s_src, Size(3,3), 0, 0, BORDER_DEFAULT );

		// Convert Color image to gray
		//printf("cvtcolor\n"); 

		cvtColor( s_src, src_gray, CV_RGB2GRAY );
		//printf("after cvtcolor\n"); 
		// Generate x and y matrix

		Mat sobel_x, sobel_y;
		Mat sobel_abs_x, sobel_abs_y;

		// Gradient X

		Sobel( src_gray, sobel_x, SOBEL_DEPTH, 1, 0, 3, SOBEL_SCALE, SOBEL_DELTA, BORDER_DEFAULT );
		convertScaleAbs( sobel_x, sobel_abs_x );

		// Gradient Y

		Sobel( src_gray, sobel_y, SOBEL_DEPTH, 0, 1, 3, SOBEL_SCALE, SOBEL_DELTA, BORDER_DEFAULT );
		convertScaleAbs( sobel_y, sobel_abs_y );

		// Total Gradient
		addWeighted( sobel_abs_x, 0.5, sobel_abs_y, 0.5, 0, grad );

		/*------------------------- Storing the Image ---------------------------*/

		// Storing the Image

		sprintf(frame_name2,"Sobel_frame_no_%05u.ppm",i);

		imwrite(frame_name2, grad);

	}
	// End of 'for' loop

	return (NULL);
}
Ejemplo n.º 25
0
// extract patches from database images
bool ObjPatchMatcher::PreparePatchDB(DatasetName db_name) {

	patch_meta.objects.clear();
	cout<<patch_meta.objects.max_size()<<endl;

	DataManagerInterface* db_man = NULL;
	if(db_name == DB_NYU2_RGBD)
		db_man = new NYUDepth2DataMan;
	if(db_name == DB_SALIENCY_RGBD)
		db_man = new RGBDECCV14;

	srand(time(0));
	FileInfos imgfns, dmapfns;
	db_man->GetImageList(imgfns);
	random_shuffle(imgfns.begin(), imgfns.end());
	imgfns.erase(imgfns.begin()+30, imgfns.end());
	db_man->GetDepthmapList(imgfns, dmapfns);
	map<string, vector<VisualObject>> gt_masks;
	db_man->LoadGTMasks(imgfns, gt_masks);

	int gt_obj_cnt = 0;
	gt_obj_masks.clear();

	for(size_t i=0; i<imgfns.size(); i++) {
		// color
		Mat cimg = imread(imgfns[i].filepath);
		Size newsz(400, 400);
		//tools::ToolFactory::compute_downsample_ratio(Size(cimg.cols, cimg.rows), 400, newsz);
		resize(cimg, cimg, newsz);
		// depth
		Mat dmap_float;
		db_man->LoadDepthData(dmapfns[i].filepath, dmap_float);
		resize(dmap_float, dmap_float, newsz);
		dmap_float.convertTo(dmap_float, CV_32F, 1000);
		Mat cmp_mask;
		compare(dmap_float, 800, cmp_mask, CMP_LT);
		dmap_float.setTo(800, cmp_mask);
		compare(dmap_float, 7000, cmp_mask, CMP_GT);
		dmap_float.setTo(7000, cmp_mask);
		dmap_float = (dmap_float-800)/(7000-800);
		
		// get label image
		Mat lable_mask = Mat::zeros(newsz.height, newsz.width, CV_8U);
		Mat label_id_mask = Mat::zeros(newsz.height, newsz.width, CV_32S)-1;
		vector<VisualObject>& gt_objs = gt_masks[imgfns[i].filename];
		for(auto& cur_gt : gt_objs) {
			//if( !valid_cls[cur_gt.category_id] ) continue;
			resize(cur_gt.visual_data.mask, cur_gt.visual_data.mask, newsz);
			label_id_mask.setTo(gt_obj_cnt++, cur_gt.visual_data.mask);
			gt_obj_masks.push_back(cur_gt.visual_data.mask);
			lable_mask.setTo(1, cur_gt.visual_data.mask);
		}

		imshow("color", cimg);
		ImgVisualizer::DrawFloatImg("depth", dmap_float);
		imshow("label", lable_mask*255);
		waitKey(10);

		// do edge detection to locate boundary point quickly
		Mat gray_img, edge_map, gray_img_float;
		cvtColor(cimg, gray_img, CV_BGR2GRAY);
		gray_img.convertTo(gray_img_float, CV_32F, 1.f/255);
		Canny(gray_img, edge_map, 10, 50);
		Mat grad_x, grad_y, grad_mag;
		Sobel(gray_img_float, grad_x, CV_32F, 1, 0);
		Sobel(gray_img_float, grad_y, CV_32F, 0, 1);
		magnitude(grad_x, grad_y, grad_mag);
		Mat pts3d, normal_map;
		if(use_depth) {
			Feature3D feat3d;
			feat3d.ComputeKinect3DMap(dmap_float, pts3d, false);
			feat3d.ComputeNormalMap(pts3d, normal_map);
		}

		// selected patch indicator, avoid very close duplicate patches
		Mat picked_patch_mask = Mat::zeros(lable_mask.rows, lable_mask.cols, CV_8U);

		// extract patches
		for(int r=patch_size.height/2; r<edge_map.rows-patch_size.height/2; r+=3) {
			for(int c=patch_size.width/2; c<edge_map.cols-patch_size.width/2; c+=3) {

				if( edge_map.at<uchar>(r,c) == 0 ) continue;

				// only use perfect boundary points
				Point center_pt(c, r), left_pt(c-1, r), right_pt(c+1, r), top_pt(c, r-1), bottom_pt(c, r+1);
				if(lable_mask.at<uchar>(center_pt) == lable_mask.at<uchar>(left_pt) && 
					lable_mask.at<uchar>(center_pt) == lable_mask.at<uchar>(right_pt) &&
					lable_mask.at<uchar>(center_pt) == lable_mask.at<uchar>(top_pt) &&
					lable_mask.at<uchar>(center_pt) == lable_mask.at<uchar>(bottom_pt) );
					//continue;

				// set picked
				picked_patch_mask.at<uchar>(center_pt) = 1;

				VisualObject cur_patch;
				cur_patch.meta_data.img_path = imgfns[i].filepath;
				Rect box(c-patch_size.width/2, r-patch_size.height/2, patch_size.width, patch_size.height);
				cur_patch.visual_data.bbox = box;

				// find which object is dominant
				map<int, int> obj_label_cnt;
				int max_num = 0;
				cur_patch.meta_data.category_id = -1;
				for(int rr=box.y; rr<box.br().y; rr++) for(int cc=box.x; cc<box.br().x; cc++) {
					int cur_id = label_id_mask.at<int>(rr,cc);
					if(cur_id != -1)
					{
						obj_label_cnt[cur_id]++;
						if(obj_label_cnt[cur_id] > max_num) 
						{ 
							max_num = obj_label_cnt[cur_id];
							cur_patch.meta_data.category_id= cur_id;
						}
					}
				}
				/*vector<ImgWin> boxes; boxes.push_back(box);
				ImgVisualizer::DrawWinsOnImg("", cimg, boxes);
				if(cur_patch.category_id != -1) {
				imshow("mask", gt_obj_masks[cur_patch.category_id]*255);
				waitKey(0);
				}*/
				

				lable_mask(box).convertTo(cur_patch.visual_data.mask, CV_32F);
				// extract feature vector
				gray_img_float(box).copyTo( cur_patch.visual_data.custom_feats["gray"] );
				//grad_mag(box).copyTo( cur_patch.visual_desc.extra_features["gradient"] );
				if(use_depth) {
					normal_map(box).copyTo( cur_patch.visual_data.custom_feats["normal"] );
					dmap_float(box).copyTo( cur_patch.visual_data.custom_feats["depth"] );
					/*ImgVisualizer::DrawFloatImg("depthmask", cur_patch.visual_desc.extra_features["depth"]);
					cout<<"new box"<<endl;
					waitKey(0);*/
				}
				Mat feat;
				ComputePatchFeat(cur_patch.visual_data.custom_feats, feat);
				patch_data.push_back(feat);
				patch_meta.objects.push_back(cur_patch);
			}
				
				//}
			//}
		}
		
		cout<<"finished image: "<<i<<"/"<<imgfns.size()<<endl;
		cout<<"db size: "<<patch_data.rows<<endl;
	}

	cout<<"total patch number: "<<patch_data.rows<<endl;

	if(use_code) {
		// compress to codes
		LSHCoder lsh_coder;
		if( !lsh_coder.LearnOptimalCodes(patch_data, 64, patch_keys) ) {
			return false;
		}
	}

	return true;
}
Ejemplo n.º 26
0
Mat imcvLoadImages(char *filename, uint8 opentype)
{
	return (imread(filename,opentype));
} 
Ejemplo n.º 27
0
// CMYTEST2Doc 命令
BOOL CMYTEST2Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	if(GetFileExt(lpszPathName).CompareNoCase(_T("")) == 0)	// 打开数据文件
	{
        CDlgRawData Dlg;
		if(Dlg.DoModal()==IDOK)
		{
			
			switch((ImageType)Dlg.m_ImageType)
	{
	case IMAGE_TYPE_8BITS:
		m_srcType = CV_8UC1;
		break;
	case IMAGE_TYPE_16BITS:
		m_srcType = CV_16UC1;
		break;
	default:
		break;
	}
	m_srcWidth=Dlg.m_width;
	m_srcHeight=Dlg.m_height;
	LONGLONG m_nGap=Dlg.m_nGap;
	LONGLONG m_nOffset=Dlg.m_nOffset;
	BOOL LittleEndian=Dlg.LittleEndian;
	bWhiteZero=Dlg.WhiteZero;
    m_nMaxCurrentFrm=Dlg.m_nFrame;
	

		CFile pFile;
		if(pFile.Open(lpszPathName, CFile::modeRead |CFile::typeBinary)==FALSE)
		{

			MessageBox(NULL, TEXT("Wrong file!"), TEXT("Open"), MB_OK);
			return  FALSE;
		}

		if (m_srcRGB.empty()==false)	// 如果img已分配空间
		{
			if (m_srcRGB.cols!=m_srcWidth || m_srcRGB.rows!=m_srcHeight || m_srcRGB.type()!=m_srcType)
			{
				m_srcRGB.release();	// 如果宽高或类型不相符,删除重新分配
				m_srcRGB.create(m_srcHeight, m_srcWidth, m_srcType); // 创建图像缓存
			}
		}
		else
			m_srcRGB.create(m_srcHeight, m_srcWidth, m_srcType); // 创建缓存


		// 图像像素数
		UINT nPixel = m_srcHeight*m_srcWidth;
		UINT i, j;
		
			// 指向帧数据位置
   LONGLONG pos = m_nOffset+m_nCurrentFrm*(m_nGap+nPixel*sizeof(ushort)); // 首帧前的偏移+当前帧数*(帧间隔区+每帧大小)
	LONGLONG sizebuff = pFile.Seek(pos, CFile::begin);//文件移动到特定的偏移量
	
		ushort *pSource = new ushort[nPixel];
		pFile.Read(pSource,nPixel*sizeof(ushort));  

		// 医学图像大部分在0——4096之间
		m_srcMaxValue = 4096;
		m_srcMinValue = 0;
	

		// 将数据导入Mat中
		for (i=0; i<m_srcHeight; i++)  
		{  
			ushort *ps = m_srcRGB.ptr<ushort>(i);
			for (j=0 ; j<m_srcWidth; j++)  
			{  
				ps[j] = ushort(pSource[i*m_srcWidth+j]);
			}   
		} 

		delete []pSource;
		pFile.Close();
	
		if (m_srcRGB.empty())
		{
			MessageBox(NULL, TEXT("装载图像文件失败!"), TEXT("Open"), MB_OK);
			return FALSE;
		}
	Mat m_dstRGB=m_srcRGB;
		Dst=m_srcRGB;
		//保存至CImage中供显示
		m_pPic.Destroy();
		Mat16bToCImage(m_srcMaxValue, m_srcMinValue,m_dstRGB,bWhiteZero);	// 专用于16bit数据文件
	}	
	}
	else	// 打开普通图像
	{
		string pathname = CString2StdString(lpszPathName);
		m_srcRGB = imread(pathname);

		if (m_srcRGB.empty())
		{
			MessageBox(NULL, TEXT("装载图像文件失败!"), TEXT("Open"), MB_OK);
			return FALSE;
		}
		//保存至CImage中供显示
		m_pPic.Destroy();
		Mat8bToCImage();	// 用于8bit普通图像
	}


	return TRUE;
}
Ejemplo n.º 28
0
int main(int argc, char **argv) 
{
    char c;
    char *filepath;
    int cut_horizontal = 0;
    int cut_vertical = 0;
    int timed = 0;
    int show = 0;

    while ((c = getopt(argc, argv, "f:h:v:ts")) != -1) {
        switch (c) {
        case 'f': filepath = optarg; break;
        case 'h': cut_horizontal = (int)strtol(optarg, NULL, 10); break;
        case 'v': cut_vertical = (int)strtol(optarg, NULL, 10); break;
        case 't': timed = 1; break;
        case 's': show = 1; break;
        default: exit(1); 
        }
    }

    // OpenCL boilerplate
    std::string ..._kernel_str;

    std::string ..._name_str = std::string("...");
    std::string ..._kernel_file = std::string("...");

    cl_vars_t cv; 
    cl_kernel ...;

    readFile(..._kernel_file, ..._kernel_str);

    initialize_ocl(cv);

    compile_ocl_program(..., cv, ..._kernel_str.c_str(), ..._name_str.c_str());

    // Read image
    Mat_<Vec3b> image = imread(filepath);

    if (!image.data) {
        cout << "Invalid input";
        image.release();
        return -1;
    }

    if (show) {
        imshow("Original Image", image);
    }

    SeamCarver s(image);

    // imshow("Gradient", s.energy);
    // Mat tmp = s.energy/195075.0*255.0;
    // s.energy.convertTo(tmp,CV_8U,-1);
    // imwrite("bench_gradient.jpg", tmp);
    // vector<uint> sm = s.findVerticalSeam();
    // s.showVerticalSeam(sm);


    // Carving happens here
    double start = get_time();
    ...;
    double elapsed = get_time() - start;
    // --------------------

    // double start = get_time();
    // for (int i = 0; i < cut_horizontal; ++i) {
    //     vector<uint> seam = s.findHorizontalSeam();
    //     // s.showHorizontalSeam(seam);
    //     s.removeHorizontalSeam(seam);
    // }
    // for (int i = 0; i < cut_vertical; ++i) {
    //     vector<uint> seam = s.findVerticalSeam();
    //     // s.showVerticalSeam(seam);
    //     s.removeVerticalSeam(seam);
    // }
    // double elapsed = get_time() - start;

    if (timed) {
        printf("Elapsed time: %.3lf seconds\n", elapsed);
    }

    Mat_<Vec3b> output = s.getImage();
    imwrite("scarved.jpg", output);

    if (show) {
        imshow("Carved Image", output);
        while (waitKey(20) != 27);
    }

    // cout << "Seam Length: " << seam.size() << endl;
    // s.showImage();
    // s.showEnergy();

    // imwrite("bench_carved.jpg", s.getImage());

    // for (int i = 0; i < 5; ++i) {
    //     for (int j = 0; j < 5; ++j) {
    //         cout << s.energy.at<uint32_t>(i,j) << " ";
    //     }
    //     cout << endl;
    // }

    uninitialize_ocl(cv);

    ...;

    clReleaseMemObject(...); 

    image.release();

    return 0;
}
Ejemplo n.º 29
0
void ObjectTester::TestObjectRanking(const DatasetName& dbname)
{
	// load dataset
	FileInfos img_fns;
	FileInfos dmap_fns;
	map<string, vector<ImgWin>> rawgtwins;

	NYUDepth2DataMan nyudata;
	Berkeley3DDataManager berkeleydata;

	if(dbname == DB_NYU2_RGBD)
	{
		nyudata.GetImageList(img_fns);
		nyudata.GetDepthmapList(dmap_fns);
		if(img_fns.size() != dmap_fns.size())
			return;
		nyudata.LoadGTWins(img_fns, rawgtwins);
	}
	if(dbname == DB_BERKELEY3D)
	{
		berkeleydata.GetImageList(img_fns);
		berkeleydata.GetDepthmapList(dmap_fns);
		if(img_fns.size() != dmap_fns.size())
			return;
		berkeleydata.LoadGTWins(img_fns, rawgtwins);
	}

	GenericObjectDetector detector;
	if( !detector.InitBingObjectness() )
		return;

	SalientRegionDetector saldet;
	SalientRGBDRegionDetector salrgbd;
	DepthSaliency depth_sal;
	vector<vector<ImgWin>> objdetwins(img_fns.size()), saldetwins(img_fns.size()), depthdetwins;
	vector<vector<ImgWin>> gtwins(img_fns.size());

#pragma omp parallel for
	for (int i=0; i<img_fns.size(); i++)
	{
		// read image
		Mat curimg = imread(img_fns[i].filepath);
		if(curimg.empty())
			continue;

		// read depth
		Mat curdmap;
		if(dbname == DB_NYU2_RGBD)
			if( !nyudata.LoadDepthData(dmap_fns[i].filepath, curdmap) )
				continue;
		if(dbname == DB_BERKELEY3D)
			if( !berkeleydata.LoadDepthData(dmap_fns[i].filepath, curdmap) )
				continue;

//#define VERBOSE
#ifdef VERBOSE
		// show gt
		visualsearch::ImgVisualizer::DrawImgWins("gt", curimg, rawgtwins[img_fns[i].filename]);
		visualsearch::ImgVisualizer::DrawFloatImg("dmap", curdmap, Mat());
#endif

		// normalize to image
		//normalize(curdmap, curdmap, 0, 255, NORM_MINMAX);
		//curdmap.convertTo(curdmap, CV_8U);
		//cvtColor(curdmap, curdmap, CV_GRAY2BGR);
		//imshow("depthimg", curdmap);
		//waitKey(10);

		//visualsearch::ImgVisualizer::DrawImgWins("b3d", curimg, rawgtwins[img_fns[i].filename]);
		//waitKey(0);

		// resize
		Size newsz;
		//ToolFactory::compute_downsample_ratio(Size(curimg.cols, curimg.rows), 300, newsz);
		//resize(curimg, curimg, newsz);

		double start_t = getTickCount();
		// get objectness windows
		vector<ImgWin> objboxes;
		detector.GetObjectsFromBing(curimg, objboxes, 1000);
		//visualsearch::ImgVisualizer::DrawImgWins("objectness", curimg, objboxes);
		cout<<"objectness: "<<(double)(getTickCount()-start_t) / getTickFrequency()<<endl;
		
		start_t = getTickCount();
		// rank
		normalize(curdmap, curdmap, 0, 255, NORM_MINMAX);
		vector<ImgWin> salboxes = objboxes;
		int saltype = SAL_COLOR | SAL_DEPTH;
		salrgbd.Init(saltype, curimg, curdmap);
		salrgbd.RankWins(salboxes);
		//depth_sal.RankWins(curdmap, salboxes);
		cout<<"Depth ranking: "<<(double)(getTickCount()-start_t) / getTickFrequency()<<endl;

#ifdef VERBOSE
		vector<Mat> imgs(50);
		for (int i=0; i<50; i++)
		{
			imgs[i] = curimg(salboxes[i]);
		}
		Mat dispimg;
		visualsearch::ImgVisualizer::DrawImgCollection("objectness", imgs, 50, 15, dispimg);
		imshow("objectness", dispimg);
		visualsearch::ImgVisualizer::DrawImgWins("saldet", curimg, salboxes);
		waitKey(0);
#endif
		/*saldet.g_para.segThresholdK = 200;
		saldet.Init(curdmap);
		saldet.RankWins(salboxes);*/
		//visualsearch::ImgVisualizer::DrawImgWins("sal", curimg, salboxes);
		//waitKey(0);

		// add to collection
		objdetwins[i] = objboxes;
		saldetwins[i] = salboxes;
		gtwins[i] = rawgtwins[img_fns[i].filename];
		
		cout<<"Finish detection on "<<i<<"/"<<img_fns.size()<<endl;
	}

	// evaluation
	WindowEvaluator eval;
	vector<Point2f> objprvals, salprvals, depthprvals;
	int topnum[] = {1, 5, 10, 50, 100, 200, 500, 800, 1000};
	for(int i=0; i<9; i++)
	{
		Point2f curpr = eval.ComputePR(objdetwins, gtwins, topnum[i]);
		objprvals.push_back(curpr);
		curpr = eval.ComputePR(saldetwins, gtwins, topnum[i]);
		salprvals.push_back(curpr);
	}
	
	// save to file
	ofstream out1("nyu_objpr.txt");
	for (size_t i=0; i<objprvals.size(); i++) out1<<objprvals[i].x<<" "<<objprvals[i].y<<endl;
	ofstream out2("nyu_rgbdpr.txt");
	for (size_t i=0; i<salprvals.size(); i++) out2<<salprvals[i].x<<" "<<salprvals[i].y<<endl;

	cout<<"Finish evaluation"<<endl;

}
int _tmain(int argc, _TCHAR* argv[])
{
// 	initModule_nonfree();//if use SIFT or SURF  
// 	Ptr<FeatureDetector> detector = FeatureDetector::create( "SIFT" );  
// 	Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SIFT" );  
// 	Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );  
// 	if( detector.empty() || descriptor_extractor.empty() )  
// 		throw runtime_error("fail to create detector!");  
// 
// 	Mat img1 = imread("E:\\DayBreakZcs\\ImageProcessing\\source\\test3.jpg");  
// 	Mat img2 = imread("E:\\DayBreakZcs\\ImageProcessing\\source\\test2.jpg");  
// 
// 	//detect keypoints;  
// 	vector<KeyPoint> keypoints1,keypoints2;  
// 	detector->detect( img1, keypoints1 );  
// 	detector->detect( img2, keypoints2 );  
// 	cout <<"img1:"<< keypoints1.size() << " points  img2:" <<keypoints2.size()   
// 		<< " points" << endl << ">" << endl;  
// 
// 	//compute descriptors for keypoints;  
// 	cout << "< Computing descriptors for keypoints from images..." << endl;  
// 	Mat descriptors1,descriptors2;  
// 	descriptor_extractor->compute( img1, keypoints1, descriptors1 );  
// 	descriptor_extractor->compute( img2, keypoints2, descriptors2 );  
// 
// 	cout<<endl<<"Descriptors Size: "<<descriptors2.size()<<" >"<<endl;  
// 	cout<<endl<<"Descriptor's Column: "<<descriptors2.cols<<endl  
// 		<<"Descriptor's Row: "<<descriptors2.rows<<endl;  
// 	cout << ">" << endl;  
// 
// 	//Draw And Match img1,img2 keypoints  
// 	Mat img_keypoints1,img_keypoints2;  
// 	drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);  
// 	drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);  
// 	imshow("Box_in_scene keyPoints",img_keypoints1);  
// 	imshow("Box keyPoints",img_keypoints2);  
// 
// 	descriptor_extractor->compute( img1, keypoints1, descriptors1 );    
// 	vector<DMatch> matches;  
// 	descriptor_matcher->match( descriptors1, descriptors2, matches );  
// 
// 	Mat img_matches;  
// 	drawMatches(img1,keypoints1,img2,keypoints2,matches,img_matches,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);  
// 
// 	imshow("Mathc",img_matches);  
// 	waitKey(0);  
// 	return 0;  
// 	return 0;
	Mat image = imread("E:\\DayBreakZcs\\ImageProcessing\\source\\test4.jpg");  
	Mat descriptors;  
	vector<KeyPoint> keypoints;  
	SimpleBlobDetector::Params params;  
	params.minThreshold = 10;  
	params.maxThreshold = 100;  
	params.thresholdStep = 10;  
	params.minArea = 10;   
	params.minConvexity = 0.3;  
	params.minInertiaRatio = 0.01;  
	params.maxArea = 8000;  
	params.maxConvexity = 10;  
	params.filterByColor = false;  
	params.filterByCircularity = false;  
	SimpleBlobDetector blobDetector( params );  
	blobDetector.create("SimpleBlob");  
	blobDetector.detect( image, keypoints );  
	drawKeypoints(image, keypoints, image, Scalar(255,0,0));  
	imshow("Mathc",image);  
	waitKey(0); 
}