00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #if defined INVT_HAVE_QT3 || defined INVT_HAVE_QT4
00039 # define IMAGECOMPARE_USE_QT 1
00040 #endif
00041
00042 #include "Component/ModelManager.H"
00043 #include "Component/ModelOptionDef.H"
00044 #include "Component/ModelParam.H"
00045 #ifdef IMAGECOMPARE_USE_QT
00046 # ifdef INVT_HAVE_QT4
00047 # include "GUI/QtDisplayStream4.H"
00048 # else
00049 # include "GUI/QtDisplayStream.H"
00050 # endif
00051 #else
00052 # include "GUI/ImageDisplayStream.H"
00053 #endif
00054 #include "Image/ColorOps.H"
00055 #include "Image/MathOps.H"
00056 #include "Image/Image.H"
00057 #include "Image/Pixels.H"
00058 #include "Raster/GenericFrame.H"
00059 #include "Raster/Raster.H"
00060 #include "Transport/FrameInfo.H"
00061
00062 #include <cstdio>
00063 #include <cstdlib>
00064 #include <unistd.h>
00065 #include <vector>
00066
00067 static Image<float> rectify(const Image<float>& x)
00068 {
00069 Image<float> y(x);
00070 inplaceRectify(y);
00071 return y;
00072 }
00073
00074 static Image<PixRGB<float> > rectifyRgb(const Image<PixRGB<float> >& x)
00075 {
00076 Image<float> r, g, b;
00077 getComponents(x, r, g, b);
00078 return makeRGB(rectify(r), rectify(g), rectify(b));
00079 }
00080
00081 template <class T>
00082 static Image<byte> findNonZero(const Image<T>& x)
00083 {
00084 Image<byte> result(x.getDims(), NO_INIT);
00085
00086 typename Image<T>::const_iterator sptr = x.begin();
00087 Image<byte>::iterator dptr = result.beginw();
00088 Image<byte>::iterator stop = result.endw();
00089
00090 const T zero = T();
00091
00092 while (dptr != stop) { *dptr++ = (*sptr++ == zero) ? 0 : 255; }
00093
00094 return result;
00095 }
00096
00097 static const ModelOptionDef OPT_Compare =
00098 { MODOPT_FLAG, "Compare", &MOC_GENERAL, OPTEXP_CORE,
00099 "Whether to show comparisons between images (otherwise, just the images themselves will be shown)",
00100 "compare", '\0', "", "true" };
00101
00102 int main(int argc, char** argv)
00103 {
00104 MYLOGVERB = LOG_CRIT;
00105
00106 ModelManager mgr("imageCompare");
00107 OModelParam<bool> docompare(&OPT_Compare, &mgr);
00108
00109 #ifdef IMAGECOMPARE_USE_QT
00110 nub::ref<FrameOstream> ofs(new QtDisplayStream(mgr));
00111 #else
00112 nub::ref<FrameOstream> ofs(new ImageDisplayStream(mgr));
00113 #endif
00114 mgr.addSubComponent(ofs);
00115
00116 if (mgr.parseCommandLine(argc, argv, "image1 [image2 [image3 ...]]", 1, -1) == false)
00117 return(1);
00118
00119 mgr.start();
00120
00121 std::vector<GenericFrame> im;
00122 std::vector<std::string> imname;
00123
00124 for (uint i = 0; i < mgr.numExtraArgs(); ++i)
00125 {
00126 imname.push_back(mgr.getExtraArg(i));
00127
00128 im.push_back(Raster::ReadFrame(imname.back()));
00129
00130 im.back().setFloatFlags(FLOAT_NORM_0_255);
00131
00132 ofs->writeFrame(im.back(), sformat("image%u",i+1), FrameInfo(imname.back(), SRC_POS));
00133 }
00134
00135 if (docompare.getVal())
00136 for (size_t i = 0; i < im.size(); ++i)
00137 {
00138 const GenericFrame im1 = im[i];
00139
00140 for (size_t j = i+1; j < im.size(); ++j)
00141 {
00142 const GenericFrame im2 = im[j];
00143
00144 if (im1.getDims() != im2.getDims())
00145 continue;
00146
00147 GenericFrame im1_greater_than_im2, im2_greater_than_im1, im1_neq_im2;
00148
00149 switch (im1.nativeType())
00150 {
00151 case GenericFrame::NONE:
00152 case GenericFrame::RGB_U8:
00153 case GenericFrame::RGBD:
00154 case GenericFrame::RGB_F32:
00155 case GenericFrame::VIDEO:
00156 im1_greater_than_im2 = GenericFrame(rectifyRgb(im1.asRgbF32() - im2.asRgbF32()), FLOAT_NORM_0_255);
00157 im2_greater_than_im1 = GenericFrame(rectifyRgb(im2.asRgbF32() - im1.asRgbF32()), FLOAT_NORM_0_255);
00158 im1_neq_im2 = GenericFrame(findNonZero(im1.asRgbF32() - im2.asRgbF32()));
00159 break;
00160
00161 case GenericFrame::GRAY_U8:
00162 case GenericFrame::GRAY_F32:
00163 im1_greater_than_im2 = GenericFrame(rectify(im1.asGrayF32() - im2.asGrayF32()), FLOAT_NORM_0_255);
00164 im2_greater_than_im1 = GenericFrame(rectify(im2.asGrayF32() - im1.asGrayF32()), FLOAT_NORM_0_255);
00165 im1_neq_im2 = GenericFrame(findNonZero(im1.asGrayF32() - im2.asGrayF32()));
00166 break;
00167
00168 case GenericFrame::RGB_U16: break;
00169 case GenericFrame::GRAY_U16: break;
00170 }
00171
00172 ofs->writeFrame(im1_greater_than_im2, sformat("image%"ZU">image%"ZU"", i+1, j+1),
00173 FrameInfo(imname[i] + " > " + imname[j], SRC_POS));
00174 ofs->writeFrame(im2_greater_than_im1, sformat("image%"ZU">image%"ZU"", j+1, i+1),
00175 FrameInfo(imname[j] + " > " + imname[i], SRC_POS));
00176 ofs->writeFrame(im1_neq_im2, sformat("image%"ZU"!=image%"ZU"", i+1, j+1),
00177 FrameInfo(imname[i] + " != " + imname[j], SRC_POS));
00178 }
00179 }
00180
00181 while (!ofs->isVoid()) sleep(1);
00182
00183 mgr.stop();
00184 }
00185
00186
00187
00188
00189
00190
00191