Skip to main content

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.




Popular Posts

最佳化處理策略之快速消除扭曲演算法

波蘭文學習之旅:1-1. 波蘭文字母與發音(注音版)

Python 日期與時間的處理

最佳化處理策略之快速消除扭曲演算法

Image

波蘭文學習之旅:1-1. 波蘭文字母與發音(注音版)

Image

Python 日期與時間的處理

Image

Visual Basic 6.0 程式案例學習: 10. 條碼列印程式 (2014版)

Image

Visual Basic .Net 程式案例學習: 06. 題庫測驗系統 (2014版)

Image

修復損毀的 SQLite DB 資料庫

Image

用10種程式語言做影像二值化(Image binarization)

Image

解決 ValueError: If using all scalar values, you must pass an index

Image

Visual Basic 6.0 程式案例學習: 04. 人事考勤管理系統 (2014版)

Image

Visual Basic 6.0 程式案例學習: 07. 收據列印程式 (2014版)

Image

佑佑的 Line 貼圖創作

貼圖作者網址:

https://line.me/S/shop/sticker/author/3883362

靜態貼圖

 
 
 
 
  牡羊座:狗狗角色

作者:佑佑
依照牡羊座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  金牛座:兔兔角色

作者:佑佑
依照金牛座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  雙子座:貓貓角色

作者:佑佑
依照雙子座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  巨蟹座:倉鼠角色

作者:佑佑
依照巨蟹座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
 
 
 
 
  獅子座:幼獅角色

作者:佑佑
依照獅子座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  處女座:松鼠角色

作者:佑佑
依照處女座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  天秤座:鴿子角色

作者:佑佑
依照天秤座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  天蠍座:鳳凰角色

作者:佑佑
依照天蠍座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
 
 
 
 
  射手座:人馬角色

作者:佑佑
依照射手座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  魔羯座:山羊角色

作者:佑佑
依照魔羯座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  水瓶座:海豚角色

作者:佑佑
依照水瓶座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
  雙魚座:小丑魚角色

作者:佑佑
依照雙魚座的個性優點、個性缺點、生活習慣、理財習慣、社交習慣、愛情觀、節假日、禮貌問候,所編撰的貼圖。星座只能當參考用,不會完全準確。
購買貼圖
 
 
 
 
  上班族都變成狒狒,心裡想要說的話。

作者:佑佑
最近在台灣走紅的狒狒,用擬人化的方式,變成上班族,畫出心裡的想說的話。
購買貼圖
  接案派遣的日常

作者:佑佑
接案派遣到其他公司的日常對話,有調皮、正經、日常對話。
購買貼圖
  貓女事務員的搞笑時刻

作者:佑佑
把行政人員在職場上遇到千奇百怪的事情畫出來,用可愛的貓女來擔任行政人員的代言人。
購買貼圖
  貓貓說每天都會用到的話

作者:佑佑
日常生活常見的短句,用小貓擬人化的方式呈現,比較親切可愛。
購買貼圖
 
     
  大人的煩惱特輯:小兔角色

作者:佑佑
錢包餓了,生活苦了。變美變健康?先讓我睡飽吧。心好累,我需要充電。
購買貼圖
     

動態貼圖

 
 
 
 
  牡羊座日常交際:狗狗角色

作者:佑佑
依照牡羊座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  金牛座日常交際:兔兔角色

作者:佑佑
依照金牛座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  雙子座日常交際:貓貓角色

作者:佑佑
依照雙子座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  巨蟹座日常交際:倉鼠角色

作者:佑佑
依照巨蟹座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
 
 
 
 
  獅子座日常交際:幼獅角色

作者:佑佑
依照獅子座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  處女座日常交際:松鼠角色

作者:佑佑
依照處女座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  天秤座日常交際:鴿子角色

作者:佑佑
依照天秤座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  天蠍座日常交際:鳳凰角色

作者:佑佑
依照天蠍座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
 
 
 
 
  射手座日常交際:人馬角色

作者:佑佑
依照射手座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  魔羯座日常交際:山羊角色

作者:佑佑
依照魔羯座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  水瓶座日常交際:海豚角色

作者:佑佑
依照水瓶座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
  雙魚座日常交際:小丑魚角色

作者:佑佑
依照雙魚座的個性,描繪日常生活的行為,供交際表達心情使用。星座只能當參考用,不會完全準確。
購買貼圖
 
     
  厭世報:鬍渣男

作者:佑佑
厭世總有個理由跟原因,實在是令人不爽,為什麼最倒楣的總是我?真希望明天就是世界末日,讓大家都一樣慘。
購買貼圖
     

表情貼

 
 
 
 
  貓貓說每天都用得到的表情貼

作者:佑佑
日常生活常見的表情用語,用小貓擬人化的方式呈現,比較親切可愛。
購買貼圖
  天秤座的表情貼:鴿子角色

作者:佑佑
日常生活常見的表情用語,天秤座的鴿子擬人化方式呈現,比較親切可愛。
購買貼圖
  天蠍座的表情貼:鳳凰角色

作者:佑佑
日常生活常見的表情用語,天蠍座的鳳凰擬人化方式呈現,比較親切可愛。
購買貼圖
  射手座的表情貼:人馬角色

作者:佑佑
日常生活常見的表情用語,射手座的人馬擬人化方式呈現,比較親切可愛。
購買貼圖
 
 
 
 
  魔羯座的表情貼:山羊角色

作者:佑佑
日常生活常見的表情用語,魔羯座的山羊擬人化方式呈現,比較親切可愛。
購買貼圖
  水瓶座的表情貼:海豚角色

作者:佑佑
日常生活常見的表情用語,水瓶座的海豚擬人化方式呈現,比較親切可愛。
購買貼圖
  雙魚座的表情貼:小丑魚角色

作者:佑佑
日常生活常見的表情用語,雙魚座的小丑魚擬人化方式呈現,比較親切可愛。
購買貼圖