How to use Bayes Classification to Recognize the Image?
How to use Bayes classification to recognize the
image?
Introduction
If you want to compare how many images to match.
You can try the Bayes classification to help you measure similar degree. If
high similar will get high result value. Opposite, if low similar will get low
result value.
Equipment
Operation System: Microsoft Windows 7 (64 bit)
Development Utility: Microsoft Visual Studio 2010
Usage
// The height of
the image.
for ( int iY = 0; iY <
imageA->DibInfo->bmiHeader.biHeight; iY++ )
{
// The width
of the image.
for ( int iX = 0; iX <
imageA->DibInfo->bmiHeader.biWidth; iX++ )
{
// This
is the first image.
// The
index of the image. The bit depth is three, because we use 24 bits format.
lIDXA = ( iX * 3 ) + ( iY *
imageA->DibInfo->bmiHeader.biWidth * 3 );
// Get
the pixel of the blue channel.
byteRGB_BA =
imageA->DibArry[lIDXA+0];
// Get
the pixel of the green channel.
byteRGB_GA = imageA->DibArry[lIDXA+1];
// Get
the pixel of the red channel.
byteRGB_RA =
imageA->DibArry[lIDXA+2];
//
Transform an RGB color space to gray scale.
dobYUV_YA =
(0.299 * byteRGB_RA + 0.587 * byteRGB_GA + 0.114 * byteRGB_BA);
// We have to transform the image to grayscale.
// Set
the pixel of the blue channel.
imageA->DibArry[lIDXA+0] =
dobYUV_YA;
// Set
the pixel of the green channel.
imageA->DibArry[lIDXA+1] = dobYUV_YA;
// Set
the pixel of the red channel.
imageA->DibArry[lIDXA+2] =
dobYUV_YA;
// This
is the second image.
// The
index of the image. The bit depth is three, because we use 24 bits format.
lIDXB = ( iX * 3 ) + ( iY *
imageB->DibInfo->bmiHeader.biWidth * 3 );
// Get
the pixel of the blue channel.
byteRGB_BB =
imageB->DibArry[lIDXB+0];
// Get
the pixel of the green channel.
byteRGB_GB = imageB->DibArry[lIDXB+1];
// Get
the pixel of the red channel.
byteRGB_RB =
imageB->DibArry[lIDXB+2];
//
Transform an RGB color space to gray scale.
dobYUV_YB = (0.299 * byteRGB_RB +
0.587 * byteRGB_GB + 0.114 * byteRGB_BB);
// We
have to transform the image to grayscale.
// Set
the pixel of the blue channel.
imageB->DibArry[lIDXB+0] =
dobYUV_YB;
// Set
the pixel of the green channel.
imageB->DibArry[lIDXB+1] =
dobYUV_YB;
// Set
the pixel of the red channel.
imageB->DibArry[lIDXB+2] =
dobYUV_YB;
// This
is a third image that is a test sample image.
// The
index of the image. The bit depth is three, because we use 24 bits format.
lIDXC = ( iX * 3 ) + ( iY *
imageC->DibInfo->bmiHeader.biWidth * 3 );
// Get
the pixel of the blue channel.
byteRGB_BC =
imageC->DibArry[lIDXC+0];
// Get
the pixel of the green channel.
byteRGB_GC =
imageC->DibArry[lIDXC+1];
// Get
the pixel of the red channel.
byteRGB_RC =
imageC->DibArry[lIDXC+2];
//
Transform an RGB color space to gray scale.
dobYUV_YC = (0.299 * byteRGB_RC +
0.587 * byteRGB_GC + 0.114 * byteRGB_BC);
// We
have to transform the image to grayscale.
// Set
the pixel of the blue channel.
imageC->DibArry[lIDXC+0] =
dobYUV_YC;
// Set
the pixel of the green channel.
imageC->DibArry[lIDXC+1] = dobYUV_YC;
// Set
the pixel of the red channel.
imageC->DibArry[lIDXC+2] =
dobYUV_YC;
} // The
closing "The width of the image".
} // The closing
"The height of the image".
// The variable
is a counter.
int iCount
= 0;
// We have a
sample.
FPattern[0].number = 1;
// In this
section, we create image features.
// The height of
the image.
for ( int iY = 0; iY <
imageA->DibInfo->bmiHeader.biHeight; iY+=10 )
{
// The width of the image.
for ( int iX = 0; iX <
imageA->DibInfo->bmiHeader.biWidth; iX+=10 )
{
// The
variable is an amount that is more than a threshold of the count.
int
numof1 = 0;
// The
height of the 10 by 10 matrix.
for
( int iYY = iY; iYY < iY+10; iYY++ )
{
//
The width of the 10 by 10 matrix.
for
( int iXX = iX; iXX < iX+10; iXX++ )
{
//
The index of the 10 by 10 matrix.
lIDXA = ( iXX * 3 ) + ( iYY
* imageA->DibInfo->bmiHeader.biWidth * 3 );
//
Get the pixel of the blue channel.
//
We get a gray value because of the previous transform to gray scale.
byteRGB_BA =
imageA->DibArry[lIDXA+0];
//
If the pixel more than our threshold, it should add one.
if
( byteRGB_BA > 100 )
{
numof1 +=1;
}
else
{
numof1 +=0;
}
} //
The closing "The width of the 10 by 10 matrix".
} // The
closing "The height of the 10 by 10 matrix".
// Set the
feature value.
FPattern[0].feature[0][iCount] = (double)numof1/100;
// The
variable is feature counter.
iCount++;
} // The
closing "The width of the image".
} // The closing
"The height of the image".
// The variable
is a counter.
iCount = 0;
// We have a
sample.
FPattern[1].number = 1;
// In this
section, we create image features.
// The height of
the image.
for ( int iY = 0; iY <
imageB->DibInfo->bmiHeader.biHeight; iY+=10 )
{
// The width
of the image.
for ( int iX = 0; iX <
imageB->DibInfo->bmiHeader.biWidth; iX+=10 )
{
// The
variable is an amount that is more than a threshold of the count.
int
numof1 = 0;
// The height of the 10 by 10 matrix.
for
( int iYY = iY; iYY < iY+10; iYY++ )
{
//
The width of the 10 by 10 matrix.
for
( int iXX = iX; iXX < iX+10; iXX++ )
{
// The index
of the 10 by 10 matrix.
lIDXB = ( iXX * 3 ) + ( iYY
* imageB->DibInfo->bmiHeader.biWidth * 3 );
//
Get the pixel of the blue channel.
//
We get a gray value because of the previous transform to gray scale.
byteRGB_BB =
imageB->DibArry[lIDXB+0];
//
If the pixel more than our threshold, it should add one.
if
( byteRGB_BB > 100 )
{
numof1 +=1;
}
else
{
numof1 +=0;
}
} //
The closing "The width of the 10 by 10 matrix".
} // The
closing "The height of the 10 by 10 matrix".
// Set
the feature value.
FPattern[1].feature[0][iCount] = (double)numof1/100;
// The
variable is feature counter.
iCount++;
} // The
closing "The width of the image".
} // The closing
"The height of the image".
// The variable
is a counter.
iCount = 0;
// In this
section, we create image features.
// The height of
the image.
for ( int iY = 0; iY <
imageC->DibInfo->bmiHeader.biHeight; iY+=10 )
{
// The width
of the image.
for ( int iX = 0; iX <
imageC->DibInfo->bmiHeader.biWidth; iX+=10 )
{
// The
variable is an amount that is more than a threshold of the count.
int
numof1 = 0;
// The
height of the 10 by 10 matrix.
for
( int iYY = iY; iYY < iY+10; iYY++ )
{
//
The width of the 10 by 10 matrix.
for
( int iXX = iX; iXX < iX+10; iXX++ )
{
//
The index of the 10 by 10 matrix.
lIDXC = ( iXX * 3 ) + ( iYY
* imageC->DibInfo->bmiHeader.biWidth * 3 );
//
Get the pixel of the blue channel.
//
We get a gray value because of the previous transform to gray scale.
byteRGB_BC =
imageC->DibArry[lIDXC+0];
//
If the pixel more than our threshold, it should add one.
if
( byteRGB_BC > 100 )
{
numof1 +=1;
}
else
{
numof1 +=0;
}
} //
The closing "The width of the 10 by 10 matrix".
} // The
closing "The height of the 10 by 10 matrix".
// Set the
feature value.
testsample[iCount] = (double)numof1/100;
// The
variable is feature counter.
iCount++;
} // The
closing "The width of the image".
} // The closing
"The height of the image".
// The prior probability, is the
initial degree of belief in wj. P (wj)=Nj/N
double
Pw[2] = {0};
// Pj(wi) wi: wi Classification. j: j of number feature.
double
P[2][25] = {0};
// P(X|wj) the
conditional probability or likelihood, is the degree of belief in X, given
that the proposition wj is true.
double
PXw[2] = {0};
// P(wj|X) the posterior
probability, is the probability for wj after taking into account X
for and against wj.
double
PwX[2] = {0};
// This variable is temporal value.
int iI = 0;
int iJ = 0;
int iK = 0;
// The prior probability section
// There are two samples.
int
n[2];
// The amount of samples.
int N =
0;
// There are two samples.
for ( iI =
0; iI < 2; iI++ )
{
// Each sample of the amount
n[iI] = FPattern[iI].number;
// The amount of samples
N += n[iI];
}
// There are two samples.
for ( iI =
0; iI < 2; iI++ )
{
// The prior
probability.
Pw[iI] = (double)n[iI]
/ (double)N;
}
// The conditional probability section.
// There are two samples.
for ( iI =
0; iI < 2; iI++ )
{
// There are twenty-five feature in
the array.
for (
iJ = 0; iJ < 25; iJ++ )
{
// The amount of features.
int
numof1 = 0;
// Review twenty-five feature.
for
( iK = 0; iK < FPattern[iI].number; iK++ )
{
// If the feature value more than 0.5, it should add one.
Otherwise, it should add zero.
numof1 +=
FPattern[iI].feature[iK][iJ]>0.5?1:0;
}
// Calculate the future value.
P[iI][iJ] = (double)(numof1+1) / (double)(n[iI]+2);
}
}
// There are two samples.
for ( iI =
0; iI < 2; iI++ )
{
// The
conditional probability set up 1.
double
p = 1;
// Review twenty-five feature.
for (
iJ = 0; iJ < 25; iJ++ )
{
// Calculate the conditional
probability.
p *= (testsample[iJ] >
0.5)?P[iI][iJ]:1-P[iI][iJ];
}
// Set the conditional probability.
PXw [iI] = p;
}
// The
posterior probability section.
double PX =
0.0, maxP = 0.0;
// Initial the number is 255, To avoid becoming to zero.
int number
= 255;
// There are two samples.
for ( iI =
0; iI < 2; iI++ )
{
// The
prior probability multiplied
by The conditional probability.
PX += Pw[iI] * PXw[iI];
}
// There are two samples.
for ( iI =
0; iI < 2; iI++ )
{
// Calculate the
posterior probability.
PwX[iI] = Pw[iI] * PXw[iI] / PX;
// Debug
output
char
log[100];
sprintf_s( log, "PwX[%d] :%f \n",
iI, PwX[iI] );
OutputDebugStringA(log);
// Find out maximum probability.
if (
PwX[iI] > maxP )
{
maxP = PwX[iI];
// Set number.
number = iI;
}
}
PwX[0] :0.333333
PwX[1] :0.666667
Test :1
You can download source code and binary code.
Exception
1. There is a notice, if your bit depth of bitmap
file are not 24 bits, you should change your bitmap files to adapt this
program, or you could rewrite this source code to fit your bitmap format.
2. You have to install Microsoft SDK v7.1, because
I include windowscodes.lib.
#pragma comment(lib, "windowscodecs.lib")
Reference
[1] Gary Bradski and Adrian Kaehler, “Learning
OpenCV: Computer Vision with the OpenCV Library,” O’REILLY, September 2008,
ISBN:978-0-596-51613-0
Acknowledge
Thank you (Microsoft Visual Studio 2010) very much
for this great development utility.





































