Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleHSVConversion.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 ExampleHSVConversion.cpp
27  @relates ImageConvert
28  @brief Example converting an image to/from HSV colormodel
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 // must be first:
34 //#include <Base/Common/LeakChecking.h>
35 
36 
37 #include <iostream>
38 
39 #include <Base/Image/ImageBase.hh>
40 #include <Base/Image/ImageConvert.hh>
41 #include <Base/Image/ImageIO.hh>
42 
43 using namespace BIAS;
44 using namespace std;
45 
46 int main()
47 {
48  unsigned int size=800;
49  Image<unsigned char> im(size, size, 3);
50  im.SetColorModel(ImageBase::CM_RGB);
51  unsigned int block_field_size=size/4;
52  unsigned int c=0, r=0;
53  unsigned char **ida=im.GetImageDataArray();
54  cerr<<"R=255, G=0, B=0-255 | ";
55  float blue=0;
56  for (c=0;c<block_field_size;c++) {
57  for (r=0; r< (block_field_size / 2) ; r++) {
58  ida[r][c*3 + 0]= 255;
59  ida[r][c*3 + 1]= 0;
60  ida[r][c*3 + 2]= (unsigned char)blue;
61  }
62  blue+=255.0f / (float)block_field_size;
63  }
64 
65  cerr<<"R=255, G=0-255, B=255 | ";
66  float green=0;
67  for (c=block_field_size+1;c<(2*block_field_size);c++) {
68  for (r=0; r< (block_field_size / 2) ; r++) {
69  ida[r][c*3 + 0]= 255;
70  ida[r][c*3 + 1]= (unsigned char)green;
71  ida[r][c*3 + 2]= 255;
72  }
73  green+=255.0f / (float)block_field_size;
74  }
75 
76  cerr<<"R=255, G=255, B=255-0 | ";
77  blue=255;
78  for (c=2*block_field_size+1;c<(3*block_field_size);c++) {
79  for (r=0; r< (block_field_size / 2) ; r++) {
80  ida[r][c*3 + 0]= 255;
81  ida[r][c*3 + 1]= 255;
82  ida[r][c*3 + 2]= (unsigned char)blue;
83  }
84  blue-=255.0f / (float)block_field_size;
85  }
86 
87  cerr<<"R=255, G=255, B=255-0"<<endl;
88  green=255;
89  for (c=3*block_field_size+1; c<size;c++) {
90  for (r=0; r< (block_field_size / 2) ; r++) {
91  ida[r][c*3 + 0]= 255;
92  ida[r][c*3 + 1]= (unsigned char)green;
93  ida[r][c*3 + 2]= 0;
94  }
95  green-=255.0f / (float)block_field_size;
96  }
97  cerr<<"--------------------------------------------------------------"<<endl;
98  cerr<<"R=0, G=0-255, B=255 | ";
99  green=0;
100  for (c=0;c<block_field_size;c++) {
101  for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
102  ida[r][c*3 + 0]= 0;
103  ida[r][c*3 + 1]= (unsigned char)green;
104  ida[r][c*3 + 2]= 255;
105  }
106  green+=255.0f / (float)block_field_size;
107  }
108 
109  cerr<<"R=0, G=255, B=255-0 | ";
110  blue=0;
111  for (c=block_field_size+1;c<(2*block_field_size);c++) {
112  for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
113  ida[r][c*3 + 0]= 0;
114  ida[r][c*3 + 1]= 255;
115  ida[r][c*3 + 2]= (unsigned char)blue;
116  }
117  blue-=255.0f / (float)block_field_size;
118  }
119 
120  cerr<<"R=0-255, G=255-0, B=0 | ";
121  green=255;
122  float red=0;
123  for (c=2*block_field_size+1;c<(3*block_field_size);c++) {
124  for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
125  ida[r][c*3 + 0]= (unsigned char)red;
126  ida[r][c*3 + 1]= (unsigned char)green;
127  ida[r][c*3 + 2]= 0;
128  }
129  green-=255.0f / (float)block_field_size;
130  red+=255.0f / (float)block_field_size;
131  }
132 
133  cerr<<"R=255-0, G=0, B=0-255"<<endl;
134  red=255; blue=0;
135  for (c=3*block_field_size+1; c<size;c++) {
136  for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
137  ida[r][c*3 + 0]= (unsigned char)red;
138  ida[r][c*3 + 1]= 0;
139  ida[r][c*3 + 2]= 0;
140  }
141  red-=255.0f / (float)block_field_size;
142  blue+=255.0f / (float)block_field_size;
143  }
144 
145  Image<unsigned char> imageHSV(size,size,3);
147 
148  cerr<<"-------------------------------------------------------"<<endl;
149  cerr<<"corresonding HSV channels: ";
150  cerr<<"hue , saturation and value"<<endl;
151  // copy channels of the imageHSV into original
152  unsigned char **idaHSV=imageHSV.GetImageDataArray();
153  for (c=0;c<size;c++) {
154  for (r=0;r< block_field_size;r++) {
155  // Hue
156  ida[r/2+block_field_size+20][c*3 + 0] = idaHSV[r][c*3 + 0];
157  ida[r/2+block_field_size+20][c*3 + 1] = idaHSV[r][c*3 + 0];
158  ida[r/2+block_field_size+20][c*3 + 2] = idaHSV[r][c*3 + 0];
159  // Saturation
160  ida[r/2+2*block_field_size+20][c*3 + 0] = idaHSV[r][c*3 + 1];
161  ida[r/2+2*block_field_size+20][c*3 + 1] = idaHSV[r][c*3 + 1];
162  ida[r/2+2*block_field_size+20][c*3 + 2] = idaHSV[r][c*3 + 1];
163  // Value
164  ida[r/2+3*block_field_size+20][c*3 + 0] = idaHSV[r][c*3 + 2];
165  ida[r/2+3*block_field_size+20][c*3 + 1] = idaHSV[r][c*3 + 2];
166  ida[r/2+3*block_field_size+20][c*3 + 2] = idaHSV[r][c*3 + 2];
167  }
168  }
169 
170 
171  //if (ImageIO::Save("imageHSVExample", im)!=0){
172  if (ImageIO::Save("imageHSVExample", im)!=0){
173  BIASERR("error writing image ");
174  return -2;
175  }
176 
177  if (ImageIO::Save("imageHSVExample", im, ImageIO::FF_ppm)!=0){
178  BIASERR("error exporting image ");
179  return -2;
180  }
181  cerr<<"image data written to file: imageHSVExample. ppm/mip"<<endl;
182  return 0;
183 }
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
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
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
HSV, 3 channels, order: hue, sat , value.
Definition: ImageBase.hh:138