1. 程式人生 > >Dlib提取Hog特徵

Dlib提取Hog特徵

效果如圖…


關鍵點:

1.設定cell大小

2.其他的看程式碼

3.Dlib配置見: 地址

程式碼:


#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>


using namespace std;
using namespace dlib;

//  ----------------------------------------------------------------------------

int main()
{
	try
	{	
		array2d<rgb_pixel> img;
		//只能是BMP,如果想要JPG之類的,需要編譯Dlib讓其支援…
		load_image(img,"C:\\Users\\zhumingde\\Pictures\\timg.bmp");
		//400*362
			
		//單個hog特徵是31維的…
		array2d<matrix<float, 31, 1> > hog;
		
		extract_fhog_features(img, hog,16);//預設為8
		//左右上下留白麼233
		//如果不整除,進1…
		//也就是說如果cell為100,,,最終的hog為2*2
		//提取出的hog特徵維度為  (cols/cell-2)*(rows/cell-2)

		cout << "hog image has " << hog.nr() << " rows and " << hog.nc() << " columns." << endl;

		//看看Hog特徵
		image_window win(img);
		image_window winhog(draw_fhog(hog));

		
		//點選原圖可以找出對應的hog特徵
		// Another thing you might want to do is map between the pixels in img and the
		// cells in the hog image.  dlib provides the image_to_fhog() and fhog_to_image()
		// routines for this.  Their use is demonstrated in the following loop which
		// responds to the user clicking on pixels in the image img.
		point p;  // A 2D point, used to represent pixel locations.
		while (win.get_next_double_click(p))
		{
			point hp = image_to_fhog(p);
			cout << "The point " << p << " in the input image corresponds to " << hp << " in hog space." << endl;
			cout << "FHOG features at this point: " << trans(hog[hp.y()][hp.x()]) << endl;
		}

		//另一種格式…
		dlib::array<array2d<float> > planar_hog;
		extract_fhog_features(img, planar_hog);

		system("pause");
	}
	catch (exception& e)
	{
		cout << "exception thrown: " << e.what() << endl;
		system("pause");
	}
}

//  ----------------------------------------------------------------------------