Emgucv不完整图像分割试验(五)——透视变换&&OCR(自带的Tesseract)
程序员文章站
2023-12-27 09:05:33
...
再次吐槽Emgucv的版本升级后代码不能用,修正代码如下:
PointF[] srcquad = new PointF[4];
PointF[] dstquad = new PointF[4];
srcquad[0] = pa;
srcquad[1] = pc;
srcquad[2] = pd;
srcquad[3] = pb;
dstquad[0] = new PointF(0, 0);
dstquad[1] = new PointF(this.imageBox2.Width, 0);
dstquad[2] = new PointF(0, this.imageBox2.Height);
dstquad[3] = new PointF(this.imageBox2.Width, this.imageBox2.Height);
Image<Bgr, byte> tempRGBimage = 原始图.ToImage<Bgr, byte>();
Image<Bgr, byte> tempSmallImage = new Image<Bgr, byte>(this.imageBox2.Size);
//生成透视变换矩阵
//Emgu.CV.CameraCalibration.GetPerspectiveTransform();IntPtr map_matrix=new Matrix<double>(3,3);
//进行透视变换(第三个参数决定了矩形框之外的部分进行处理的方式,现在所选为用黑色填充)
Mat transformation = CvInvoke.EstimateRigidTransform(srcquad, dstquad, true);
CvInvoke.WarpAffine(tempRGBimage, tempSmallImage, transformation, this.imageBox2.Size);
this.imageBox2.Image = tempSmallImage;
自带的Tesseract也是,版本升级后有报错,修正代码如下:
_ocr = new Tesseract("", "eng",OcrEngineMode.TesseractLstmCombined);
_ocr.SetVariable("tessedit_char_whitelist", "0123456789");
List<Bitmap> CutImage_List = new List<Bitmap>();
try
{
Image<Gray, byte> gray = new Image<Gray, byte>(new Bitmap(this.imageBox2.Image.Bitmap));
_ocr.SetImage(gray);//就这一步,否则会报内存错误
_ocr.Recognize();
Tesseract.Character[] charactors = _ocr.GetCharacters();
for (int i = 0; i < charactors.Length; i++)
{ this.textBox5.Text += charactors[i].Text; }
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
最后结果如图:
初开博客,目的是交流与合作,本人QQ:273651820。