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

时间:2013-10-25 11:53:13

标签: c++ image-processing histogram image-comparison

我有一个基本图像(数字或操作符),我必须与14个图像(0到9和* / - +)进行比较,以了解哪一个与基本图像匹配。

我创建了基本图像的直方图,并使用我为所有14个图像的直方图创建的循环,并对直方图进行了标准化。

在循环中,对于每个新创建的直方图,我使用compareHist()函数与基本直方图进行比较。并输出结果的双值。

使用相关或卡方或交点或Bhattacharyya方法:

我得到一组特定的值。 使用不同的基础时,我仍然会得到相同的值集。

为什么我会这样?我是否需要更改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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您可以从一些类似位图的图像中分离并裁剪一个字符,然后您想要确定它是什么字符? 就像自动字符识别一样?

也许您可以使用边缘检测器而不是比较直方图?

我会尝试类似的算法:

  1. 查找,隔离并裁剪要识别的角色
  2. 将其缩放到预定的水平和垂直尺寸以对其进行标准化。
  3. 在角色上扫描一个定向边缘探测器,就像索贝尔一样简单 边缘检测器。
    • 首先应用水平边缘检测器,然后水平“展平”边缘图 获得表示每个像素行的边缘信息的向量。 (即,对每个像素行计算边缘图中的1和0)
    • 第二个应用垂直边缘检测器,并垂直展平边缘图给出 另一个向量,表示每个像素列的边缘信息。 (即累计并计算每个像素列的边缘信息)
  4. 连接这两个向量[水平边缘信息, 垂直边信息]
  5. 然后将最终的连接向量与一个库进行比较 已知测试样本的预计算向量(0-9,+ / * - )
  6. 看起来有些相似的数字(8,6,9,3)在水平分量或垂直分量中仍然应该有明显的峰值和谷值。

相关问题