0

I have found a program which can binarize an image and make the image monochrome

OpenCV Adaptive Threshold OCR

I have copy/pasted the code

#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdarg.h>
#include "/usr/include/opencv2/opencv.hpp"
#include "fstream"
#include "iostream"
using namespace std;
using namespace cv;

void CalcBlockMeanVariance(Mat& Img,Mat& Res,float blockSide=21) // blockSide - the parameter (set greater for larger font on image)
{
    Mat I;
    Img.convertTo(I,CV_32FC1);
    Res=Mat::zeros(Img.rows/blockSide,Img.cols/blockSide,CV_32FC1);
    Mat inpaintmask;
    Mat patch;
    Mat smallImg;
    Scalar m,s;

    for(int i=0;i<Img.rows-blockSide;i+=blockSide)
    {       
        for (int j=0;j<Img.cols-blockSide;j+=blockSide)
        {
            patch=I(Range(i,i+blockSide+1),Range(j,j+blockSide+1));
            cv::meanStdDev(patch,m,s);
            if(s[0]>0.01) // Thresholding parameter (set smaller for lower contrast image)
            {
                Res.at<float>(i/blockSide,j/blockSide)=m[0];
            }else
            {
                Res.at<float>(i/blockSide,j/blockSide)=0;
            }           
        }
    }

    cv::resize(I,smallImg,Res.size());

    cv::threshold(Res,inpaintmask,0.02,1.0,cv::THRESH_BINARY);

    Mat inpainted;
    smallImg.convertTo(smallImg,CV_8UC1,255);

    inpaintmask.convertTo(inpaintmask,CV_8UC1);
    inpaint(smallImg, inpaintmask, inpainted, 5, INPAINT_TELEA);

    cv::resize(inpainted,Res,Img.size());
    Res.convertTo(Res,CV_32FC1,1.0/255.0);

}

int main( int argc, char** argv )
{
    namedWindow("Img");
    namedWindow("Edges");
    //Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
    Mat Img=imread("Test2.JPG",0);
    Mat res;
    Img.convertTo(Img,CV_32FC1,1.0/255.0);
    CalcBlockMeanVariance(Img,res); 
    res=1.0-res;
    res=Img+res;
    imshow("Img",Img);
    cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
    cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
    imwrite("result.jpg",res*255);
    imshow("Edges",res);
    waitKey(0);

    return 0;
}

compile

g++ binarize.cpp `pkg-config opencv --cflags --libs`

run

./a.out

error

(Img:27277): Gtk-WARNING **: cannot open display:

update

int main( int argc, char** argv )
{
    namedWindow("Img");
    namedWindow("Edges");
    //Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
    Mat Img=imread("Test2.JPG",0);
    Mat res;
    Img.convertTo(Img,CV_32FC1,1.0/255.0);
    CalcBlockMeanVariance(Img,res); 
    res=1.0-res;
    res=Img+res;
    imshow("Img",Img);
    cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
    cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
    imwrite("result.tif",res*255);
    imshow("Edges",res);
    waitKey(0);

    return 0;
}

enter image description here

Community
  • 1
  • 1
clarkk
  • 27,151
  • 72
  • 200
  • 340

1 Answers1

0

Just built this code on ubuntu 14.03 x64, using the same command line as you used, no any messages, runs fine.

I think warning related to your system environment.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • I found a solution by running `xvfb-run ./txtbin` :) but I have one last thing.. If the output file is `tif` the colors in the image is like inverted.. black is white and white is black.. is there an easy way to make the code work with `tif` output instead of `jpg` output? – clarkk Dec 10 '15 at 02:55
  • res=1-res; or res=255-res; depends on scale. – Andrey Smorodov Dec 10 '15 at 02:57
  • have updated the question.. if `res=255-res` and the output is tif the image is all black.. have uploaded an image with `res=1-res` and output tif – clarkk Dec 10 '15 at 03:05
  • Try to reduce blockSide, it is 21 on default, looks like you need smaller values. – Andrey Smorodov Dec 10 '15 at 03:25
  • Have now tried to set `blockSide` to 15, 10 and 5.. The result is unchanged – clarkk Dec 10 '15 at 11:45
  • Have created a new question if you have the answer :) http://stackoverflow.com/questions/34201184/binarize-image-with-text-to-tiff – clarkk Dec 10 '15 at 11:54
  • have created a new question which is about your binarize code :) I will give you 200 points if you have a solution :) Its not about this issue about tiff output.. http://stackoverflow.com/questions/34398046/opencv-binarize-images-with-text – clarkk Dec 21 '15 at 14:35