Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleROI.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  @example ExampleROI.cpp
27  @relates ImageBase, ROI
28  @brief Example for usage of Image ROI (Region Of Interest)
29  @ingroup g_examples
30  @author MIP
31 */
32 #include <iostream>
33 
34 #include <Base/Image/ImageBase.hh>
35 #include <Base/Image/ImageConvert.hh>
36 #include <Base/Image/ImageIO.hh>
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 int main(int argc, char *argv[])
42 {
44 
45 
46  if (argc<=1 || ImageIO::Load(argv[1], im)!=0){
47  cerr << argv[0] << " <image>\n";
48  im.Init(100, 75, 1);
50  }
51 
52  int w=im.GetWidth();
53  int h=im.GetHeight();
54  im.SetROICorners(w>>2, h>>2, w-(w>>2), h-(h>>2));
55 
56  cout << "zero image outside of ("<<(w>>2)<<", "<<(h>>2)<<") <--> ("
57  <<(w-(w>>2))<<", "<<(h-(h>>2))<<")\n";
58 
59  im.SetOutsideROIZero();
60  //ImageIO::Save("zeroed_corners.mip", im);
61  ImageIO::Save("zeroed_corners.mip", im);
62 
63  cout << "roi: "<<*im.GetROI()<<endl;
64  if (w>3 && h>3){
65  for (int x=w-3; x<w; x++)
66  for (int y=h-3; y<h; y++)
67  im.GetROI()->SetMask(x, y, false);
68 
69  } else {
70  cerr << "image too small\n";
71  }
72 
74  im.SetOutsideROIZero();
75  //ImageIO::Save("zeroed_mask.mip", im);
76  ImageIO::Save("zeroed_mask.mip", im);
77 
78  // Convert ROI representation from mask to point vector
80  cout << "im: "<<*im.GetROI() << endl;
81 
83  im.SetOutsideROIZero();
84  //ImageIO::Save("zeroed_vector.mip", im);
85  ImageIO::Save("zeroed_vector.mip", im);
86 
87  //ImageIO::Save("roi.mip", im);
88  ImageIO::Save("roi.mip", im);
90  //if (ImageIO::Load("roi.mip", im2)!=0){
91  if (ImageIO::Load("roi.mip", im2)!=0){
92  BIASERR("error loading image \"roi.mip\"");
93  return -1;
94  }
95 
96  cout << "im2: "<<*im2.GetROI()<<endl;
97 
98  vector<Position> vec;
99  vec.resize(3);
100 
101  vec[0]=Position(w>>1, h>>1);
102  vec[1]=Position(0, 0);
103  vec[2]=Position(w>>1, 0);
104 
105  im2.GetROI()->SetVector(vec);
106 
107  // Convert ROI representation back from point vector to mask
109 
110  cout << *im2.GetROI()<<endl;
111 
112  //if (ImageIO::Load("zeroed.mip", im2)!=0){
113  if (ImageIO::Load("zeroed.mip", im2)!=0){
114  BIASERR("error reading image \"zeroed.mip\"");
115  return -1;
116  }
117 
118  return 0;
119 }
stores valid/nvalid positions in image
Definition: ROI.hh:49
unsigned int GetWidth() const
Definition: ImageBase.hh:312
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void FillImageWithXValue()
fills image with value depending on x coordinate
Definition: Image.cpp:608
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
void SetOutsideROIZero()
sets all pixel not in ROI to zero
Definition: ImageBase.cpp:675
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
Definition: Image.cpp:421
int ConvertROIType(const enum ERoiType &type)
Generic conversion function from current ROI representation to the given ROI representation.
Definition: ROI.cpp:720
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
void SetMask(const unsigned &x, const unsigned &y, const bool val)
Direct access to the mask data.
Definition: ROI.cpp:478
void SetVector(std::vector< Position > &pos)
Sets MaskValid_=false and VectorValid_=true.
Definition: ROI.cpp:463
int SetROICorners(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
Definition: ImageBase.cpp:1048