26 #include "CondensHisto.hh"
27 #include "Base/ImageUtils/ImageDraw.hh"
29 #include <Base/Common/BIASpragma.hh>
43 useMultiHistogram_=
false;
44 secondOrderScale_ = 0.01;
45 firstOrderScale_ = 0.99;
46 bIntegralHisto_=
false;
47 m_dMaxRefHistoSize=DBL_MAX;
48 secondHistPos_[0]=0; secondHistPos_[1]=1;
53 bIntegralHisto_ = bIntHisto;
60 processMean_=areaMax_ * 0.5;
64 processFirstOrderScale_.Set(firstOrderScale_);
68 processSecondOrderScale_.Set(secondOrderScale_);
71 diffusionSigma_[0]= diffScale_ * areaMax_[0];
72 diffusionSigma_[1]= diffScale_ * areaMax_[1];
74 diffusionSigma_[stateDim_-1] = diffScaleSize_ * areaMax_[stateDim_-1];
78 importanceFraction_ = 0.0;
88 for (
unsigned int n=0; n<nrOfInitSamples; n++) {
89 for (
unsigned int d=0;d<stateDim_;d++) {
91 random_.GetUniformDistributed(areaMin_[d],areaMax_[d]);
100 intHisto_.GenerateIntegralHistDiffBin(*img_,refHisto_.GetBinSize(),
101 refHisto_.GetBinSizeSat());
103 BCDOUT(D_COND_INT,
"in Integral EOD\n");
104 int width = (int)img_->GetWidth();
105 int height = (int)img_->GetHeight();
107 int heightHalf = refHisto_.GetHeightROI()/2;
108 int widthHalf = refHisto_.GetWidthROI()/2;
111 int upperleftx,upperlefty,lowerrightx, lowerrighty;
112 int upperleftx2,upperlefty2,lowerrightx2, lowerrighty2;
114 for(
unsigned int x = 0; x<Nsamples_; x++){
115 BCDOUT(D_COND_PROCESS,
"Sample Pos: "<<(
int)samplePosNew_[x][0]<<
116 ","<<(
int)samplePosNew_[x][1]<<std::endl);
120 (int)rint(samplePosNew_[x][0]-widthHalf * samplePosNew_[x][2]);
122 (int)rint(samplePosNew_[x][1]-heightHalf* samplePosNew_[x][2]);
124 (int)rint(samplePosNew_[x][0]+widthHalf* samplePosNew_[x][2]);
126 (int)rint(samplePosNew_[x][1]+heightHalf* samplePosNew_[x][2]);
128 BCDOUT(D_COND_INT,
"ROI: "<<upperleftx<<
","<<upperlefty<<
129 ","<<lowerrightx<<
","<<lowerrighty<<std::endl);
132 if((upperleftx>=width)||(upperleftx<0)||(upperlefty<0)
133 ||(upperlefty>=height)||(lowerrightx<0)
134 ||(lowerrightx>=width)||(lowerrighty<0)
135 ||(lowerrighty>=height) ||(samplePosNew_[x][2]<=0)
136 ||(samplePosNew_[x][2]> m_dMaxRefHistoSize)
137 ||(lowerrighty <= upperlefty)||(lowerrightx <= upperleftx)) {
138 sampleWeights_[x] = 0.0;
140 if (useMultiHistogram_) {
141 upperleftx2= upperleftx + int(secondHistPos_[0]*2)*widthHalf;
142 lowerrightx2=upperleftx2+ 2*widthHalf;
143 upperlefty2= upperlefty+ int(secondHistPos_[1]*2)*heightHalf;
144 lowerrighty2=upperlefty2+2*heightHalf;
146 if (useMultiHistogram_ &&
147 !((upperleftx2>=width)||(upperleftx2<0)||(upperlefty2<0)
148 ||(upperlefty2>=height)||(lowerrightx2<0)
149 ||(lowerrightx2>=width)||(lowerrighty2<0)
150 ||(lowerrighty2>=height)) )
151 dist=intHisto_.CalcSimilarity(refHisto_,upperleftx,upperlefty,
152 lowerrightx,lowerrighty,
153 refHisto2_,upperleftx2,upperlefty2,
154 lowerrightx2, lowerrighty2);
157 dist=intHisto_.CalcSimilarity(refHisto_,upperleftx,upperlefty,
158 lowerrightx,lowerrighty);
160 BCDOUT(D_COND_PROCESS,
"Distance of Histograms: "<<dist<<std::endl);
162 sampleWeights_[x] = minWeight_;
164 sampleWeights_[x] = dist;
177 std::vector<int> point;
181 EvaluateFromIntegralHistoImage_();
183 int heightHalf = refHisto_.GetHeightROI()/2;
184 int widthHalf = refHisto_.GetWidthROI()/2;
188 int upperleftx,upperlefty,lowerrightx, lowerrighty;
189 int upperleftx2,upperlefty2,lowerrightx2, lowerrighty2;
190 int width=img_->GetWidth();
191 int height=img_->GetHeight();
192 for(
unsigned int x = 0; x<Nsamples_; x++){
193 BCDOUT(D_COND_PROCESS,
"Sample Pos: "<<(
int)samplePosNew_[x][0]<<
194 ","<<(
int)samplePosNew_[x][1]<<std::endl);
198 upperleftx=(int)(samplePosNew_[x][0]-widthHalf * samplePosNew_[x][2]);
199 upperlefty=(int)(samplePosNew_[x][1]-heightHalf* samplePosNew_[x][2]);
200 lowerrightx=(int)(samplePosNew_[x][0]+widthHalf* samplePosNew_[x][2]);
201 lowerrighty=(int)(samplePosNew_[x][1]+heightHalf* samplePosNew_[x][2]);
203 BCDOUT(D_COND_DEBUG,
"RIU: "<<upperleftx<<
","<<upperlefty<<
204 ","<<lowerrightx<<
","<<lowerrighty<<std::endl);
206 if ( upperleftx>=width || upperleftx<0 || upperlefty<0 ||
207 upperlefty>=height || lowerrightx<0 ||
208 lowerrightx>=width || lowerrighty<0 ||
209 lowerrighty>=height ) {
210 sampleWeights_[x] = 0;
212 if (useMultiHistogram_) {
213 upperleftx2= upperleftx + int(secondHistPos_[0]*2)*widthHalf;
214 lowerrightx2=upperleftx2+ 2*widthHalf;
215 upperlefty2= upperlefty+ int(secondHistPos_[1]*2)*heightHalf;
216 lowerrighty2=upperlefty2+2*heightHalf;
218 if (useMultiHistogram_ &&
219 !((upperleftx2>=width)||(upperleftx2<0)||(upperlefty2<0)
220 ||(upperlefty2>=height)||(lowerrightx2<0)
221 ||(lowerrightx2>=width)||(lowerrighty2<0)
222 ||(lowerrighty2>=height))
224 chist_.GenerateHist(*img_,upperleftx2, upperlefty2,
225 lowerrightx2, lowerrighty2);
226 dist = refHisto_.CalcSimilarity(chist_);
229 chist_.GenerateHist(*img_,upperleftx, upperlefty,
230 lowerrightx, lowerrighty);
232 dist = refHisto_.CalcSimilarity(chist_);
234 dist = 0.5*dist+ 0.5*refHisto_.CalcSimilarity(chist_);
235 BCDOUT(D_COND_PROCESS,
"Distance of Histograms: "<<dist<<std::endl);
238 sampleWeights_[x] = minWeight_;
240 sampleWeights_[x] = dist;
257 int heightHalf = refHisto_.GetHeightROI()/2;
258 int widthHalf = refHisto_.GetWidthROI()/2;
259 unsigned int upperleftx,upperlefty,lowerrightx, lowerrighty;
262 upperleftx=(int)(mean_[0]-widthHalf * mean_[2]);
263 upperlefty=(int)(mean_[1]-heightHalf* mean_[2]);
264 lowerrightx=(int)(mean_[0]+widthHalf* mean_[2]);
265 lowerrighty=(int)(mean_[1]+heightHalf* mean_[2]);
268 if(!useMultiHistogram_)
270 BIASERR(
"Only implemented for Integral Histogram");
272 else if(bIntegralHisto_)
274 std::vector<int> roi;
275 roi.push_back(upperleftx);
276 roi.push_back(upperlefty);
277 roi.push_back(lowerrightx);
278 roi.push_back(lowerrighty);
279 Vector<int> hist = intHisto_.GetHistogram(upperleftx,upperlefty,
280 lowerrightx,lowerrighty);
282 int nBinSat = intHisto_.GetBinSizeSat();
283 int nBinHue = intHisto_.GetBinSize();
284 int nLumStart = nBinSat * nBinHue;
289 double lowerLum =hist[nLumStart]+hist[nLumStart+1]+hist[nLumStart+2]+
291 double higherLum = hist[nLumStart+5]+hist[nLumStart+6]+
293 int numPixel = hist[nLumStart+8];
294 if(lowerLum / numPixel > percent)
296 cout<<
"Luminance is low: increase!"<<endl;
299 else if(higherLum / numPixel > percent)
301 cout<<
"Luminance is high: decrease!"<<endl;
307 BIASERR(
"Only implemented for binSizeHue==8");
312 BIASERR(
"Only implemented for Integral Histogram");
322 max[0] = samplePosOld_[pos][0];
323 max[1] = samplePosOld_[pos][1];
324 max[2] = samplePosOld_[pos][2];
340 double meanWeight = GetMeanWeight();
341 double varWeight=0.0, ddiff;
346 for(
unsigned int i=0;i<Nsamples_;i++){
347 diff = samplePosOld_[i] - mean_;
349 ddiff = (sampleWeights_[i]-meanWeight);
350 varWeight += (ddiff*ddiff);
353 mVar/=double(Nsamples_-1);
358 meanVar[3]=varWeight/double(Nsamples_-1);
367 double maxWeight=0.0;
369 for(
unsigned int i=0;i<Nsamples_;i++)
371 if(sampleWeights_[i] > maxWeight)
374 maxWeight = sampleWeights_[i];
389 double maxWeight = GetMaxWeight(index);
390 double varWeight=0.0, ddiff;
395 for(
unsigned int i=0;i<Nsamples_;i++){
396 diff = samplePosOld_[i] - max;
398 ddiff = (sampleWeights_[i]-maxWeight);
399 varWeight += (ddiff*ddiff);
402 var/=double(Nsamples_-1);
407 maxVar[3]=varWeight/double(Nsamples_-1);
420 for (
unsigned int i =0;i<Nsamples_;i++){
421 y = (
unsigned int)fabs(samplePosNew_[i][1]);
422 x = (
unsigned int)fabs(samplePosNew_[i][0]);
423 imgData[y][x] = float(sampleWeights_[i]);
440 img.
Init(img_->GetWidth(),img_->GetHeight(),3);
444 unsigned int histosize;
445 unsigned char values[1];
447 for (
unsigned int i=0;i<Nsamples_;i++)
451 (
unsigned int)(samplePosNew_[i][2]*
452 sqrt(
double(refHisto_.GetHeightROI()*
453 refHisto_.GetWidthROI())));
455 radius = (
unsigned int)(sampleWeights_[i]* Nsamples_ /
456 sumOfWeights_ + 0.5);
458 if ( (samplePosNew_[i][0]-radius>0) && (samplePosNew_[i][1]-radius>0) &&
459 (samplePosNew_[i][0]+radius<img.
GetWidth()) &&
460 (samplePosNew_[i][1]+radius<img.
GetHeight()) )
462 (
unsigned int)samplePosNew_[i][0],
476 img.
Init(img_->GetWidth(),img_->GetHeight(),1);
483 for (
unsigned int n=0;n<Nsamples_;n++)
484 if (sampleWeights_[n]>maxWeight) maxWeight=sampleWeights_[n];
485 for (
unsigned int n=0;n<Nsamples_;n++) {
487 values[c]=(
unsigned char)(sampleWeights_[n]/maxWeight*255.0+0.5);
489 size=(
unsigned int)(samplePosNew_[n][2]*
double(refHisto_.GetHeightROI()));
491 if ( (samplePosNew_[n][0]-size/2>0) &&
492 (samplePosNew_[n][1]-size/2>0) &&
493 (samplePosNew_[n][0]+size/2<img.
GetWidth()) &&
494 (samplePosNew_[n][1]+size/2<img.
GetHeight()) )
496 int(samplePosNew_[n][1]),
509 img.
Init(img_->GetWidth(),img_->GetHeight(),1);
517 unsigned int maxSampleIndex = 0;
518 for (
unsigned int n=0;n<Nsamples_;n++)
520 if (sampleWeights_[n]>maxWeight){
522 maxWeight=sampleWeights_[n];
525 for (
unsigned int n=0;n<Nsamples_;n++)
527 size= (
unsigned int)(samplePosNew_[n][2]*
double(refHisto_.GetHeightROI()));
529 if ( (samplePosNew_[n][0]-size/2>0) &&
530 (samplePosNew_[n][1]-size/2>0) &&
531 (samplePosNew_[n][0]+size/2<img.
GetWidth()) &&
532 (samplePosNew_[n][1]+size/2<img.
GetHeight()) )
534 if(n==maxSampleIndex)
536 values[0] = (
unsigned char)0;
537 values[1] = (
unsigned char)0;
538 values[2] = (
unsigned char)0;
541 values[0] = (
unsigned char)100;
542 values[1] = (
unsigned char)100;
543 values[2] = (
unsigned char)100;
546 int(samplePosNew_[n][1]),
551 values[0] = (
unsigned char)0;
552 values[1] = (
unsigned char)0;
553 values[2] = (
unsigned char)0;
554 size= (
unsigned int)(samplePosNew_[maxSampleIndex][2]*
555 double(refHisto_.GetHeightROI()));
557 int(samplePosNew_[maxSampleIndex][0]),
558 int(samplePosNew_[maxSampleIndex][1]),
568 for (
unsigned int n=0;n<Nsamples_;n++)
569 if (sampleWeights_[n]>maxWeight) maxWeight=sampleWeights_[n];
570 for (
unsigned int n=0;n<Nsamples_;n++) {
572 values[c]=(
unsigned char)(sampleWeights_[n]/maxWeight*255.0);
573 size= (
unsigned int)(samplePosNew_[n][2]*refHisto_.GetHeightROI());
575 if ( (samplePosNew_[n][0]-size/2>0) &&
576 (samplePosNew_[n][1]-size/2>0) &&
577 (samplePosNew_[n][0]+size/2<img.
GetWidth()) &&
578 (samplePosNew_[n][1]+size/2<img.
GetHeight()) )
580 (
unsigned int)samplePosNew_[n][0],
581 (
unsigned int)samplePosNew_[n][1],
599 img.
Init(img_->GetWidth(),img_->GetHeight(),1);
603 float *values=
new float[Nsamples_];
605 for (
unsigned int i=0;i<Nsamples_;i++) {
607 values[i]=float(samplePosOld_[i][stateDim_-1]);
608 radius = (
unsigned int)(sampleWeights_[i]* Nsamples_ /
609 sumOfWeights_ + 0.5);
611 if ( (samplePosOld_[i][0]-radius>0) && (samplePosOld_[i][1]-radius>0) &&
612 (samplePosOld_[i][0]+radius<img.
GetWidth()) &&
613 (samplePosOld_[i][1]+radius<img.
GetHeight()) )
615 (
unsigned int)samplePosOld_[i][0],
616 (
unsigned int)samplePosOld_[i][1],
630 double max = DrawPosteriorDistribution(fImg_);
634 pixel = fImg_.GetImageData();
637 for (
unsigned int i=0;i<size;i++) {
638 *data = (
unsigned char)(*pixel / max * 255.0);
655 for (
unsigned int i=0;i<Nsamples_;i++) {
656 x = samplePosOld_[i][0];
657 y = samplePosOld_[i][1];
659 if ( (x>0) && (y>0) &&
660 (x<width-1) && (y<height-1) ) {
663 dx = x - (double)floor(x); dy = y - (double)floor(y);
665 *pixel+= float((1-dx) * (1-dy) * sampleWeights_[i]);
666 if (*pixel>max) max=*pixel;
669 *pixel+= float((1-dy) * dx * sampleWeights_[i]);
670 if (*pixel>max) max=*pixel;
673 *pixel+= float(dx * dy * sampleWeights_[i]);
674 if (*pixel>max) max=*pixel;
677 *pixel+= float((1-dx) * dy * sampleWeights_[i]);
678 if (*pixel>max) max=*pixel;
692 cout<<
"Mean: "<<mean_[0]<<
","<<mean_[1]<<
","<<mean_[2]<<endl;
694 std::vector<double> point;
695 refMultiHisto_.GetDistROIs(point);
696 point[0] = mean_[0]+point[0]*mean_[2];
697 point[1] = mean_[1]+point[1]*mean_[2];
698 cout<<
"In Draw, Point: "<<point[0]<<
","<<point[1]<<endl;
701 int upperleftx,upperlefty,lowerrightx, lowerrighty;
702 int upperleftx2,upperlefty2,lowerrightx2, lowerrighty2;
703 float i=float(mean_[2]*((refHisto_.GetHeightROI()+
704 refHisto_.GetWidthROI())/2));
707 int heightHalf = int(
double(refHisto_.GetHeightROI())/2.0 * mean_[2] + 0.5);
708 int widthHalf = int(
double(refHisto_.GetWidthROI())/2.0 * mean_[2] + 0.5);
710 (int)rint(mean_[0]-widthHalf);
712 (int)rint(mean_[1]-heightHalf);
714 (int)rint(mean_[0]+widthHalf);
716 (int)rint(mean_[1]+heightHalf);
721 if ((upperleftx>0) && (upperlefty>0) && (lowerrightx<(
int)img.
GetWidth()) &&
725 lowerrightx,lowerrighty);
727 lowerrightx-1,lowerrighty-1);
729 lowerrightx+1,lowerrighty+1);
732 if(useMultiHistogram_){
733 upperleftx2= upperleftx + (int)(secondHistPos_[0]*2*widthHalf);
734 lowerrightx2=upperleftx2+ 2*widthHalf;
735 upperlefty2= upperlefty+ (int)(secondHistPos_[1]*2*heightHalf);
736 lowerrighty2=upperlefty2+2*heightHalf;
739 if ((upperleftx2>0) && (upperlefty2>0) &&
740 (lowerrightx2<(
int)img.
GetWidth()) &&
743 lowerrightx2,lowerrighty2);
void Release()
reimplemented from ImageBase
void UseIntegralHistogram(bool bIntHisto)
virtual void DrawSamplesWhite(Image< unsigned char > &img)
this draws the sample weights as retangeles with greyvalues on white base
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
void SetColorModel(EColorModel Model)
void ElementwiseProduct(const Vector< T > &arg, Vector< T > &dest) const
multiply this with arg elementwise and store the result in dest
virtual void EvaluateObservationDensities()
Here assign a weight to each sample.
void GetMaxVariance(Vector< double > &maxVar)
void GetMeanVariance(Vector< double > &meanVar)
CondensHisto()
Set the state dimension in the constructor.
void DrawSamplesExt(Image< unsigned char > &img)
this draws the samples, histogram sizes as rects and weights as grey values
unsigned int GetWidth() const
void SetZero()
equivalent to matrix call
static int RectangleCorners(Image< StorageType > &im, const int minx, const int miny, const int maxx, const int maxy, const StorageType value[])
rectangles
static int CircleCenter(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const StorageType Value[]=NULL)
draws a circular line, either using Value or a good contrast value
Vector< T > & newsize(Subscript N)
void DrawWeightedSamples(Image< float > &img)
this draws the samples as circles and the color indicates the size of the histogram (the z-coordinate...
virtual void DrawMean(BIAS::Image< unsigned char > &img)
Draws the mean as a rectangle which size is proportional to the histogram size.
virtual void InitModelDefaults()
Here u can specify the model and process defaults.
void SetArea(Vector3< double > min, Vector3< double > max)
The area for the initial uniform distribution.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Image< unsigned char > GetAktImage()
unsigned int GetHeight() const
void FillImageWithConstValue(StorageType Value)
fill grey images
static int RectangleCenter(Image< StorageType > &im, const int x, const int y, const int size, const StorageType value[])
Draws the rectangle around X, Y with Size and Value[i] in channel i.
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
void GetMeanValue(Vector< double > &mean)
virtual void InitPriorPositions(unsigned int nrOfInitSamples)
The Prior condition is used to init the first sample positions.
void DrawPosteriorDistribution(BIAS::Image< unsigned char > &img)
Draws the samples with their weight as greyvalue, the weight is distributed to the 4 pixels around th...
void GetMaxValue(Vector< double > &max)
double GetMaxWeight(int &index)
void EvaluateFromIntegralHistoImage_()
drawing simple entities into the image like rectangles or lines As all functions are static they have...
void DrawHistoSizes(Image< unsigned char > &img)
this draws the Sizes of the Histogram as arrows at their x position
unsigned long int GetPixelCount() const
returns number of pixels in image
void CheckImageQuality(double percent, int &shutter, int &gain)
Checks the image Quality by checking the luminance of the image.
const std::vector< BIAS::Vector< double > > & GetSamplePositions()
virtual void DrawSamples(Image< unsigned char > &img)
this draws the sample weights as circles
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase