20 #include <gsl/gsl_rng.h>
21 #include "GSLUtility.h"
24 #include "CentroidApolloPan.h"
36 CentroidApolloPan::CentroidApolloPan(
double pixelSizeMicrons)
38 if( pixelSizeMicrons > 0)
39 this->m_pixelSize = pixelSizeMicrons;
41 this->m_pixelSize = 5.0;
48 CentroidApolloPan::~CentroidApolloPan(){};
57 bool CentroidApolloPan::setPixelSize(
double microns)
59 if( microns <= 0)
return false;
60 this->m_pixelSize = microns;
74 int CentroidApolloPan::selectAdaptive(
Chip *inputChip,
Chip *selectionChip) {
93 double minDN,maxDN,temp;
97 lines = inputChip->
Lines();
101 if (lines <= 0 || samples <= 0)
105 minDN = this->getMinDN();
106 maxDN = this->getMaxDN();
109 for (i=1;i<=lines;i++) {
110 if (i == 1 || i == lines) {
116 for(j=1;j<=samples;j+=step) {
117 dn.push_back(inputChip->
GetValue(j,i));
127 for (k=1;k<int(dn.size());k++) {
134 if (i <
int(j/10)) dn.erase(dn.begin()+l);
138 if( temp > minDN) this->setDNRange(temp,maxDN);
142 this->select(inputChip,selectionChip);
144 this->setDNRange(minDN,maxDN);
192 int CentroidApolloPan::elipticalReduction(
Chip *selectionChip,
double percent_selected,
193 double play,
int patience_limit)
201 std::vector< std::vector<int> > pts;
202 std::vector< std::vector<double> > cen_pts;
204 std::vector<double> pt(2);
208 double a = 60.0*5.0/m_pixelSize,
209 b = 60.0*5.0/m_pixelSize,
213 gsl_rng *randGen = gsl_rng_alloc(gsl_rng_taus);
214 gsl_rng_set(randGen, time(NULL));
216 samples = selectionChip->
Samples();
217 lines = selectionChip->
Lines();
221 k = int(ceil(
double(samples) - a));
222 l = int(ceil(
double(lines ) - b));
233 double searchStep = 5.0*5.0/m_pixelSize;
234 double aSearch,centerSample,centerLine,delta,cSearch;
235 cSearch = searchStep/5.0;
236 centerSample = double((k+i)/2.0);
237 centerLine = double((l+j)/2.0);
238 delta = (centerSample-double(i))*(centerSample-
double(i)) + (centerLine -
double(l))*(centerLine -
double(l));
240 aSearch = (searchStep-cSearch)/delta;
242 for (pt[0]=floor(a+1.0); pt[0]<k; pt[0]+=searchStep) {
243 for (pt[1]=floor(b+1.0); pt[1]<l; pt[1]+=searchStep) {
244 cen_pts.push_back(pt);
245 delta = (centerSample-double(pt[0]))*(centerSample-
double(pt[0])) + (centerLine-
double(pt[1]))*(centerLine-
double(pt[1]));
246 searchStep = aSearch*delta + cSearch;
247 if (searchStep > l - pt[1] && l - pt[1] > 1e-4)
248 searchStep = l - pt[1];
250 if (searchStep > k - pt[0] && k - pt[0] > 1e-4)
251 searchStep = k - pt[0];
255 selectionEdge(selectionChip,&pts);
258 if (pts.size() ==0)
return 0;
263 while (emptySets < patience_limit && cen_pts.size()>0) {
266 randPt = gsl_rng_uniform_int( randGen ,cen_pts.size() );
267 dpt[0] = cen_pts[randPt][0];
268 dpt[1] = cen_pts[randPt][1];
270 cen_pts.erase( cen_pts.begin() + randPt);
273 ellipseFromCenterAxesAngle(&ellNew, dpt[0], dpt[1], a, b, 0.0);
277 if (!bestFitEllipse(&ellNew, &pts, play, 50)) {
283 if (ellNew.area < ellBest.area) {
288 if (!ellipseInChip(&ellNew,selectionChip)) {
294 if (elipsePercentSelected(selectionChip,&ellNew) < percent_selected) {
302 ellBest.area = ellNew.area;
303 ellBest.A[0] = ellNew.A[0];
304 ellBest.A[1] = ellNew.A[1];
305 ellBest.A[2] = ellNew.A[2];
307 ellBest.cen[0] = ellNew.cen[0];
308 ellBest.cen[1] = ellNew.cen[1];
310 ellBest.semiMajor = ellNew.semiMajor;
311 ellBest.semiMinor = ellNew.semiMinor;
312 ellBest.majorAxis[0] = ellNew.majorAxis[0];
313 ellBest.majorAxis[1] = ellNew.majorAxis[1];
314 ellBest.minorAxis[0] = ellNew.minorAxis[0];
315 ellBest.minorAxis[1] = ellNew.minorAxis[1];
318 if( ellBest.area == 0)
324 for (i=1;i<=samples;i++) {
325 for (j=1;j<=lines;j++) {
329 if (!pointInEllipse(&ellBest,dpt,play)) {
330 if( selectionChip->
GetValue(i,j) == 1)