Disclosure of Invention
The invention aims to provide a character perspective correction method based on a genetic optimization algorithm, which solves the problem of higher complexity in the existing optimal quadrilateral estimation technology.
The technical scheme adopted by the invention is that a character perspective correction method based on a genetic optimization algorithm is implemented according to the following steps:
step 1, opening a shot scene image Img;
step 2, preprocessing the Img, wherein the preprocessing comprises the following steps: binarization, morphological processing, text box detection and text image set extraction;
step 2.1, extracting edges of Img to obtain an edge image SImgTo SImgPerforming adaptive threshold binarization to obtain binary image BImgTo B, pairImgMorphological processing is carried out to obtain a processed image CImgDefault to CImgThe middle white point pixels are character pixels, and the black point pixels are background pixels;
step 2.2, to image CImgDetecting the character frame to obtainTo text box set RS ═ { R ═ Ri|i=1,2,…I};
Step 2.3, initializing a text frame number counter I to be 1, setting the total number of text frames to be I, and correcting the post text image set ImgRSetting the character region position set Rect as NULL, wherein NULL is an empty set;
step 2.4, to the character frame R
iExtracting external box Br
iTo external box Br
iPerforming a-time scale expansion to obtain an expansion frame RE
iAt C
ImgExtract RE therefrom
iCorresponding region of interest image
In R
iCorresponding four vertices are
Respectively correspond to R
iIn that
A middle left upper corner point, a right lower corner point and a left lower corner point;
step 3, initializing a genetic algorithm, including control parameter setting, chromosome setting, population initialization and fitness function establishment;
and 4, iteratively executing a genetic algorithm Inter _ GenProcessing, stopping iteration when the total iteration frequency gen is reached, and obtaining the chromosome at the end of iteration as Chr
i best=(P
i best1,P
i best2,P
i best3,P
i best4),Chr
i bestThe quadrangle enclosed by four vertexes is
Step 5, calculating the quadrangle
Corresponding correction rectangle
And obtaining a perspective transformation matrix H
i(ii) a Computing
External box
At C
ImgIs prepared by
Corresponding region of interest image
To pair
In the middle do not belong to
Performing a 0 operation on the pixel; by matrix H
iFor images
Correcting to obtain corrected character image
Step 6, updating the corrected image set Img
R,
Updating text region location sets
Updating a character area number counter i, i being i + 1; if I is less than or equal to I, entering the step 2.4; if I > I, entering step 7;
step 7, outputting the character image set Img after the correction of the scene image ImgRAnd a corresponding set of locations Rect.
The invention is also characterized in that:
the step 2.1 specifically comprises the following steps: calling a plurality of open source OpenCV library functions, and preprocessing the image Img: using Img as input, calling a function cvtColor (), and calculating to obtain a gray level image GImg(ii) a Handle GImgAs input, call function GaussianBlur () to get smooth image GfImg(ii) a Gf (general formula)ImgAs input, calling a function Sobel (), and calculating to obtain an x-direction gradient image Gradx and a y-direction gradient image Grady; using Gradx and Grady as input, calling function addWeighted (), calculating to obtain combined edge image SImg(ii) a Handle SImgAs input, calling a function adaptiveThreshold (), and calculating to obtain a binary image BImg(ii) a Handle BImgCalling an expansion function dilate () as an input, and calculating to obtain a morphological-processed image C to be detectedImg。
Step 2.2 text box detection is specifically implemented according to the following steps:
step 2.2.1, image CImgAs input, calling OpenCV library function findContours () of open source, and calculating to obtain extracted contour set Contours0={Con0 i|i=1,2,…N};
Step 2.2.2, combine each contour
Calling an open-source OpenCV library function contourArea () as an input, and calculating to obtain an outline Area Area
iArea of the stem
iContour elimination less than or equal to 800 to obtain a retained contour set Contours ═ Con
i|i=1,2,…I};
Step 2.2.3, associate each contour Con
iCalling an open-source OpenCV library function approxPlyDP () as an input, and calculating to obtain Con
iThe fitted polygon poly
i(ii) a Poly (o) is reacted with
iAs input, calling OpenCV library function minareaRect () of open source, and calculating to obtain poly
iMinimum circumscribed rectangle frame R of
i;R
iIs recorded as center
iSet of vertices as
Set of text boxes RS ═ R
i|i=1,2,…I}。
Step 2.4 text region extraction is specifically implemented according to the following steps:
step 2.4.1, according to the character frame R
iCenter point center
iCoordinate, will Rp
i 0The top points in the middle are arranged in the order of the upper left corner, the upper right corner, the lower right corner and the lower left corner
The arrangement method comprises the following steps:
if it is
And is
Then
If it is
And is
Then
If it is
And is
Then
If it is
And is
Then
Step 2.4.2, treating
As an input, calling an OpenCV library function bounngselect () of an open source, and calculating to obtain a text box R
iExternal connection box Br
i,Br
iSet of vertices as
External box Br
iPerforming a-time expansion to obtain an expansion frame RE
i,RE
iSet of vertices as
Wherein a is more than 0 and less than or equal to min (C)
Img.w/RE
i.w,C
Img.h/RE
iH), min () is the minimum function;
step 2.4.3, RE
i、C
ImgAs input, call OpenCV library function cvSetImageROI () from open source, at C
ImgExtract RE therefrom
iCorresponding region of interest image
R
iFour vertexes
In that
Respectively corresponding spatial positions in
Step 3 is specifically implemented according to the following steps:
step 3.1, setting control parameters as follows: the population size popsize is 4, the coding bit number chroma is 40, the total iteration number gen is 100, the crossing rate pcross is 100%, and the variation rate pmutation is 2%;
step 3.2, respectively using R
iFour vertices of
Is centered at
In extracting four square areas
N
δ(P) is represented as a neighborhood of size (2 δ +1) × (2 δ +1) centered on P, and the neighborhood set is denoted as
The chromosome extraction specifically comprises the following steps: respectively in each region of the neighborhood set
Randomly selecting a point P
mAll of P
mConstituting a chromosome chr, chr ═ (P)
1,P
2,P
3,P
4) (ii) a The chromosome code is specifically: will P
mIn that
One-dimensional pixel coordinate of (1) is recorded as L
m,0≤L
m(2 delta +1) × (2 delta +1), 0-delta 15, for L
mPerforming 10-bit binary coding to obtain code word C
mThe 40-bit code word of the chromosome chr is: 2
30C
1+2
20C
2+2
10C
3+C
4;
Step 3.3, the population initialization specifically comprises the following steps: according to the chromosome extraction method in step 3.2, in the neighborhood set
Co-extracting popsize chromosomes to form an initial population of genetic algorithm (chr)
k1, …, popsize }, wherein
Coding each chromosome of the initial population according to the coding method in the step 3.2 to obtain a code word set { cs ] of the
initial population k1, …, popsize }, wherein
Is that
The 10-bit binary code word;
step 3.4, defining fitness function
quad is the four vertices of chromosome chr
A quadrangle formed by the four corners, n is an image
The number of character pixels in the middle quadrangle quad (namely the number of white dot pixels) is s, and is the area of the quadrangle quad.
The genetic algorithm Inter _ GenProcessing in the step 4 is specifically implemented according to the following steps:
step 4.1, inputting: control parameter, chromosome inception population {
chr k1, …, popsize }, fitness function, text region
Character frame R
i(ii) a Defining chromosome population variable set in genetic algorithm iteration process as oldtop
i,oldpop
i={
oldchr i k1, …, popsize }, for oldchr
i kAssigning an initial value, oldchr
i k=chr
k(ii) a Optimal chromosome Chr in genetic algorithm iterative process
i bestInitialisation to NULL, Chr
i bestAdaptation value Chr of
i bestFitness is initialized to 0; defining an iteration variable as j, and initializing to 1;
step 4.2, calculating oldPop
iOf each chromosome oldchr
i kCorresponding to the area s of the quadrangle, counting the number n of character pixel points in the area of the quadrangle, and calculating
oldchr
i kN and s as inputs, calling the fitness function
Calculated to obtain oldchr
i kIs of the fitness value oldchr
i k.fitness;
Step 4.3, according to Roulette selection method Roulette _ Choosing (), the cluster of the population oldhop is treatediThe individuals in the population are subjected to importance sampling to obtain a new population newporti={newchri k|k=1,…,popsize};
The specific execution process of Roulette _ Chooning () is as follows:
step 4.3.1, calculate individual oldchr
i kSelection probability P (oldchr)
i k): for population oldtop
iFitness oldchr of all individuals in China
i kThe total fitness of the population is obtained by the fitness summation
P(oldchr
i k)=oldchr
i k.fitness/Fit;
Step 4.3.2, calculating population individual oldchr
i kAccumulated probability of (2)
4.3.3, performing popsize importance sampling on the individual, wherein the importance sampling process comprises the following steps: in the interval [0,1]In which a uniformly distributed random number r is generated
iIf, if
Then the population individual oldchr
i 1Is selected; if it is
Then the population individual oldchr
i kAnd (6) selecting. New population newport
iConsisting of sampled popsize individuals, i.e., newpots
i={newchr
i k|k=1,…,popsize};
Step 4.4, carrying out newport on the new population according to the cross rate pcrossiPerforming cross genetic operation on the individuals in the population, and updating the newport population by using all the individuals after the cross operationi;
Step 4.4.1, in the newport of the populationiRandomly selecting popsize multiplied by pcross individuals to carry out pairwise genetic crossing operation, wherein the specific process of the cross genetic operation is as follows: 2 in two individual code words respectively39,238,…,230Randomly selecting 4 bits from the bits for exchange operation, and respectively carrying out {2 ] operation in two individual code words29,228,…,220Randomly selecting 4 bits from the bits for exchange operation, and respectively carrying out {2 ] operation in two individual code words19,218,…,210Randomly selecting 4 bits from the bits for exchange operation, and respectively carrying out {2 ] operation in two individual code words9,28,…,20Randomly selecting 4 bits from the bits for exchange operation; updating the newport of the population by using individuals after cross operationi;
4.4.2, error correction processing of illegal code words after cross genetic operation:
the population after cross genetic operation is newcrop
i={
newchr i k1, …, popsize, each chromosome newchr
i kThe 40-bit code word is Cchr
i k,
Wherein
Is the mth 10-bit code word of the corresponding kth chromosome. For code word
And (3) carrying out illegal judgment and error correction: combining binary code words
Decimal number conversion to obtain
If it is not
Then it is considered that
For illegal code words, handle
Linear transformation to
Where int () is a rounding function,
the error-corrected code word is
A 10 bit binary number of;
step 4.5, carrying out newport on the populationiPerforming mutation genetic operation on each chromosome in the new crop plant, and performing newcrop plant operation on all the individuals after mutationiAnd (6) updating. Calculating the number of variation digits H according to the variation rate calculationbit, HbitPopsize × 40 × precipitation, population newport by number of mutationsiAll individuals in (a) were subjected to genetic variation manipulation. If the number of the mutation is less than 1, not carrying out mutation; otherwise, performing mutation operation. The mutation operation process comprises the following steps: in {239,238,…,20H is randomly selected between the digit numbersbitThe bits are varied, with a variation of 1 for a value of 0 and a variation of 0 for a value of 1;
according to the illegal code word error correction processing method in the step 4.4.2, the newport is processed to the population
iAll 10-bit code words under each chromosome are subjected to illegal judgment and error correction to obtain the population subjected to error correction
Step 4.6, updating population variable oldtop
i,oldpop
i=newpop
i(ii) a At oldpost
iSelecting individual pair population oldtop with maximum adaptability
iOptimum chromosome Chr in (1)
i maxThe value is assigned to the value to be assigned,
optimal chromosome Chr in iterative process of genetic algorithm
i bestUpdating: if Chr
i max.fitness>Chr
i bestFitness, then Chr
i best=Chr
i max(ii) a If Chr
i max.fitness≤Chr
i bestFitness, no processing is done;
step 4.7, j equals j + 1; if J is less than or equal to J, entering the step 4.2; if J is larger than J, ending the iteration process, and entering the step 4.8;
step 4.8, chromosome Chr
i bestOptimal chromosome Chr found for the end of the Inter _ Genprocessing iteration of the genetic algorithm
i best=(P
i best1,P
i best2,P
i best3,P
i best4),Chr
i bestThe quadrangle enclosed by four points is the best outer-packaging quadrangle of the character area and is marked as
Step 5, the perspective correction of the character area is implemented according to the following specific steps:
step 5.1, according to the quadrangle
Four vertices P of
i best1,P
i best2,P
i best3,P
i best4Calculating a corresponding correction rectangle
Wherein P is
i best1、w
+And h
+Respectively correspond to
Top left vertex of (1), width and height;
w+,h+the calculation is as follows:
w+=max(hor0,hor1);
h+=max(ver0,ver1);
where max () is the function of the maximum;
handle
And
as input, calling open source OpenCV library function getPerfectTransform (), and calculating to obtain perspective transformation matrix H
i;
Step 5.2, calculating
External box
Handle
And C
ImgAs input, calling the OpenCV library function cvSetImageROI () of open source to obtain C
ImgExtracted from
Corresponding region of interest image
To pair
In the middle do not belong to
The pixel value of (a) is subjected to a 0 operation,
the coordinates of the inner pixel points are marked as (x, y), the pixel values are marked as P (x, y), if
Then P (x, y) is 0;
step 5.3, define the corrected image variable as
Handle
And H
iCalling openCV library function warp Perspective () of an open source as input, and calculating to obtain
Corrected image
The invention has the beneficial effects that:
the invention relates to a character perspective correction method based on a genetic optimization algorithm, which is mainly used for correcting the distortion of the perspective of a scene text shot in a non-frontal manner, so that the complexity is reduced and the recognition rate of characters is improved during the perspective correction of the characters in the scene.
Detailed Description
The present invention will be described in detail below with reference to the accompanying drawings and specific embodiments.
The scene text correction method based on the genetic algorithm is implemented according to the following steps as shown in figure 1:
step 1, opening a shot scene image;
step 2, preparing before correction, as shown in fig. 2-4, specifically comprising the following steps:
recording a shot scene image as Img, calling a plurality of open source OpenCV library functions to carry out preprocessing operation on the Img to obtain a processed image CImgThe method is implemented by the following steps:
1) using Img as input, calling a function cvtColor (), and calculating to obtain a gray level image GImg(ii) a To reduce image noise, GImgAs input, a 3 × 3 gaussian kernel template is used, and the function GaussianBlur () is called to the grayscale image GImgPerforming Gaussian smoothing to obtain an image GfImg(ii) a Gf (general formula)ImgAs input, calling a function Sobel (), and calculating to obtain an x-direction gradient image Gradx and a y-direction gradient image Grady; using Gradx and Grady as input, calling function addWeighted (), calculating to obtain combined edge feature image SImg;
2) Processing the edge feature image SImgAs input, calling an open source OpenCV library function adaptive threshold () to perform adaptive threshold binarization processing to obtain a binary image BImg(ii) a Handle BImgAs input, calling an expansion function dilate (), and calculating to obtain the morphologically processed image C to be detectedImgDefault to CImgThe middle white point pixels are character pixels, and the black point pixels are background pixels;
② to detect the image CImgThe detection of the image text box is realized by contour extraction, and the method is implemented according to the following steps:
1) image CImgAs input, call OpenCV library function findContours () extract C of open sourceImgObtaining the external contour of all the connected regions inside to obtain the extracted contour set Contours0={Con0 i|i=1,2,…N},N is the total number of the profiles;
2) each contour Con0 iAs input, calling OpenCV library function contourArea () of open source to calculate to obtain the outline area Con0 iArea of (1)i(ii) a If AreaiIf the Area of the outline is less than or equal to 800, the Area of the outline is judged to be too small, no research significance is realized, the outline is removed from the outline set, and if the Area is less than or equal to 800, the outline is removediIf the area of the contour is more than 800, the contour area meets the condition, and the contour is stored; the remaining contour set Contours ═ Con is obtained by the above area judgmenti|i=1,2,…I};
3) Each contour ConiEach contour Con consisting of a set of spatial points in the imageiUsing a point set composed of the space points as input, calling an open-source OpenCV library function approxColDP (), and calculating to obtain the outline ConiThe fitted polygon polyiThe polygon set corresponding to the contour set Contours is designated as Con _ poly ═ poly { (poly)i|i=1,2,…I};
4) Each polygon poly
iAs input, calling OpenCV library function minareaRect () of open source, and calculating to obtain poly
iMinimum circumscribed rectangle frame R
i;R
iIs recorded as center
iSet of vertices as
Set of text boxes RS ═ R
i|i=1,2,…I};
Thirdly, initializing a frame number counter I equal to 1, setting the total number of the frames as I, and correcting the post-image set ImgRSetting the character region position set Rect as NULL, wherein NULL is an empty set;
r from the text box set RS ═
R i1,2, … I in binary image C
ImgUpper extraction of R
iCorresponding region of interest image
And extracting the image
Lower R
iCharacter(s)Frame vertex
The method is implemented by the following steps:
1) according to the character frame R
iCenter point center
iCoordinate, will Rp
i 0The top points in the middle are arranged in the order of the upper left corner, the upper right corner, the lower right corner and the lower left corner
The sorting method specifically comprises the following steps: according to R
iFour vertices and center point center
iTo determine R
iSpecific directions of the four vertices, if
And is
Then determine the top left corner point
If it is
And is
Then the upper right corner point is determined
If it is
And is
Then determine the lower right corner point
If it is
And is
Then determine the lower left corner point
Arranging according to the sequence of the upper left corner, the upper right corner, the lower right corner and the lower left corner to obtain Rp
i 0Four vertices are
2) Handle
As an input, calling an OpenCV library function bounngselect () of an open source, and calculating to obtain a text box R
iExternal connection box Br
i,Br
iSet of vertices as
External box Br
iPerforming a-time expansion to obtain an expansion frame RE
i,RE
iSet of vertices as
Wherein a is more than 0 and less than or equal to min (C)
Img.w/RE
i.w,C
Img.h/RE
iH), min () is the minimum function;
extension frame REiThe four vertices of (a) are calculated as follows:
calculate the external box Br
iWide Br of
iW, high Br
iH, center point
Coordinates of the object
And
compute extension frame RE
iWide RE of
iW and high RE
i.h: RE
i.w=a×Br
iW and high RE
i.h=a×Br
iH; compute extension frame RE
iFour vertices of
The coordinates of (a):
REiand (3) processing beyond image boundary:
3) R (R) RE
i、C
ImgAs input, call OpenCV library function cvSetImageROI () from open source, at C
ImgExtract RE therefrom
iCorresponding region of interest image
In that
In R
iThe corresponding vertex is
Step 3, preparing before genetic manipulation, specifically comprising the following steps:
setting control parameters of a genetic algorithm:
setting the population size of the genetic algorithm popsize to be 4, setting the encoding digit chrome to be 40, setting the total iteration number gen to be 100, setting the cross rate pcross to be 100 percent and setting the mutation rate pmutation to be 2 percent;
setting chromosomes: are each represented by R
iFour vertexes
Is centered at
Extracting four square areas
N
δ(P) is represented as a neighborhood of size (2 δ +1) × (2 δ +1) centered on P, and the neighborhood set is denoted as
The chromosome extraction specifically comprises the following steps: respectively in each region of the neighborhood set
Randomly selecting a point P
mAll of P
mConstituting a chromosome chr, chr ═ (P)
1,P
2,P
3,P
4) (ii) a The chromosome code is specifically: will P
mIn that
One-dimensional pixel coordinate of (1) is recorded as L
m,0≤L
m(2 δ +1) × (2 δ +1), δ 15; to L
mCarry out 10-bit binary coding to obtain code word C
mThe 40-bit code word of the chromosome chr is: 2
30C
1+2
20C
2+2
10C
3+C
4;
The initialization of the population is specifically as follows: according to the chromosome extraction method in the step II, a neighborhood set is subjected to
Co-extracting popsize chromosomes to form an initial population of genetic algorithm (chr)
k1, …, popsize }, wherein
Coding each chromosome of the initial population according to the coding method in the step II to obtain a code word set { cs ] of the
initial population k1, …, popsize }, wherein
Is that
The 10-bit binary code word;
fourthly, defining a fitness function
quad is the four vertices of chromosome chr
A quadrangle formed by the four corners, n is an image
The number of character pixels counted in the middle quadrangle quad (namely the number of white dot pixels) is s, and is the area of the quadrangle quad;
and 4, iteratively executing a genetic algorithm Inter _ GenProcessing, finishing iteration when the total iteration number gen is reached, and obtaining the chromosome at the end of iteration as Chr
i best=(P
i best1,P
i best2,P
i best3,P
i best4),Chr
i bestFour vertexes forming a quadrangle
As shown in fig. 5, the genetic algorithm Inter _ GenProcessing specifically includes the following steps:
firstly, inputting: control parameter, chromosome inception population {
chr k1, …, popsize }, fitness function, text region
And a text box R
i(ii) a Defining chromosome population variable set in genetic algorithm iteration process as oldtop
i,oldpop
i={
oldchr i k1, …, popsize }, for oldchr
i kAssigning an initial value, oldchr
i k=chr
k(ii) a Optimal chromosome Chr in genetic algorithm iteration process
i bestInitialisation to NULL, Chr
i bestAdaptation value Chr of
i bestFitness is initialized to 0; defining an iteration variable as j, and initializing to 1;
② calculate oldPop
iOf each chromosome oldchr
i kCounting the number n of character pixel points in the quadrilateral area corresponding to the quadrilateral area s, and counting
oldchr
i kN and s as inputs, calling the fitness function
Calculated to obtain oldchr
i kIs of the fitness value oldchr
i k.fitness;
③ according to Roulette selection method Roulette _ Choosing (), the cluster oldpops of the populationiThe individuals in the new population are subjected to important sampling to obtain a new population newporti={newchri k|k=1,…,popsize};
The Roulette _ sounding () method in the third embodiment is specifically implemented as follows:
computing population oldtop
iThe total fitness of Fit of (1) is,
calculating individual oldchr
i kSelection probability P (oldchr)
i k),
Calculating population individual oldchr
i kAccumulated probability of (2)
Performing popsize secondary importance sampling on the individual, wherein the primary importance sampling process comprises the following steps: in the interval [0,1]In which a uniformly distributed random number r is generated
iIf, if
Then the population individual oldchr
i 1Selected as a new individual of the population; if it is
Then the population individual oldchr
i kSelected as new individuals of the population. New population newport
iConsisting of sampled popsize individuals, i.e., newpots
i={newchr
i k|k=1,…,popsize};
Fourthly, carrying out cross genetic operation on the individuals in the new population according to the cross rate pcross, and updating the newport population by using all the individuals after the cross operationi(ii) a For new population newportiCarrying out illegal code word error correction processing on the individuals;
the crossing method in the fourth step specifically comprises the following steps:
in newport of populationiSelecting popsize X pcross individuals to carry out pairwise genetic cross operation, wherein the specific process of the cross genetic operation is as follows: 2 in two individual code words respectively39,238,…,230Randomly selecting 4 bits from the bits for exchange operation, and respectively carrying out {2 ] operation in two individual code words29,228,…,220Randomly selecting 4 bits from the bits for exchange operation, and respectively carrying out {2 ] operation in two individual code words19,218,…,210Randomly selecting 4 bits from the bits for exchange operation, and respectively carrying out {2 ] operation in two individual code words9,28,…,20Randomly select 4 bits from the bits to perform the swap operation. Updating the newport of the population by using individuals after cross operationi;
The illegal codeword error correction processing in the above fourth is specifically:
the population after cross genetic operation is newcrop
i={
newchr i k1, …, popsize, each chromosome newchr
i kThe 40-bit code word is Cchr
i k,
Wherein
Is the mth 10-bit code word of the corresponding kth chromosome. For code word
And (3) carrying out illegal judgment and error correction: combining binary code words
Decimal number conversion to obtain
If it is not
Then it is considered that
For illegal code words, handle
Linear transformation to
Where int () is a rounding function,
the error-corrected code word is
A 10 bit binary number of;
fifth to newcropiPerforming mutation genetic operation on each chromosome in the new genome, and performing newport genetic operation on all the mutated chromosomesiUpdating is carried out; for the newcrop of the population after the genetic operation of the variationiThe individual in the system carries out illegal code word error correction processing;
the mutation operation in the fifth step is specifically implemented as follows:
calculating the number of mutation bits Hbit:HbitPopsize × 40 × precipitation, population newport by number of mutationsiPerforming genetic variation operation on all individuals in the group; if the number of the mutation is less than 1, not carrying out mutation; otherwise, carrying out mutation operation; the mutation operation process comprises the following steps: in {239,238,…,20H is randomly selected between the digit numbersbitThe bits are varied, with a variation of 1 for a value of 0 and a variation of 0 for a value of 1;
the illegal codeword error correction processing in the above fifth embodiment is as follows:
according to the illegal code word error correction processing method in the step (IV), the newport of the population is correctediAll 10-bit code words under each chromosome are subjected to illegal judgment and error correction to obtain a new population newport after error correctioni;
Sixthly, updating the population variable oldtop
i,oldpop
i=newpop
i(ii) a At oldpost
iSelecting individual pair population oldtop with maximum fitness
iOptimum chromosome Chr in (1)
i maxThe value is assigned to the value to be assigned,
optimal chromosome Chr in iterative process of genetic algorithm
i bestUpdating: if Chr
i max.fitness>Chr
i bestFitness, then Chr
i best=Chr
i max(ii) a If Chr
i max.fitness≤Chr
i bestFitness, no processing is done;
seventhly, j is j + 1; if J is less than or equal to J, entering the step II; if J is more than J, ending the iterative process and entering the step (b);
' calculating optimal outsourcing quadrangle of character area
Is composed of optimal chromosome Chr
i best=(P
i best1,P
i best2,P
i best3,P
i best4) A quadrangle formed by four points;
step 5, aligning the best outsourcing quadrangle
Correcting the enclosed character area, specifically comprising the following steps:
according to the quadrangle
Four vertices P of
i best1,P
i best2,P
i best3,P
i best4Calculating a corresponding correction rectangle
Wherein P is
i best1、w
+And h
+Respectively correspond to
Upper left vertex of (d), width and height:
w+,h+the calculations are shown as (5) and (6):
where max () is the function of the maximum.
② handle
And
as input, the open source OpenCV library function getPerfectTransform () is called to be calculated
And
inter-perspective transformation matrix H
i;
Calculating
External box
Handle
And C
ImgAs input, calling the OpenCV library function cvSetImageROI () of open source to obtain C
ImgExtracted from
Corresponding region of interest image
To pair
In the middle do not belong to
The pixel value of (a) is subjected to a 0 operation,
the coordinates of the inner pixel points are marked as (x, y), the pixel values are marked as P (x, y), if
Then P (x, y) is 0; defining the rectified image variable as
Handle
And H
iAs input, calling OpenCV library function warp perspective () of open source, and calculating to obtain
Corrected image
Step 6, updating the corrected image set Img
R,
Updating text region location sets
Updating a character area number counter i, i being i + 1; if I is not more than I, entering the fourth step in the step 2; if I > I, entering step 7;
step 7, outputting the character image set Img after the correction of the scene image ImgRAnd a corresponding set of locations Rect.
The invention relates to a character perspective correction method based on a genetic optimization algorithm, which removes image noise by calling a fuzzy function in the preprocessing of an image; acquiring strong edge information of the characters by calling an edge extraction function; the binary segmentation of different scene images is realized by self-adaptive threshold binarization processing on the edge images; only relevant areas containing text contents are processed to reduce the operation complexity through contour extraction, region-of-interest extraction, text box expansion and expanded area extraction; by establishing an objective function of the ratio of the elevation to the number of white point pixels, the number of character pixels is maximized while the shape compactness of a character area is met; the importance sampling of the samples is carried out by using a roulette method, so that the high sampling rate of the samples with high fitness values can be met; by utilizing an optimization mechanism of a genetic algorithm, the time complexity for searching the optimal character outsourcing quadrangle can be reduced; the method realizes the perspective correction of the character area by utilizing the calculation of the optimal character outsourcing quadrangle, so that the method provided by the invention can reduce the complexity of the perspective correction of the characters in the scene and improve the recognition rate of the characters at the same time.