c++ - OpenCV binary adaptive threshold OCR -
i need convert images binary ocr.
here functions using:
mat binarize(mat & img, mat& res, float blocksize, bool inverse) { img.convertto(img,cv_32fc1,1.0/255.0); calcblockmeanvariance(img,res, blocksize, inverse); res=1.0-res; res=img+res; if (inverse) { cv::threshold(res,res,0.85,1,cv::thresh_binary_inv); } else { cv::threshold(res,res,0.85,1,cv::thresh_binary); } cv::resize(res,res,cv::size(res.cols/2,res.rows/2)); return res; }
where calcblockmeanvariance
:
void calcblockmeanvariance(mat& img,mat& res,float blockside, bool inverse) //21 blockside - parameter (set greater 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) { (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 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()); if (inverse) { cv::threshold(res,inpaintmask,0.02,1.0,cv::thresh_binary_inv); } else { 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); }
when passing in 1
calcblockmeanvariance
blockside
result, have tried raise blockside
results in worse results.
before:
after:
can suggest different method converting image binary prep ocr?
thanks.
i think can thresholding using otsu method. can apply on whole image or on blocks of image. did following steps:
- thresholding using
otsu
method on desired input. closing
result.
python code
image = cv2.imread('image4.png', cv2.imread_grayscale) # reading image if image none: print 'can not find image!' exit(-1) # thresholding image using ostu method ret, thresh = cv2.threshold(image, 0, 255, cv2.thresh_binary_inv | cv2.thresh_otsu) # applying closing operation using ellipse kernel n = 3 kernel = cv2.getstructuringelement(cv2.morph_ellipse, (n, n)) thresh = cv2.morphologyex(thresh, cv2.morph_close, kernel) # showing result cv2.imshow('thresh', thresh) cv2.waitkey(0) cv2.destroyallwindows()
explanation
in first part read input image using imread
, checked image opened correctly!.
image = cv2.imread('image4.png', cv2.imread_grayscale) # reading image if image none: print 'can not find image!' exit(-1)
now thresholding image otsu
method feeding thresh
method thresh_binary_inv | thresh_otsu
argument. otsu
method works base on optimization problem finding best value thresholding. provided range of possible value threshold value giving lower bound 0
, upper bound 255
.
ret, thresh = cv2.threshold(image, 0, 255, cv2.thresh_binary_inv | cv2.thresh_otsu)
and closing operation done removing black holes in image using ellipse
kernel.
kernel = cv2.getstructuringelement(cv2.morph_ellipse, (n, n)) thresh = cv2.morphologyex(thresh, cv2.morph_close, kernel)
result
Comments
Post a Comment