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

This little example demonstrates the usage of color normalised cross correlation

Author
woelk
/*
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 ExampleColorNCC.cpp
@brief This little example demonstrates the usage of color normalised cross correlation
@ingroup g_examples
@author woelk
*/
#include <iostream>
#include <iomanip>
#include <Utils/Param.hh>
#include <Base/Image/ImageBase.hh>
#include <Base/Image/ImageConvert.hh>
#include <Base/Image/ImageIO.hh>
#include <Matcher2D/RegionMatcher.hh>
using namespace BIAS;
using namespace std;
int demo(unsigned int size) {
Image<unsigned char> im(size, size, 3);
unsigned int chess_field_size=size/8;
unsigned int x=0, y=0, c=0, r=0;
unsigned char **ida=im.GetImageDataArray();
unsigned char red=89,blue=127;
for (x=0;x<im.GetWidth(); x+= chess_field_size) {
// color = (color==0) ? (unsigned char)255 : (unsigned char)0;
if (red==127) red=89; else red=127;
if (blue==127) blue=25; else blue=127;
for (y=0;y<im.GetHeight(); y+= chess_field_size) {
if (red==127) red=89; else red=127;
if (blue==127) blue=25; else blue=127;
//color = (color==0) ? (unsigned char)255 : (unsigned char)0;
for (r=0; r<chess_field_size; r++)
for (c=0; c<chess_field_size; c++){
ida[y+r][(x+c)*3 + 0]=red;
ida[y+r][(x+c)*3 + 1]=255;
ida[y+r][(x+c)*3 + 2]=blue;
}
}
}
// grey level ncc
Image<unsigned char> imGrey(size,size,1);
int n=1;
double result=0;
//matcher.SetDebugLevel(D_REGION_MATCHER_COLORNCC);
unsigned int p1[2], p2[2];
p1[0]=chess_field_size; p1[1]= chess_field_size;
for (x=chess_field_size-n;x<=chess_field_size+n;x++) {
for (y=chess_field_size-n;y<=chess_field_size+n;y++) {
p2[0]=x; p2[1]=y;
matcher.NCC(p1, p2, (const unsigned char **)imGrey.GetImageDataArray(),
(const unsigned char **)imGrey.GetImageDataArray(),
3, result);
cout<<"["<<p2[0]<<","<<p2[1]<<"]:"<<setprecision(2)<<result<<" ";
}
cout<<endl;
}
cout<<endl;
Image<unsigned char> imHSV(size,size,3);
for (x=chess_field_size-n;x<=chess_field_size+n;x++) {
for (y=chess_field_size-n;y<=chess_field_size+n;y++) {
p2[0]=x; p2[1]=y;
matcher.CNCC(p1, p2, (const unsigned char **)imHSV.GetImageDataArray(),
(const unsigned char **)imHSV.GetImageDataArray(),
3, result);
cout<<"["<<p2[0]<<","<<p2[1]<<"]:"<<setprecision(2)<<result<<" ";
}
cout<<endl;
}
//if (ImageIO::Save("chessboard", im)!=0){
if (ImageIO::Save("chessboard", im)!=0){
BIASERR("error writing image ");
return -2;
}
//if (ImageIO::Save("chessboardGrey", imGrey)!=0){
if (ImageIO::Save("chessboardGrey", imGrey)!=0){
BIASERR("error writing image ");
return -2;
}
if (ImageIO::Save("chessboard", im, ImageIO::FF_ppm)!=0){
BIASERR("error exporting image ");
return -2;
}
return 0;
}
int main(int argc, char *argv[])
{
BIAS::Param params;
params.AddParamBool("chessBoard",
"only writes colored chessboard image",false);
params.AddParamInt("winSize","the odd half window size of the CNCC operator",
5,3,1001,'w');
params.AddParamVecDbl("pos","the position in the image where the CNCC"
" is applied, if only a single image is given",
"10 10");
params.AddParamString("image1","the image file name","",'i');
params.AddParamString("image2","the image file name of an optional"
" second image","",'I');
params.ParseCommandLine(argc,argv);
if ((argc<=1) || params.CheckParam("help")) {
cout<<argv[0]<<" [options] <file1> <file2>"<<endl;
cout<<"CNCC is applied to image1 at the given position(defualt is 10,10)."
<<endl<<" if a second image is given CNCC is applied at every pixel in"
" both images at the same position"<<endl;
params.Usage();
exit(0);
}
unsigned int size=800;
if (*params.GetParamBool("chessBoard"))
exit(demo(size));
if (ImageIO::Load(*params.GetParamString("image1"),im1)!=0) {
BIASERR("error loading image "<<*params.GetParamString("image1"));
exit (-1);
}
// if (ImageIO::Load(argv[2],im2)!=0) {
// BIASERR("error loading image "<<argv[2]);
// exit (-1);
// }
Image<unsigned char> imGrey1, imHSV1(im1.GetWidth(),im1.GetHeight(),3),
imGrey2, imHSV2(im2.GetWidth(),im2.GetHeight(),3), imRGB1(im1),imRGB2(im2);
Image<unsigned char> imgGreyRes(imGrey1),imgColorRes(imGrey1);
imgColorRes.FillImageWithConstValue((unsigned char)0);
// copmute NCC for the whole image (except border)
//matcher.SetDebugLevel(D_REGION_MATCHER_COLORNCC);
unsigned int winSizeNCC= *params.GetParamInt("winSize");
unsigned char **idaRes=imgColorRes.GetImageDataArray();
BIAS::Vector<double> pos= *params.GetParamVecDbl("pos");
unsigned int x,y,p1[2], p2[2];
double result=0;
if (params.GetParamString("image2")->length()>0) {// second image is given
if (ImageIO::Load(*params.GetParamString("image2"),im2)!=0) {
BIASERR("error loading image "<<*params.GetParamString("image2"));
exit (-1);
}
for (y=winSizeNCC;y<imHSV1.GetHeight()-winSizeNCC-1; y++) {
std::cerr << "Processing ColorNCC " <<
(int)((double)(y-winSizeNCC) /
(double)(imHSV1.GetHeight()-winSizeNCC-1)*100) << "%" << '\r';
for (x=winSizeNCC;x<imHSV1.GetWidth()-winSizeNCC-1; x++) {
p2[0]=x; p2[1]=y;
matcher.CNCC(p2,p2,
(const unsigned char**)imHSV1.GetImageDataArray(),
(const unsigned char**)imHSV2.GetImageDataArray(),
winSizeNCC, result);
if (result<0) result=0;
idaRes[y][x]=(unsigned char)(result*255.0);
}
}
//if (ImageIO::Save("resultCNCC",imgColorRes)!=0){
if (ImageIO::Save("resultCNCC",imgColorRes)!=0){
BIASERR("error writing image resultCNCC");
return -2;
}
cout<<"result of Color NCC comparison bewtween image"<<
*params.GetParamString("image1") <<" and "<<
*params.GetParamString("image1") <<" written to"<<
" resultCNCC.mip"<<endl;
// now grey vlaue NCC
for (y=winSizeNCC;y<imHSV1.GetHeight()-winSizeNCC-1; y++) {
std::cerr << "Processing Grey value NCC " <<
(int)((double)(y-winSizeNCC) /
(double)(imHSV1.GetHeight()-winSizeNCC-1)*100) << "%" << '\r';
for (x=winSizeNCC;x<imHSV1.GetWidth()-winSizeNCC-1; x++) {
p2[0]=x; p2[1]=y;
matcher.NCC(p2,p2,
(const unsigned char**)imGrey1.GetImageDataArray(),
(const unsigned char**)imGrey2.GetImageDataArray(),
winSizeNCC, result);
if (result<0) result=0;
idaRes[y][x]=(unsigned char)(result*255.0);
}
}
//if (ImageIO::Save("resultNCC",imgColorRes)!=0){
if (ImageIO::Save("resultNCC",imgColorRes)!=0){
BIASERR("error writing image resultNCC");
return -2;
}
cout<<"result of grey value NCC comparison bewtween image"<<
*params.GetParamString("image1") <<" and "<<
*params.GetParamString("image1") <<" written to"<<
" resultNCC.mip"<<endl;
} else { ///////////////////////////////////////// one image case
p1[0]=(unsigned int)pos[0]; p1[1]=(unsigned int)pos[1];
for (y=winSizeNCC;y<imHSV1.GetHeight()-winSizeNCC-1; y++) {
std::cerr << "Processing ColorNCC " <<
(int)((double)(y-winSizeNCC) /
(double)(imHSV1.GetHeight()-winSizeNCC-1)*100) << "%" << '\r';
for (x=winSizeNCC;x<imHSV1.GetWidth()-winSizeNCC-1; x++) {
p2[0]=x; p2[1]=y;
matcher.CNCC(p1,p2,
(const unsigned char**)imHSV1.GetImageDataArray(),
(const unsigned char**)imHSV1.GetImageDataArray(),
winSizeNCC, result);
if (result<0) result=0;
idaRes[y][x]=(unsigned char)(result*255.0);
}
}
//if (ImageIO::Save("resultCNCC",imgColorRes)!=0){
if (ImageIO::Save("resultCNCC",imgColorRes)!=0){
BIASERR("error writing image resultCNCC");
return -2;
}
cout<<"result of Color NCC at "<<pos<<" written to resultCNCC.mip"<<endl;
// now grey vlaue NCC
for (y=winSizeNCC;y<imHSV1.GetHeight()-winSizeNCC-1; y++) {
std::cerr << "Processing Grey value NCC " <<
(int)((double)(y-winSizeNCC) /
(double)(imHSV1.GetHeight()-winSizeNCC-1)*100) << "%" << '\r';
for (x=winSizeNCC;x<imHSV1.GetWidth()-winSizeNCC-1; x++) {
p2[0]=x; p2[1]=y;
matcher.NCC(p1,p2,
(const unsigned char**)imGrey1.GetImageDataArray(),
(const unsigned char**)imGrey1.GetImageDataArray(),
winSizeNCC, result);
if (result<0) result=0;
idaRes[y][x]=(unsigned char)(result*255.0);
}
}
//if (ImageIO::Save("resultNCC",imgColorRes)!=0){
if (ImageIO::Save("resultNCC",imgColorRes)!=0){
BIASERR("error writing image resultNCC");
return -2;
}
cout<<"result of grey value NCC at "<<pos<<
" written to resultNCC.mip"<<endl;
}
// if (ImageIO::Save("imGrey1",imGrey1)!=0){
// BIASERR("error writing image imgColorRes");
// return -2;
// }
// winSizeNCC=3;
// idaRes=imgGreyRes.GetImageDataArray();
// for (x=winSizeNCC;x<imHSV1.GetWidth()-winSizeNCC; x++) {
// for (y=winSizeNCC;y<imHSV1.GetHeight()-winSizeNCC; y++) {
// p1[0]=p2[0]=x; p1[1]=p2[1]=y;
// matcher.NCC(p1, p2,imGrey1.GetImageDataArray(),
// imGrey2.GetImageDataArray(),
// winSizeNCC, result);
// if (result<0) result=0;
// idaRes[y][x]=(unsigned char)(result*255.0);
// }
// }
// if (ImageIO::Save("imgGreyRes",imgGreyRes )!=0){
// BIASERR("error writing image imgColorRes");
// return -2;
// }
return 0;
}