Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv 用于OCR的图像二值化_Opencv_Image Processing - Fatal编程技术网

Opencv 用于OCR的图像二值化

Opencv 用于OCR的图像二值化,opencv,image-processing,Opencv,Image Processing,我想把扫描图像转换成黑白图像,目标是在图像通过互联网传输用于OCR之前减小文件大小 由扫描仪/通用图像编辑软件创建的正常二值化/黑白图像会产生不理想的结果 大量随机黑色像素被留下,这实际上只是二值化产生的噪声,这导致OCR尝试识别没有字符的字符,或者在字符后插入句号、冒号等 我可以在OpenCV中使用什么来对图像进行二值化,保持线条、字符和黑色区域为实心,以及减少白色区域的像素噪声 我曾经玩弄过cvThreshold和cvAdaptiveThreshold,但效果还不是很好 举个例子,看看这个和

我想把扫描图像转换成黑白图像,目标是在图像通过互联网传输用于OCR之前减小文件大小

由扫描仪/通用图像编辑软件创建的正常二值化/黑白图像会产生不理想的结果

大量随机黑色像素被留下,这实际上只是二值化产生的噪声,这导致OCR尝试识别没有字符的字符,或者在字符后插入句号、冒号等

我可以在OpenCV中使用什么来对图像进行二值化,保持线条、字符和黑色区域为实心,以及减少白色区域的像素噪声

我曾经玩弄过cvThreshold和cvAdaptiveThreshold,但效果还不是很好

举个例子,看看这个和。

你可以使用一种算法,删除那些没有填满图像中合理数量像素的组件

在OpenCV中实现它的一种非常简单的方法是使用等高线:

1. Do the preliminary bizariztion of the OCR, that will give you a very noise output. 
2. Find all contours on that noise image.
3. For each found contour:
  3.1. Fill the contour with a color different of the two options in the binarized image.
  3.2. Count the ammount of pixels filled with that color.
  3.3. If the ammount of pixels are smaller than a given treshold, fill the contour with the void collor of the binary image.
供参考:及


可以在3.1上优化循环分类多个轮廓。在3.2中对所有这些颜色进行一次像素计数。我没有回答优化的版本,因为您可能有超过253个不同的组(255种颜色-二值图像的2种默认颜色),考虑到这一点并不那么简单。

您可以尝试一下,但仍需要调整一些参数

#define ALPHA_SCALE 2
#define THRESHOLD_VAL 40
#define MAX_VAL_FOR_THRESHOLD 250
#define PIXEL_MISMATCH_COUNT 10 //9, 7
Mat current_frame_t2;        

     IplImage *img = cvLoadImage("Original.tiff", CV_LOAD_IMAGE_UNCHANGED );
     cvNamedWindow("My_Win", CV_WINDOW_AUTOSIZE);
    // namedWindow("My_Win", 1);
     cvShowImage("My_Win", img);
      cvWaitKey(10);
     Mat current_frame_t1(img);
     cvtColor(current_frame_t1, current_frame_t2, CV_RGB2GRAY);
    current_frame_t1.release();
    imshow("My_Win", current_frame_t2);
     cvWaitKey(10);
     equalizeHist(current_frame_t2, current_frame_t1);
    current_frame_t2.release();
    convertScaleAbs(current_frame_t1, current_frame_t2,ALPHA_SCALE);

    threshold(current_frame_t2, current_frame_t1, THRESHOLD_VAL, MAX_VAL_FOR_THRESHOLD, CV_THRESH_BINARY);
    medianBlur(current_frame_t1,current_frame_t2,1); 
    imshow("My_Win", current_frame_t2);
    imwrite("outimg.tiff", current_frame_t2),
    cvWaitKey(0);

您的示例似乎是三元的,除了黑白之外,我还看到了至少一种灰色。@markransem当我回去查看IrfanView中的图像时,我认为您是对的,我一定是错误地保存了黑白图像。但是,在Gimp中查看图像时,像素仅为黑白。您使用什么来查看图像?就我而言,我信任gimp而不是IrfanView。我当时正在Chrome中查看它。今天Firefox看起来不错,我不知道发生了什么。作为一名计算机视觉和OpenCV新手,我必须花一点时间研究你的答案,才能想出我自己的实现。您能提供一个小的代码片段吗?