Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleHSVConversion.cpp

Example converting an image to/from HSV colormodel

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleHSVConversion.cpp
@relates ImageConvert
@brief Example converting an image to/from HSV colormodel
@ingroup g_examples
@author MIP
*/
// must be first:
//#include <Base/Common/LeakChecking.h>
#include <iostream>
#include <Base/Image/ImageBase.hh>
#include <Base/Image/ImageConvert.hh>
#include <Base/Image/ImageIO.hh>
using namespace BIAS;
using namespace std;
int main()
{
unsigned int size=800;
Image<unsigned char> im(size, size, 3);
unsigned int block_field_size=size/4;
unsigned int c=0, r=0;
unsigned char **ida=im.GetImageDataArray();
cerr<<"R=255, G=0, B=0-255 | ";
float blue=0;
for (c=0;c<block_field_size;c++) {
for (r=0; r< (block_field_size / 2) ; r++) {
ida[r][c*3 + 0]= 255;
ida[r][c*3 + 1]= 0;
ida[r][c*3 + 2]= (unsigned char)blue;
}
blue+=255.0f / (float)block_field_size;
}
cerr<<"R=255, G=0-255, B=255 | ";
float green=0;
for (c=block_field_size+1;c<(2*block_field_size);c++) {
for (r=0; r< (block_field_size / 2) ; r++) {
ida[r][c*3 + 0]= 255;
ida[r][c*3 + 1]= (unsigned char)green;
ida[r][c*3 + 2]= 255;
}
green+=255.0f / (float)block_field_size;
}
cerr<<"R=255, G=255, B=255-0 | ";
blue=255;
for (c=2*block_field_size+1;c<(3*block_field_size);c++) {
for (r=0; r< (block_field_size / 2) ; r++) {
ida[r][c*3 + 0]= 255;
ida[r][c*3 + 1]= 255;
ida[r][c*3 + 2]= (unsigned char)blue;
}
blue-=255.0f / (float)block_field_size;
}
cerr<<"R=255, G=255, B=255-0"<<endl;
green=255;
for (c=3*block_field_size+1; c<size;c++) {
for (r=0; r< (block_field_size / 2) ; r++) {
ida[r][c*3 + 0]= 255;
ida[r][c*3 + 1]= (unsigned char)green;
ida[r][c*3 + 2]= 0;
}
green-=255.0f / (float)block_field_size;
}
cerr<<"--------------------------------------------------------------"<<endl;
cerr<<"R=0, G=0-255, B=255 | ";
green=0;
for (c=0;c<block_field_size;c++) {
for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
ida[r][c*3 + 0]= 0;
ida[r][c*3 + 1]= (unsigned char)green;
ida[r][c*3 + 2]= 255;
}
green+=255.0f / (float)block_field_size;
}
cerr<<"R=0, G=255, B=255-0 | ";
blue=0;
for (c=block_field_size+1;c<(2*block_field_size);c++) {
for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
ida[r][c*3 + 0]= 0;
ida[r][c*3 + 1]= 255;
ida[r][c*3 + 2]= (unsigned char)blue;
}
blue-=255.0f / (float)block_field_size;
}
cerr<<"R=0-255, G=255-0, B=0 | ";
green=255;
float red=0;
for (c=2*block_field_size+1;c<(3*block_field_size);c++) {
for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
ida[r][c*3 + 0]= (unsigned char)red;
ida[r][c*3 + 1]= (unsigned char)green;
ida[r][c*3 + 2]= 0;
}
green-=255.0f / (float)block_field_size;
red+=255.0f / (float)block_field_size;
}
cerr<<"R=255-0, G=0, B=0-255"<<endl;
red=255; blue=0;
for (c=3*block_field_size+1; c<size;c++) {
for (r=block_field_size / 2 +1; r< block_field_size ; r++) {
ida[r][c*3 + 0]= (unsigned char)red;
ida[r][c*3 + 1]= 0;
ida[r][c*3 + 2]= 0;
}
red-=255.0f / (float)block_field_size;
blue+=255.0f / (float)block_field_size;
}
Image<unsigned char> imageHSV(size,size,3);
cerr<<"-------------------------------------------------------"<<endl;
cerr<<"corresonding HSV channels: ";
cerr<<"hue , saturation and value"<<endl;
// copy channels of the imageHSV into original
unsigned char **idaHSV=imageHSV.GetImageDataArray();
for (c=0;c<size;c++) {
for (r=0;r< block_field_size;r++) {
// Hue
ida[r/2+block_field_size+20][c*3 + 0] = idaHSV[r][c*3 + 0];
ida[r/2+block_field_size+20][c*3 + 1] = idaHSV[r][c*3 + 0];
ida[r/2+block_field_size+20][c*3 + 2] = idaHSV[r][c*3 + 0];
// Saturation
ida[r/2+2*block_field_size+20][c*3 + 0] = idaHSV[r][c*3 + 1];
ida[r/2+2*block_field_size+20][c*3 + 1] = idaHSV[r][c*3 + 1];
ida[r/2+2*block_field_size+20][c*3 + 2] = idaHSV[r][c*3 + 1];
// Value
ida[r/2+3*block_field_size+20][c*3 + 0] = idaHSV[r][c*3 + 2];
ida[r/2+3*block_field_size+20][c*3 + 1] = idaHSV[r][c*3 + 2];
ida[r/2+3*block_field_size+20][c*3 + 2] = idaHSV[r][c*3 + 2];
}
}
//if (ImageIO::Save("imageHSVExample", im)!=0){
if (ImageIO::Save("imageHSVExample", im)!=0){
BIASERR("error writing image ");
return -2;
}
if (ImageIO::Save("imageHSVExample", im, ImageIO::FF_ppm)!=0){
BIASERR("error exporting image ");
return -2;
}
cerr<<"image data written to file: imageHSVExample. ppm/mip"<<endl;
return 0;
}