void FastShift::initial() { r = data.rows; n = data.cols; partitionGrid(); selectD = 2; selected.resize(selectD, 0); factor.resize(n, 1); for(int i = n - 2; i >= 0; i --) factor[i] *= grids[i+1]; gridNum = factor[0] * grids[0]; D = new int[gridNum]; C = new float*[gridNum]; for(int i = 0; i < gridNum; i ++) C[i] = new float[n]; vector<int> grid_pos(n, 0); for(int i = 0; i < n; i ++) { whichGrid(data.row(i), grid_pos); int pos = findposition(grid_pos); D[pos] ++; add(C[pos], (float*)(data.row(i).data), n); } for(int i = 0; i < gridNum; i ++) { for(int j = 0; j < n; j ++) { C[i][j] /= D[i]; } } }
vector<float> FastShift::allDirections(vector<float> shiftPoint, vector<float> shiftValue) { vector<int> grid_pos(n, 0); whichGrid(shiftPoint, grid_pos); int pos = findposition(grid_pos); vector<float> external_force(n, 0); vector<float> force1(n, 0); for(int i = 0; i < selectD; i ++) { for(int j = -1; j <= 1; j += 2) { calForce(shiftPoint, pos + j * factor[selected[i]], force1); external_force += force1; } } return external_force; }
vector<float> FastShift::maxDirectionTwoSide(vector<float> shiftPoint, vector<float> shiftValue) { vector<float> external_force(n, 0); vector<float> force(n, 0); vector<float> force1(n, 0); bool unset = true; float max; int shift; vector<int> grid_pos(n, 0); whichGrid(shiftPoint, grid_pos); int pos = findposition(grid_pos); for(int i = 0; i < selectD; i ++) { for(int j = -1; j <= 1; j += 2) { calForce(shiftPoint, pos + j * factor[selected[i]], force1); force += force1; } float l = l2norm(force); if(unset) { max = l; external_force = force; shift = pos - factor[selected[i]]; unset = false; continue; } if(l > max) { max = l; external_force = force; } } //copy(C[shift], shiftValue, n); return external_force; }
int main(void) { std::vector<long> vec; std::deque<long> deq; std::list<long> list; std::cout << "Container being filled with even numbers 2 <= x <= 64:" << std::endl; fill(vec); fill(deq); fill(list); print(vec); print(deq); print(list); std::vector<long>::iterator vecIter = vec.begin(); std::deque<long>::iterator deqIter = deq.begin(); std::list<long>::iterator listIter = list.begin(); std::cout << "Iter moved to the third element:" << std::endl; vecIter += 2; deqIter += 2; //list doesn't have a random access modifier, so we need to move the iter one by one ++++listIter; std::cout << "vector[2] = " << *vecIter << std::endl; std::cout << "deque[2] = " << *deqIter << std::endl; std::cout << "list[2] = " << *listIter << std::endl; std::vector<long>::iterator vecBefore, vecPos; std::deque<long>::iterator deqBefore, deqPos; std::list<long>::iterator listBefore, listPos; std::cout << "Inserting 31 into the container:" << std::endl; findposition(vec, 31, vecBefore, vecPos); findposition(deq, 31, deqBefore, deqPos); findposition(list, 31, listBefore, listPos); // fix as described in the report #ifndef POSFIX vec.insert(vecPos, 31); deq.insert(deqPos, 31); #else // the resulting iterator from insert is valid because it's "from AFTER the insert" vecBefore = vec.insert(vecPos, 31); deqBefore = deq.insert(deqPos, 31); // the result of insert points to the inserted element // before we can safely move it to the element before we need to check if it isn't // already the first if (vecBefore != vec.begin()) --vecBefore; if (deqBefore != deq.begin()) --deqBefore; #endif // list is unaffacted list.insert(listPos, 31); printnext3(vec, vecBefore); printnext3(deq, deqBefore); printnext3(list, listBefore); std::cout << "Containers with 31 inserted:" << std::endl; print(vec); print(deq); print(list); deleteodd(vec); deleteodd(deq); deleteodd(list); std::cout << "Containers with all odd Numbers removed:" << std::endl; print(vec); print(deq); print(list); return 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"); }