void ColorSeparation(const Mat targetImg, vector<StrokeCluster> &fisrtDrawCluster){ // Mean shifting Mat colorSegment = Mat(targetImg.size(), CV_8UC3, Scalar(255, 255, 255)); IplImage* img = cvCloneImage(&(IplImage)targetImg); int **ilabels = new int *[img->height]; for (int i = 0; i < img->height; i++) ilabels[i] = new int[img->width]; int regionNum = MeanShift(img, ilabels); cout << "Segment region number: " << regionNum << endl; vector <vector<Point>> fillRegionPoints; vector <Mat> fillRegions; vector<Scalar> colorValue; // Initial for (int i = 0; i < regionNum; i++){ colorValue.push_back(Scalar(0, 0, 0)); fillRegions.push_back(Mat(targetImg.size(), CV_8UC3, Scalar(255, 255, 255))); } fillRegionPoints.resize(regionNum); // Sort blobs size for (int i = 0; i < targetImg.rows; i++) for (int j = 0; j < targetImg.cols; j++) { int label = ilabels[i][j]; fillRegionPoints[label].push_back(Point(i, j)); } sort(fillRegionPoints.begin(), fillRegionPoints.end(), CompareLength); // Compute average color for (int i = 0; i < fillRegionPoints.size(); i++){ int pixNum = fillRegionPoints[i].size(); for (int j = 0; j < fillRegionPoints[i].size(); j++) { int x = fillRegionPoints[i][j].x; int y = fillRegionPoints[i][j].y; colorValue[i] += Scalar(targetImg.at<Vec3b>(x, y)[0], targetImg.at<Vec3b>(x, y)[1], targetImg.at<Vec3b>(x, y)[2])/pixNum; } } // Recover origin average color for (int i = 0; i < fillRegionPoints.size(); i++){ Scalar color = colorValue[i]; for (int j = 0; j < fillRegionPoints[i].size(); j++){ int x = fillRegionPoints[i][j].x; int y = fillRegionPoints[i][j].y; colorSegment.at<Vec3b>(x, y) = Vec3b(color[0], color[1], color[2]); fillRegions[i].at<Vec3b>(x, y) = Vec3b(color[0], color[1], color[2]); } } // Background Removal colorValue.erase(colorValue.begin()); fillRegions.erase(fillRegions.begin()); ShowImg("Color Segment", colorSegment,-1); imwrite("Color Segment.jpg", colorSegment); fisrtDrawCluster.resize(fillRegions.size()); FillSimulation(fillRegions, colorValue, fisrtDrawCluster); }
void MeanShiftTracker::trackObject(const cv::Mat &image) { double temp = 0; double pu_actual[colorBins][colorBins][colorBins]; double pu_nuevo[colorBins][colorBins][colorBins]; cv::Point newCenter = cv::Point(-1, -1); cv::Point currentCenter = cv::Point(-1, -1); currentCenter = trackingWindowCenter; iteration = 1; do { getPu(image, pu_actual, currentCenter); newCenter = MeanShift(image, pu_actual, currentCenter); getPu(image, pu_nuevo, newCenter); temp = getBhattaCoefficient(qu, pu_nuevo) - getBhattaCoefficient(qu, pu_actual) ; if (temp < bhattaEpsilon || (iteration == maxIterations)) { if (temp < 0) newCenter = currentCenter; bhattaCoefficient = getBhattaCoefficient(qu, pu_nuevo); break; } else { currentCenter = newCenter; iteration += 1; } } while (true); getPu(image, pu_nuevo, newCenter); double newPu = getBhattaCoefficient(qu, pu_nuevo); lastError = newPu; trackingWindowCenter = newCenter; if (enableModelUpdate) //if (getBhattaCoefficient(qu, pu_nuevo) < modelUpdateEpsilon) updateModel(image, newCenter); trackingWindow = calculateTrackingWindow(trackingWindowCenter); }