source: tags/release-1.1.2/src/Utils/zscale.cc @ 1441

Last change on this file since 1441 was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 4.4 KB
Line 
1// -----------------------------------------------------------------------
2// zscale.cc: Get a suitable pixel value scaling for displaying a 2D
3//            image in greyscale.
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <algorithm>
31#include <duchamp/Utils/utils.hh>
32const int nsample=1000;
33// const int width=300;
34const int width=150;
35const float contrast=0.25;
36
37void zscale(long imagesize, float *image, float &z1, float &z2)
38{
39
40  float *smallarray = new float[nsample];
41  float *ct = new float[nsample];
42  long size=0;
43
44  float mean=0.,sd=0.;
45  for(int i=0;i<imagesize;i++) mean+=image[i];
46  mean /= float(imagesize);
47  for(int i=0;i<imagesize;i++) sd+=(image[i]-mean)*(image[i]-mean);
48  sd /= float(imagesize);
49 
50
51  if(imagesize>nsample){
52    int step = (imagesize / nsample) + 1;
53    for(int i=0;i<imagesize;i++){
54      if((i%step)==0){
55        smallarray[size] = image[i];
56        ct[size]=(float)size+1.;
57        size++;
58      }
59    }
60  }
61  else{
62    for(int i=0;i<imagesize;i++){
63      smallarray[i] = image[i];
64      ct[i] = float(i+1);
65    }
66    size=imagesize;
67  }
68
69  std::sort(smallarray,smallarray+size);
70 
71  /* fit a linear slope to the centre of the cumulative distribution */
72  long midpt = size/2;
73  long imin = midpt - width;
74  float slope,intercept,errSlope,errIntercept,r;
75  if(size<2*width)
76    linear_regression(size,ct,smallarray,0,size-1,slope,errSlope,
77                      intercept,errIntercept,r);
78  else linear_regression(size,ct,smallarray,imin,imin+2*width,slope,errSlope,
79                         intercept,errIntercept,r);
80
81  z1 = smallarray[midpt] + (slope/contrast)*(float)(1-midpt);
82  z2 = smallarray[midpt] + (slope/contrast)*(float)(nsample-midpt);
83
84  if(z1==z2){
85 
86    if(z1!=0) z2 = z1 * 1.05;
87    else z2 = z1+0.01;
88
89  }
90
91  delete [] smallarray;
92  delete [] ct;
93           
94}
95
96
97void zscale(long imagesize, float *image, float &z1, float &z2, float blankVal)
98{
99  float *newimage = new float[imagesize];
100  int newsize=0;
101  for(int i=0;i<imagesize;i++) if(image[i]!=blankVal) newimage[newsize++] = image[i];
102
103  float *smallarray = new float[nsample];
104  float *ct = new float[nsample];
105  long size=0;
106
107  float mean=0.,sd=0.;
108  for(int i=0;i<newsize;i++) mean+=newimage[i];
109  mean /= float(newsize);
110  for(int i=0;i<newsize;i++) sd+=(newimage[i]-mean)*(newimage[i]-mean);
111  sd /= float(newsize);
112 
113
114  if(newsize>nsample){
115    int step = (newsize / nsample) + 1;
116    for(int i=0;i<newsize;i++){
117      if((i%step)==0){
118        smallarray[size] = newimage[i];
119        ct[size]=(float)size+1.;
120        size++;
121      }
122    }
123  }
124  else{
125    for(int i=0;i<newsize;i++){
126      smallarray[i] = newimage[i];
127      ct[i] = float(i+1);
128    }
129    size=newsize;
130  }
131
132  std::sort(smallarray,smallarray+size);
133 
134  /* fit a linear slope to the centre of the cumulative distribution */
135  long midpt = size/2;
136  long imin = midpt - width;
137  float slope,intercept,errSlope,errIntercept,r;
138  if(size<2*width)
139    linear_regression(size,ct,smallarray,0,size-1,slope,errSlope,
140                      intercept,errIntercept,r);
141  else linear_regression(size,ct,smallarray,imin,imin+2*width,slope,errSlope,
142                    intercept,errIntercept,r);
143
144  z1 = smallarray[midpt] + (slope/contrast)*(float)(1-midpt);
145  z2 = smallarray[midpt] + (slope/contrast)*(float)(nsample-midpt);
146
147  if(z1==z2){
148 
149    if(z1!=0) z2 = z1 * 1.05;
150    else z2 = z1+0.01;
151
152  }
153
154  delete [] newimage;
155  delete [] smallarray;
156  delete [] ct;
157
158           
159}
160
161
Note: See TracBrowser for help on using the repository browser.