简体   繁体   English

使用直方图比较c ++匹配图像

[英]Matching images using Histogram comparison c++

I have a base image(a number or an operator) which i have to compare with 14 images(0 to 9 and * / - +) to know which one matches with the base image. 我有一个基本图像(数字或运算符),我必须与14个图像(0到9和* / - +)进行比较,以了解哪一个与基本图像匹配。

I created the base image's histogram and using a loop I create for all the 14 images' histograms and the histograms were normalized. 我创建了基本图像的直方图,并使用我为所有14个图像的直方图创建的循环,并对直方图进行了标准化。

In the loop, for each newly created histogram i compared with the base histogram using the compareHist() function. 在循环中,对于每个新创建的直方图,我使用compareHist()函数与基本直方图进行比较。 And output the resulted double value. 并输出结果的双值。

Using Correlation or Chi-square or Intersection or Bhattacharyya method: 使用相关或卡方或交叉或Bhattacharyya方法:

I am getting a particular set of values. 我得到了一组特定的价值观。 And when using a different base , I am still getting that same set of values. 当使用不同的基础时 ,我仍然得到相同的值集。

Why am I getting this? 我为什么要这个? Do I need to alter the normalize function to get distinct values for different bases? 我是否需要更改normalize函数以获得不同碱基的不同值?

CODE: 码:

void matchHistogram(){

Mat src_base, hsv_base;
Mat src_test1, hsv_test1;

/// Histograms
MatND hist_base;
MatND hist_test1;

/// Using 30 bins for hue and 32 for saturation
int h_bins = 30; int s_bins = 32;
int histSize[] = { h_bins, s_bins };

// hue varies from 0 to 255, saturation from 0 to 180
float h_ranges[] = { 0, 255 };
float s_ranges[] = { 0, 180 };

const float* ranges[] = { h_ranges, s_ranges };

// Use the o-th and 1-st channels
int channels[] = { 0, 1 };


for(int i=0;i<noOfcropped;i++){ //get base image  //noOfCropped is number of base images i'll compare to 14 images
    cout<<"     "<<i<<endl;
    stringstream croppedimage;
    croppedimage<<"CroppedImages/croppedImage"<<i;
    croppedimage<<".jpg";

    src_base = imread( croppedimage.str(), 1 );
    imshow(croppedimage.str(),src_base);

    /// Convert to HSV
    cvtColor( src_base, hsv_base, CV_BGR2HSV );


    /// Calculate the histogram for the HSV images
    calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges);
    normalize( hist_base, hist_base, 0, 1,  NORM_MINMAX, -1, Mat() );


    for(int j=0;j<14;j++){//comparing 1 croppedimage with each different characters
        cout<<"  "<<j<<endl;
        stringstream test1;
        test1<<"ImagesToCompare/"<<j;
        test1<<".jpg";

        src_test1 = imread(test1.str(), 1 );

        /// Convert to HSV
        cvtColor( src_test1, hsv_test1, CV_BGR2HSV );

        /// Calculate the histogram for the HSV images
        calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges);
        normalize( hist_test1, hist_test1, 0, 1,NORM_MINMAX, -1, Mat() );

        /// Apply the histogram comparison methods
        int compare_method = 0;
        //when 0 or 2, highest comparison values>> best match
        //when 1 or 3, lowest comparison values>> best match
        double base_test1 = compareHist( hist_base, hist_test1, compare_method );


        cout<<base_test1<<endl;
    }
}

} }

If I understand your question correctly, you isolate and crop a character from some bitmap-like image and you then want to identify what character it is? 如果我正确理解你的问题,你可以从一些类似位图的图像中分离和裁剪一个字符,然后你想确定它是什么字符? Like an automatic character recognition? 像自动字符识别?

Perhaps you could use an edge detector instead of comparing histograms? 也许您可以使用边缘检测器而不是比较直方图?

I would try an algorithm something like: 我会尝试一个类似的算法:

  1. find, isolate and crop the character to be identified 查找,隔离和裁剪要识别的角色
  2. Scale it to a predetermined horizontal and vertical size to normalize it. 将其缩放到预定的水平和垂直尺寸以使其标准化。
  3. Sweep a directional edge detector over the character, something simple like a Sobel edge detector. 在角色上扫描定向边缘检测器,就像Sobel边缘检测器一样简单。
    • First apply a Horizontal edge detector, then horizontally "flatten" the edge-map to obtain a vector representing the edge information at each pixel row. 首先应用水平边缘检测器,然后水平“展平”边缘图以获得表示每个像素行的边缘信息的向量。 (ie count the 1, and 0 in the edge-map for each pixel row) (即计算每个像素行的边缘图中的1和0)
    • Second apply a Vertical edge detector, and vertically flatten the edge-map giving another vector that represents the edge information at each pixel column. 第二个应用垂直边缘检测器,并垂直展平边缘图,给出另一个向量,表示每个像素列的边缘信息。 (ie cumulate and count the edge information for each pixel column) (即累计并计算每个像素列的边缘信息)
  4. Concatenate these two vectors [horizontal-edge information, vertical-edge information] 连接这两个向量[水平边缘信息,垂直边缘信息]
  5. Then compare the final concatenated vector to a library of precomputed vectors of your known test samples (0-9, +/*-) 然后将最终的连接向量与已知测试样本的预计算向量库进行比较(0-9,+ / * - )

Numbers that look somewhat similar (8,6,9,3) should still have distinct peaks and valleys either in the horizontal component or the vertical component. 看起来有些相似的数字(8,6,9,3)在水平分量或垂直分量中仍应具有明显的峰和谷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM