// ###################################################################### void CenterSurroundHistogramSegmenter::csTemplateSalientRegion (Point2D<int> pt) { Point2D<int> gpt(pt.i/GRID_SIZE, pt.j/GRID_SIZE); Rectangle intRect = itsIntegralHistogram.getBounds(); Timer tim(1000000); tim.reset(); // try the various center surround combination for(uint i = 0; i < itsCStemplates.size(); i++) { Rectangle cR = itsCStemplates[i].first; Rectangle sR = itsCStemplates[i].second; // only use the rectangle part that overlaps the image Rectangle grC = intRect.getOverlap(cR+gpt); Rectangle grS = intRect.getOverlap(sR+gpt); // get the center and surround histograms rutz::shared_ptr<Histogram> hC = getGridHistogramDistribution(grC); rutz::shared_ptr<Histogram> hCS = getGridHistogramDistribution(grS); Histogram hS = (*hCS) - (*hC); // smooth and normalize int npointC = grC.area()*GRID_SIZE*GRID_SIZE; int npointS = grS.area()*GRID_SIZE*GRID_SIZE - npointC; Histogram shC = smoothAndNormalize(*hC, npointC); Histogram shS = smoothAndNormalize( hS, npointS); // get the difference float diff = shS.getChiSqDiff(shC); // update the center surround belief estimation // we store the max as the best estimation of belief for(int ii = grS.left(); ii <= grS.rightI(); ii++) for(int jj = grS.top(); jj <= grS.bottomI(); jj++) { Point2D<int> pt(ii,jj); // if point is in center if(grC.contains(pt)) { float prevC = itsGridCenterBelief.getVal(ii,jj); if(prevC < diff) itsGridCenterBelief.setVal(ii,jj, diff); } // or surround else { float prevS = itsGridSurroundBelief.getVal(ii,jj); if(prevS < diff) itsGridSurroundBelief.setVal(ii,jj, diff); } } } LINFO("time: %f", tim.get()/1000.0F); }
// ###################################################################### void CenterSurroundHistogramSegmenter::ptsSalientRegion(Point2D<int> pt) { Timer tim(1000000); tim.reset(); // // display window // uint width = itsImage.getWidth(); // uint height = itsImage.getHeight(); // if(itsWin.is_invalid()) // itsWin.reset(new XWinManaged(Dims(width, height), 0, 0, "CSHse")); // else itsWin->setDims(Dims(width, height)); Point2D<int> gpt(pt.i/GRID_SIZE, pt.j/GRID_SIZE); // try various center surround combination for(uint i = 0; i < itsCSpoints.size(); i++) { Timer tim(1000000); tim.reset(); std::vector<Point2D<int> > cPoints = itsCSpoints[i].first; std::vector<Point2D<int> > sPoints = itsCSpoints[i].second; // create center histogram from the center points // make sure the points are in bounds Histogram hC; bool cinit = false; std::vector<Point2D<int> > cAPoints; for(uint j = 0; j < cPoints.size(); j++) { Point2D<int> pt = gpt + cPoints[j]; if(!itsGridHistogram.coordsOk(pt)) continue; cAPoints.push_back(pt); if(!cinit) { hC = *itsGridHistogram.getVal(pt); cinit = true; } else hC = hC + *itsGridHistogram.getVal(pt); } // create surround histogram from the surround points // make sure the points are in bounds Histogram hS; bool sinit = false; std::vector<Point2D<int> > sAPoints; for(uint j = 0; j < sPoints.size(); j++) { Point2D<int> pt = gpt + sPoints[j]; if(!itsGridHistogram.coordsOk(pt)) continue; sAPoints.push_back(pt); if(!sinit) { hS = *itsGridHistogram.getVal(pt); sinit = true; } else hS = hS + *itsGridHistogram.getVal(pt); } // smooth and normalize the resulting histogram int npointC = cAPoints.size()*GRID_SIZE*GRID_SIZE; int npointS = sAPoints.size()*GRID_SIZE*GRID_SIZE; Histogram shC = smoothAndNormalize(hC, npointC); Histogram shS = smoothAndNormalize(hS, npointS); // print the difference float diff = shS.getChiSqDiff(shC); // update the center belief estimation // we store the max as the best estimation of belief for(uint cc = 0; cc < cAPoints.size(); cc++) { Point2D<int> pt = cAPoints[cc]; float prevC = itsGridCenterBelief.getVal(pt); if(prevC < diff) itsGridCenterBelief.setVal(pt, diff); } // update the surround belief estimation for(uint ss = 0; ss < sAPoints.size(); ss++) { Point2D<int> pt = sAPoints[ss]; float prevS = itsGridSurroundBelief.getVal(pt); if(prevS < diff) itsGridSurroundBelief.setVal(pt, diff); } // // display the window // Image<PixRGB<byte> > disp(width, height, ZEROS); // float mVal = 127; // float bVal = 255 - mVal; // Image<byte> dImaR, dImaG, dImaB; // getComponents(itsImage, dImaR, dImaG, dImaB); // inplaceNormalize(dImaR, byte(0), byte(mVal)); // inplaceNormalize(dImaG, byte(0), byte(mVal)); // inplaceNormalize(dImaB, byte(0), byte(mVal)); // Image<PixRGB<byte> > dIma = makeRGB(dImaR,dImaG,dImaB); // //inplacePaste (disp, dIma, Point2D<int>(0,0)); // Image<PixRGB<byte> > dImaCS(width,height, ZEROS); // for(uint j = 0; j < cAPoints.size(); j++) // drawFilledRect(dImaCS, Rectangle(cAPoints[j],Dims(1,1))*GRID_SIZE, // PixRGB<byte>(byte(bVal),0,0)); // for(uint j = 0; j < sAPoints.size(); j++) // drawFilledRect(dImaCS, Rectangle(sAPoints[j],Dims(1,1))*GRID_SIZE, // PixRGB<byte>(0, byte(bVal),0)); // Image<PixRGB<byte> > tdIma(dIma+dImaCS); // inplacePaste (disp, tdIma, Point2D<int>(0,0)); // drawCross(disp, pt, PixRGB<byte>(255,0,0), 10, 1); // itsWin->drawImage(disp,0,0); // Raster::waitForKey(); } LINFO("time: %f", tim.get()/1000.0F); }