Ejemplo n.º 1
0
 static inline
 std::vector<int>
 AllDirections()
 {
     std::vector<int> allDirections(DIM);
     for (int i = 0; i < DIM; ++i) {
         allDirections.emplace_back(i);
     }
     return allDirections;
 }
Ejemplo n.º 2
0
FastShift::FastShift(const Mat& feature, Mat& convexPoints)
{
	printf("start to meanshift point...\n");
	
	time_t startTime = time(0);
	data = feature;
	initial();
	printf("start to compute the convex point for every point ...\n");
	convexPoints.create(r, n, CV_32FC1);

	srand(time(0));
	vector<float> shiftPoint(n, 0);
	vector<float> shiftValue(n, 0);
	double w1, w2;
	vector<float> total_force, current_force, external_force;
	vector<int> grid_pos;
	int maxIterations = 50;

	
	for(int counts = 0; counts < r; counts ++)
	{
		printf("the %d-th iterations ...\n", counts);
		int iterations = maxIterations;
		copy((float*)data.row(counts).data, shiftPoint, n);
		while(iterations > 0)
		{
			iterations --;
			//choose the best direction
			radomSelect();
			whichGrid(shiftPoint, grid_pos);
			int pos = findposition(grid_pos);
			//so many cases to get the external forces
			#ifdef one_direction
				external_force = maxDirectionOneSide(shiftPoint, shiftValue);
			#else 
				#ifdef composite_line
					external_force = maxDirectionTwoSide(shiftPoint, shiftValue);
				#else 
					external_force = allDirections(shiftPoint, shiftValue);
				#endif
			#endif
			
			//shift the feature vector
			calForce(shiftPoint, pos, current_force);
			total_force = current_force + external_force;
			
			if(l2norm(external_force) < acceptance)
				continue;
			if(l2norm(total_force) < stopCond)
				break;
			shiftPoint = shiftPoint + step * total_force;
			/*w1 = l2norm(current_force)/l2norm(total_force);
			w2 = external_force/total_force;
			shiftPoint = w1 * shiftPoint + w2 * shiftValue;
			if(w2 < epsilon)
				break;*/
			
		}
		copy(shiftPoint, (float*)convexPoints.row(counts).data, n);
		//iters.push_back(counts);
		
	}
	string writer = "./output/convex.dat";
	MatrixDataIo mdi(writer.c_str(), true, convexPoints);
	
	printUsedTime(startTime);
	programPause();
	printf("exit meanshift clustering...\n");
}