opencv OCR通过​​Sobel边缘检测器而不是FLANN,代码

时间:2015-04-25 08:53:52

标签: c++ opencv

基本上,我正在尝试进行OCR,首先我使用sobel边缘检测器然后使用强力匹配器来查看有多少好的匹配。我将两个图像传递给brute_force_matcher的行似乎触发了断点,然后我得到一个堆已损坏的异常

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define DictSize 2
using namespace cv;

void readme();

/** @function main */
int brute_force_matcher(Mat img_1,Mat img_2)
{
//Mat img_1;
//Mat img_2;
//  cvtColor(img1, img_1, CV_RGB2GRAY);
//cvtColor(img2, img_2, CV_RGB2GRAY);

if (!img_1.data || !img_2.data)
{
    return -1;
}

//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 400;

SurfFeatureDetector detector(minHessian);

std::vector<KeyPoint> keypoints_1, keypoints_2;

detector.detect(img_1, keypoints_1);
detector.detect(img_2, keypoints_2);

//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;

Mat descriptors_1, descriptors_2;

extractor.compute(img_1, keypoints_1, descriptors_1);
extractor.compute(img_2, keypoints_2, descriptors_2);

//-- Step 3: Matching descriptor vectors with a brute force matcher
BFMatcher matcher(NORM_L2);
std::vector< DMatch > matches;
matcher.match(descriptors_1, descriptors_2, matches);

//-- Draw matches
//Mat img_matches;
//drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_matches);

//-- Show detected matches
//imshow("Matches", img_matches);
//printf("%d", matches.size());
//waitKey(0);

return (int)matches.size();
}

/** @function readme */
void readme()
{
std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl;
}
Mat sobel_edge_detector(Mat img)
{

Mat src, src_gray;
Mat grad;
// char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;

int c;

/// Load an image
// src = imread( argv[1] );
src = img;
if (!src.data)
{
    printf("no image found");
    exit(0);
}

GaussianBlur(src, src, Size(3, 3), 0, 0, BORDER_DEFAULT);

/// Convert it to gray
cvtColor(src, src_gray, CV_RGB2GRAY);

/// Create window
//amedWindow( window_name, CV_WINDOW_AUTOSIZE );

/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;

/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);

/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);

/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);

//imshow( window_name, grad );

//waitKey(0);

return grad;
}
int main(){
Mat src =      imread("pathtoimg");
String dir = "dir";
String format = ".jpg";
//Mat img_2;
//Mat src2;
int comp[DictSize];
Mat img_1 = sobel_edge_detector(src);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", img_1);
waitKey(0);

Mat src2 = imread(dir+std::to_string(0)+format);
Mat img_2 = sobel_edge_detector(src2);
imshow("image2", img_2);
waitKey(0);


int s = brute_force_matcher(img_1, img_2);

printf("%d", s);
/*for (int imgid = 0; imgid < DictSize; imgid++){
    std::string s = std::to_string(imgid);
    Mat src2 = imread("pathtoimg");
    Mat img_2 = sobel_edge_detector(src2);
//  comp[imgid] = brute_force_matcher(img_1,img_2);
}*/
return 0;
}

WD

0 个答案:

没有答案