Analysis text block division based communication domain OpenCV.Net

The last projected by way of dividing the text block (see https://www.cnblogs.com/BoyTNT/p/11812323.html ) but this method has many limitations, requirements branch clear characters have not span multiple lines, not tilt, but also more sensitive to noise. Or take a picture on the back, but I added on top of a larger word, the results have a problem:

 

It can be seen due to the lower right corner of the big "test" the word across multiple lines, resulting in horizontal projection on the wrong branch.

 The another way, to do based connectivity analysis. Simply speaking, it is to make certain image expansion operation, so that different parts of the same characters, and adjacent characters together to overlap each other into a whole, and to find each individual block of the analysis, the noise excluded, the remaining the basic result is in line with the conditions.

Directly on the code, and then later analysis:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
 
using OpenCvSharp;
using OpenCvSharp.Extensions;
using OpenCvSharp.Utilities;
 
namespace OpenCvTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //读入源文件
            var src = IplImage.FromFile("source.jpg");
                
            //转换到灰度图
            var gray = Cv.CreateImage(src.Size, BitDepth.U8, 1); 
            Cv.CvtColor (the src, Gray, ColorConversion.BgrToGray); 
                
            // do some expansion, x and y directions do, but with different coefficients
             // use Erode method, etching operation for the white region, to be equivalent to text was expanded 
            var the kernal Cv.CreateStructuringElementEx = ( . 5 , 2 , . 1 , . 1 , ElementShape.Rect); 
            Cv.Erode (Gray, Gray, the kernal, 2 ); 
                
            // binarization 
            Cv.Threshold (gray, gray, 0 , 255 , ThresholdType.BinaryInv | ThresholdType.Otsu); 
                
            // detecting communication domain, a communication domain for each series of points represented, FindContours method can only obtain the first domain 
            var Storage = Cv.CreateMemStorage (); 
            CvSeq<CvPoint> Contour = null ; 
            Cv.FindContours (Gray, Storage, OUT Contour, CvContour.SizeOf, ContourRetrieval.CComp, ContourChain.ApproxSimple);
             var Color = new new CvScalar ( 0 , 0 , 255 ); 
                
            // traversing 
            the while ( ! Contour = null ) 
            { 
                // get the communication area circumscribed rectangle 
                var RECT = Cv.BoundingRect (Contour); 
                    
                // if the height is insufficient, or the aspect ratio is too small, that is invalid data, or to draw a rectangle picture the 
                IF (rect.height> 10 && (* rect.width 1.0 / rect.height)> 0.2 ) 
                    Cv.DrawRect (the src, RECT, Color); 
                        
                // remove a communication domain 
                Contour = contour.HNext; 
            } 
            Cv.ReleaseMemStorage (Storage); 
                
            // display 
            Cv.ShowImage ( " the Result " , the src); 
            Cv.WaitKey (); 
            Cv.DestroyAllWindows (); 
        } 
    } 
}

 

Step by step analysis below. Read original is this:

 

Expanded and converted to grayscale processing, it has a plurality of characters can be seen that substantially the same text block has been attached to together:

 

After the binarized image:

 

After doing connectivity analysis, the analyzed results of the original is this:

Cv.DrawContours(src, contour, color, color, 1);

 

Take a circumscribed rectangle for each connected component, the final result is this:

 

We can see the effect of a lot better than before, the larger the word can be used as a separate text block is detected. Further, even if the text block is the same row, will fluctuate slightly, no longer absolutely aligned in rows.

 

Without permission is forbidden.

 

Guess you like

Origin www.cnblogs.com/BoyTNT/p/11812562.html