ModalityConvex::ModalityConvex(Config& config, string configbase) : Modality(config, configbase) { margin = config.read<int>(configbase + ".margin", 10); margin_diminish = CLAMP3(config.read<float>(configbase + ".diminish", 0.3f), 0, 0.9999f); persistence = CLAMP3(config.read<float>(configbase + ".persistence", 0.5f), 0, 0.9999f); flush(); }
void ModalityColor3DHistogram::update(Image& image, PatchSet* patchSet, Rect bounds) { Ptr<PatchSet> patches = Ptr<PatchSet>(reliablePatchesFilter.empty() ? patchSet : patchSet->filter(*reliablePatchesFilter)); Rect background_outer = expand(bounds, background_size + background_margin); Rect background_inner = expand(bounds, background_margin); MatND new_foreground, new_background; //tmp_float.convertTo(image.get(colorspace), CV_32F, 1.0/255.0); Mat arrays[] = {image.get(colorspace)}; Mat mask = image.get_mask(); mask.setTo(0); int half_size = patches->get_radius() * foreground_size; // Foreground histogram for (int i = 0; i < patches->size(); i++) { Point2f pos = patches->get_position(i); Rect r; r.x = CLAMP3( ((int)pos.x - half_size), 0, mask.cols); r.y = CLAMP3( ((int)pos.y - half_size), 0, mask.rows); r.width = CLAMP3( ((int)pos.x + half_size), 0, mask.cols) - r.x; r.height = CLAMP3( ((int)pos.y + half_size), 0, mask.rows) - r.y; if (r.width < 1 || r.height < 1) { continue; } mask(r) = 1; } calcHist(arrays, 1, channels, mask, new_foreground, 3, histSize, ranges, true, false); calcHist(arrays, 1, channels, mask, new_foreground, 3, histSize, ranges, true, false); mask.setTo(0); /*Point2f* convex_points = new Point2f[patches.size()]; for (int i = 0; i < patches.size(); i++) { convex_points[i] = patches.get_position(i); } fillconvex(mask, convex_points, convex_points.size(), Scalar(1));*/ Rect outer = expand(bounds, background_size + background_margin); rectangle(mask, outer.tl(), outer.br(), Scalar(1), FILLED); Rect inner = expand(bounds, background_margin); rectangle(mask, inner.tl(), inner.br(), Scalar(0), FILLED); rectangle(mask, background_outer.tl(), background_outer.br(), Scalar(1), FILLED); rectangle(mask, background_inner.tl(), background_inner.br(), Scalar(0), FILLED); calcHist(arrays, 1, channels, mask, new_background, 3, histSize, ranges, true, false); new_background += 1; // Merging model with new data float* ofd = (float*) foreground.data; float* nfd = (float*) new_foreground.data; float* obd = (float*) background.data; float* nbd = (float*) new_background.data; float* md = (float*) model.data; float apriori = (float)(bounds.width * bounds.height) / (float)(image.width() * image.height()); // TODO: justify factor int histCount = histSize[0] * histSize[1] * histSize[2]; float nfdSum = 0, nbdSum = 0; for (int i = 0; i < histCount; i++) { nfdSum += nfd[i]; nbdSum += nbd[i]; } float nfdSum2 = 0, nbdSum2 = 0; if (nfdSum > 0) { for (int i = 0; i < histCount; i++) { ofd[i] = foreground_presistence * ofd[i] + (1 - foreground_presistence) * (nfd[i] / nfdSum); nfdSum2 += ofd[i]; } } else { for (int i = 0; i < histCount; i++) { nfdSum2 += ofd[i]; } } if (nbdSum > 0) { for (int i = 0; i < histCount; i++) { obd[i] = background_presistence * obd[i] + (1 - background_presistence) * (nbd[i] / nbdSum); nbdSum2 += obd[i]; } } else { for (int i = 0; i < histCount; i++) { nbdSum2 += obd[i]; } } for (int i = 0; i < histCount; i++) { md[i] = ((apriori * (ofd[i] / nfdSum2)) / (apriori * (ofd[i] / nfdSum2) + (1 - apriori) * (obd[i] / nbdSum2))) * 255; } has_data = true; }
ModalityConvex::ModalityConvex(string configbase) : Modality(configbase) { margin = 10; margin_diminish = CLAMP3(0.3f, 0, 0.9999f); persistence = CLAMP3(0.7f, 0, 0.9999f); flush(); }