调整Canny边缘算法的门槛 [英] Adjusting the threshold in Canny edge algorithm
问题描述
我想试试我的手,在文字识别,所以我使用了OpenCV中描绘出的边缘和C ++发现斜坡,曲线等,边缘算法能够较好的大而整洁的字符集,但是,当谈到对小的印刷文本或文本有很多背景噪音就像镶嵌在验证码它的斗争,看起来不完整的,我的猜测是我还没有正确设置的阈值,并尝试不同的值,但没有成功。
I wanted to try my hand at text recognition, so i've used opencv to trace out the edges and c++ to find slopes, curves etc, the edge algorithm works well on big and uncluttered sets of characters but when it comes against small printed text or text with a lot of background noise like embedded in captcha it struggles and looks incomplete, my guess was i hadn't set the threshold values correctly and tried different values with no success.
下面是我的code:
#include "cv.h"
#include "highgui.h"
using namespace cv;
const int low_threshold = 50;
const int high_threshold = 150;
int main()
{
IplImage* newImg;
IplImage* grayImg;
IplImage* cannyImg;
newImg = cvLoadImage("ocv.bmp",1);
grayImg = cvCreateImage( cvSize(newImg->width, newImg->height), IPL_DEPTH_8U, 1 );
cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
cannyImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 1);
cvCanny(grayImg, cannyImg, low_threshold, high_threshold, 3);
cvNamedWindow ("Source", 1);
cvNamedWindow ("Destination",1);
cvShowImage ("Source", newImg );
cvShowImage ("Destination", cannyImg );
cvWaitKey(0);
cvDestroyWindow ("Source" );
cvDestroyWindow ("Destination" );
cvReleaseImage (&newImg );
cvReleaseImage (&grayImg );
cvReleaseImage (&cannyImg );
return 0;
}
我看了看对面的网,已经看到了一些复杂的阈值条件,比如从本站code:
I've looked across the net and have seen some complicated thresholding conditions like in this code from this site :
% Set direction to either 0, 45, -45 or 90 depending on angle.
[x,y]=size(f1);
for i=1:x-1,
for j=1:y-1,
if ((gradAngle(i,j)>67.5 && gradAngle(i,j)<=90) || (gradAngle(i,j)>=-90 && gradAngle(i,j)<=-67.5))
gradDirection(i,j)=0;
elseif ((gradAngle(i,j)>22.5 && gradAngle(i,j)<=67.5))
gradDirection(i,j)=45;
elseif ((gradAngle(i,j)>-22.5 && gradAngle(i,j)<=22.5))
gradDirection(i,j)=90;
elseif ((gradAngle(i,j)>-67.5 && gradAngle(i,j)<=-22.5))
gradDirection(i,j)=-45;
end
end
end
如果是这种解决方案可以提供人我这个算法的C ++等价的,如果不是我还能做什么?
If this is the solution can somebody provide me the c++ equivalent of this algorithm, if it's not what else can i do ?
推荐答案
Canny边缘检测器是使用迟滞阈值的多步检测器(它使用了两个阈值,而不是一个),和边缘跟踪(你的最后段是部分这个步骤)。我建议首先阅读维基百科作品。一个可能的解决办法是选择高门槛,因此如图像像素的70%将被归类为边缘(开始 - 你可以用柱状图这样做快),不是选择低阈值,例如40%的高门槛。这可能是一个好主意,尝试执行边缘检测图像块,而不是整个图像,让你的算法可以计算出不同的区域不同的阈值。
Canny edge detector is a multi-step detector using hysteresis thresholding (it uses two threshold instead of one), and edge tracking (your last snippet is the part of this step). I suggest reading the wikipedia entry first. One possible solution could be to choose the high threshold, so e.g. 70% of the image pixels would be classified as edge (initially - you could do this quickly using histograms), than choose the low threshold as e.g. 40% of the high threshold. It might be a good idea to try to perform edge detection on image block rather than the whole image, so your algorithm could calculate different thresholds for different areas.
请注意,CAPTCHA-S的设计是很难分割,并补充说分手边缘检测的噪声是一种技术才达到这个(您可能需要首先平滑图像)。
Note that CAPTCHA-s are designed to be hard to segment, and adding noise that broke edge detection is one technique to achive this (you might need to smooth the image first).
这篇关于调整Canny边缘算法的门槛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!