Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasdiff.cpp
1 // must be first:
2 //#include <Base/Common/LeakChecking.h>
3 
4 #include <Base/Image/ImageIO.hh>
5 #include "Base/Image/ImageConvert.hh"
6 #include "Utils/Param.hh"
7 #include <stdio.h>
8 #include <iostream>
9 
10 using namespace BIAS;
11 using namespace std;
12 
13 
14 
15 /** @file
16  @ingroup g_tools
17  @brief tool for computing intensity difference of images, see biasdiff.cpp
18  @author woelk */
19 
20 int main(int argc, char *argv[]){
21  // Dart Testing (JW)
22  if (argc>=2) if (strcmp(argv[1],"-darttest")==0){
23  std::cout<<"started dart test: "<<argv[0]<<std::endl;
24  return 0;
25  }
26 
27 
28  Param param;
29  param.AddParamInt("border", "sets ROI", 0, 0,INT_MAX,'b');
30  param.AddParamBool("absdiff","if set calculates abs difference else im1-im2",false, 'a');
31  param.AddParamBool("help","",false, 'h');
32  param.AddParamBool("grey", "converts images to grey", false,'g');
33  param.AddParamBool("zeroexclude", "do not use zero values", false,'z');
34  param.AddParamBool("exclude_negative", "do not use negative values", false,'n');
35  int next = param.ParseCommandLine(argc, argv);
36 
38 
39  if (argc<3 || *param.GetParamBool("help")){
40  param.Usage();
41  return -1;
42  }
43 
44  ImageBase im1, im2, diff;
45  if (ImageIO::Load(argv[next], im1)!=0){
46  BIASERR("error loading "<<argv[next]);
47  return -1;
48  }
49  if (ImageIO::Load(argv[next+1], im2)!=0){
50  BIASERR("error loading "<<argv[next+1]);
51  return -1;
52  }
53 
54  if (!im1.SamePixelAndChannelCount(im2) ||
55  im1.GetWidth() != im2.GetWidth() ||
56  im1.GetHeight() != im2.GetHeight()){
57  BIASERR("image sizes do not match");
58  return -2;
59  }
60 
61  if (*param.GetParamBool("grey")){
62  ImageBase tmp;
63  ImageConvert::ToGrey(im1, tmp);
64  im1 = tmp;
65  ImageConvert::ToGrey(im2, tmp);
66  im2 = tmp;
67  }
68 
69  int border = *param.GetParamInt("border");
70  bool zeroExclude = *param.GetParamBool("zeroexclude");
71  bool excludeNegative = *param.GetParamBool("exclude_negative");
72 
73  string newfilename;
74  string fn1,fn2;
75  fn1 = argv[next];
76  fn2 = argv[next+1];
77  fn1 = FileHandling::Basename(fn1); //fn1.substr(0,fn1.rfind('.'));
78  fn2 = FileHandling::Basename(fn2); //fn2.substr(0,fn2.rfind('.'));
79  newfilename = fn1+'_'+fn2+"_diff";
80 
81  im1.SetROI(border, border, im1.GetWidth()-border, im1.GetHeight()-border);
82  im2.SetROI(border, border, im2.GetWidth()-border, im2.GetHeight()-border);
83  BIASASSERT(im1.GetWidth()==im2.GetWidth());
84  BIASASSERT(im1.GetHeight()==im2.GetHeight());
85 
86  Image<float> fim1, fim2, fdiff;
87  Image<unsigned char> ucim1, ucim2, ucdiff;
88  float fmin, fmax;
89  unsigned char ucmin, ucmax;
90 
91  switch (im1.GetStorageType()){
93  ucim1=im1;
94  ucim2=im2;
95 
96  if (zeroExclude) {
97  unsigned char **p1 = ucim1.GetImageDataArray();
98  unsigned char **p2 = ucim2.GetImageDataArray();
99  const unsigned int channels = ucim1.GetChannelCount();
100  // make consistent zeros, for inspection of rendered depth maps
101  for (unsigned int y=0; y< ucim1.GetHeight(); y++) {
102  for (unsigned int x=0; x< ucim1.GetWidth(); x++) {
103  for (unsigned int c=0; c< ucim1.GetChannelCount(); c++) {
104  if (p2[y][channels*x+c]==0 || p1[y][channels*x+c]==0)
105  p2[y][channels*x+c] = p1[y][channels*x+c] = 0;
106  }
107  }
108  }
109  }
110  if(*param.GetParamBool("absdiff")){
111  ucdiff.AbsDiff(ucim1, ucim2);
112  } else {
115  fim1 -= fim2;
116  fim1.ScaleShiftBetween(0.0,255.0);
118  }
119 
120  //if (ImageIO::Save(newfilename, ucdiff)!=0){
121  if (ImageIO::Save(newfilename, ucdiff)!=0){
122  cerr << "error writing diff.mip "<<endl;
123  }
124  ucdiff.GetMinMaxPixelValue(ucmin, ucmax);
125  cerr << "differences between "<<(int)ucmin<<" and "<<(int)ucmax<<endl;
126  if (ucdiff.ScaleShiftBetween(0, 255)!=0){
127  BIASERR("error scaling image");
128  }
129  //if (ImageIO::Save(newfilename+"-scaled", ucdiff)!=0){
130  if (ImageIO::Save(newfilename+"-scaled", ucdiff)!=0){
131  cerr << "error writing diff-scaled.mip "<<endl;
132  }
133  break;
134  case ImageBase::ST_float:
135  fim1=im1;
136  fim2=im2;
137 
138  if (zeroExclude || excludeNegative) {
139  float **p1 = fim1.GetImageDataArray();
140  float **p2 = fim2.GetImageDataArray();
141  // make consistent zeros, for inspection of rendered depth maps
142  for (unsigned int y=0; y< fim1.GetHeight(); y++) {
143  for (unsigned int x=0; x< fim1.GetWidth(); x++) {
144  if (excludeNegative){
145  if (p2[y][x]<=0.0 || p1[y][x]<=0.0){
146  p2[y][x] = p1[y][x] = 0;
147  }
148  } else {
149  if (p2[y][x]==0 || p1[y][x]==0){
150  p2[y][x] = p1[y][x] = 0;
151  }
152  }
153  }
154  }
155  }
156  if(*param.GetParamBool("absdiff"))
157  fdiff.AbsDiff(fim1, fim2);
158  else
159  {
160  fdiff = fim1 - fim2;
161  }
162 
163  //if (ImageIO::Save(newfilename, fdiff)!=0){
164  if (ImageIO::Save(newfilename, fdiff)!=0){
165  cerr << "error writing diff.mip "<<endl;
166  }
167  fdiff.GetMinMaxPixelValue(fmin, fmax);
168  cerr << "differences between "<<fmin<<" and "<<fmax<<endl;
169  break;
170  default:
171  cerr << "unknown storage type\n";
172  break;
173  }
174 
175  return 0;
176 }
void DisableDestructorWarning()
Uses this just before end of your program to avoid error from destructor.
Definition: Param.hh:513
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
float image storage type
Definition: ImageBase.hh:118
void AbsDiff(const Image< StorageType > &im1, const Image< StorageType > &im2)
(*this) = | im1 - im2 | sets this as the absolute difference between two arg images ...
void GetMinMaxPixelValue(StorageType &min, StorageType &max, unsigned short int channel=0, unsigned int *mincoo=NULL, unsigned int *maxcoo=NULL) const
returns the minimal and maximal pixel value in channel only Finds minimum and maximum pixel value in ...
Definition: Image.cpp:802
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
bool * GetParamBool(const std::string &name) const
Definition: Param.cpp:633
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
static std::string Basename(const std::string &fullname)
Get file base name without path from given path and filename.
void ** GetImageDataArray() const
Get an array of pointers to image data.
Definition: ImageBase.hh:305
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int * GetParamInt(const std::string &name) const
Definition: Param.cpp:618
unsigned int GetHeight() const
Definition: ImageBase.hh:319
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same &quot;size&quot; as Image of other type
Definition: ImageBase.hh:73
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
int SetROI(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
deprecated, use SetROICorners()
Definition: ImageBase.cpp:1033
This class Param provides generic support for parameters.
Definition: Param.hh:231
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey