C++画像処理(二十二)HOG特徴抽出

3996 ワード

HOGフィーチャー抽出
原理:(一)前処理:
ガンマ補正と階調化が含まれます.これはオプションのステップです.実験がしないことに影響は大きくないことを証明したからです.ガンマ補正は実験に及ぼす光度の影響を低減する.階調化とは、カラー画像を階調図にすることである.実はカラー画像も直接処理できます.ただし、3チャネルの色値をそれぞれ勾配計算し、最後に勾配が最も大きいものを選択します.簡単にするために,階調図として入力すると仮定し,大きさは64*128である(この大きさは前述の論文の大きさであり,自分で異なる大きさを特定することもできるが,実験効果は保証されない).
コード:
//HOG    (   )
void* HOG(QImage &image,QImage &otp)
{
    //  8x8  cell
    int step = 8;
    int bins = 9;

    //       ,       ,       8    ,    
    int width = image.width();
    int height = image.height();

    int cols = width / step;
    int rows = height / step;

    width = cols * step;
    height = rows * step;

    //    
    image.scaled(width,height);




    //       QImage  ,     0
    QImage* out = new QImage(width,height,QImage::Format_ARGB32);
    Init_Image(*out,0);

    //      
    int i = 0, j = 0;
    int x = 0,y = 0;

    //       
    QVector gradiant_x;     //x      
    QVector gradiant_y;     //y      
    QVector gradiant;       //    
    QVector direct;         //    
    //sobel       
    Sobel_mat(image,gradiant_x,0);
    Sobel_mat(image,gradiant_y,1);
    //             
    cartToPolar(gradiant_x,gradiant_y,gradiant,direct);




    //int all_cell = width*height;
    int curr_index = 0;  //       
    double gray = 0;     //    ,      
    int theta = 0;       //    ,      
    int dire_temp = 0; //   20   
    int mo = 0;        //   20  
    double weight = 0;  //  
    double wn = 0;      //    


    QVector histemp(bins,0);   //  8x8 cell   ;
    QVector> histog;  //     

    // 8   
    for(i = 0; i= 10)
                    {
                        wn = mo - 10;
                        weight = (20-wn) / 20.0f;
                    }
                    else {
                        wn = 10 - mo;
                        weight = (20-wn) / 20.0f;
                    }

                    //      
                    if(mo>=10)
                    {
                        histemp[dire_temp] += (weight * gray);
                        if(dire_temp < 8)
                        {
                            histemp[dire_temp+1] += ((1.0-weight)*gray);
                        }
                    }
                    else {
                         histemp[dire_temp] += (weight * gray);
                        if(dire_temp > 0)
                        {
                            histemp[dire_temp-1] += ((1.0-weight)*gray);
                        }
                    }

                }
            }
            histog.push_back(histemp);
            histemp.fill(0);
        }
    }


    //   Naturalization
    QVector> blocks;  //2x2 block = 4 cell(8x8) = 16x16 
    QVector cell_1;
    QVector cell_2;
    QVector cell_3;
    QVector cell_4;

    for(i = 0; i block_temp = generateBlockVector(cell_1,cell_2,cell_3,cell_4);

            blocks.push_back(block_temp);

        }
    }





    otp = *out;
    return 0;
}


//ベクトルの型長
QVector generateBlockVector(QVector cell_1,QVector cell_2,QVector cell_3,QVector cell_4)
{
    QVector block;
    block.append(cell_1);
    block.append(cell_2);
    block.append(cell_3);
    block.append(cell_4);
    double sum = 0;

    for(int i = 0; i