private void Btn_answerReg3_Click(object sender, EventArgs e) { CVHelper commonUse = new CVHelper(); var src = new Image <Gray, byte>(ib_middleCut.Image.Bitmap); var thresholdImage = src.CopyBlank(); int myThreshold = 210; CvInvoke.Threshold(src, thresholdImage, myThreshold, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv); commonUse.SaveMat(thresholdImage.Mat, "二值化后"); //思路 close -腐蚀-腐蚀-膨胀 //形态学膨胀 Mat mat_dilate = commonUse.MyDilate(thresholdImage.Mat, Emgu.CV.CvEnum.MorphOp.Close); commonUse.SaveMat(mat_dilate, "形态学膨胀"); //mat_dilate = commonUse.MyDilate(mat_dilate, Emgu.CV.CvEnum.MorphOp.Close); //commonUse.SaveMat(mat_dilate, "形态学膨胀1"); mat_dilate = commonUse.MyDilate(mat_dilate, Emgu.CV.CvEnum.MorphOp.Erode); commonUse.SaveMat(mat_dilate, "形态学膨胀腐蚀1"); mat_dilate = commonUse.MyDilate(mat_dilate, Emgu.CV.CvEnum.MorphOp.Erode); commonUse.SaveMat(mat_dilate, "形态学膨胀腐蚀2"); mat_dilate = commonUse.MyDilate(mat_dilate, Emgu.CV.CvEnum.MorphOp.Dilate); commonUse.SaveMat(mat_dilate, "形态学膨胀2"); var image_dilate = mat_dilate.ToImage <Gray, byte>(); List <Rectangle> validRectList = new List <Rectangle>(); CutedRectList.ForEach(rect => { var newRect = new Rectangle(Math.Max(0, rect.X - 8), Math.Max(0, rect.Y - 8), rect.Width, rect.Height); var tmpImage = image_dilate.Copy(newRect); var result = GetWhiteColorPercenter(tmpImage); if (result > 0.2) { validRectList.Add(rect); } commonUse.SaveMat(tmpImage.Mat, "形态学后" + result); }); validRectList.ForEach(r => { CvInvoke.Rectangle(src, r, new MCvScalar(0, 0, 255)); }); new CVHelper().SaveMat(src.Mat, "通过比例计算获取的答案"); this.ib_result.Image = src; }
private void Btn_answerReg4_Click(object sender, EventArgs e) { if (this.ib_middleCut.Image == null) { MessageBox.Show("裁剪图片不能为空"); return; } CVHelper commonUse = new CVHelper(); var centerList = new List <Point>(); if (!this.ck_IsIntell.Checked && !this.ckb_ANN.Checked) { centerList = commonUse.GetCenterPointListFromBitmapByWhiteArea(this.ib_middleCut.Image.Bitmap, CutedRectList); Mat tmpMat = new Image <Bgr, byte>(this.ib_middleCut.Image.Bitmap).Mat; centerList.ForEach(p => { CvInvoke.Circle(tmpMat, p, 6, new MCvScalar(0, 0, 255), 2); }); this.ib_result.Image = tmpMat; } else { var rectList = new List <Rectangle>(); CutedRectList.ForEach(r => { r.Offset(destRect.Location); //int offsetX = Math.Min(5, r.X), offsetY = Math.Min(5, r.Y); //r.Offset(-offsetX, -offsetY); rectList.Add(r); }); CVArea area = new CVArea(this.destRect, commonUse.OrderRectList(rectList, IsFillFull: false), name: "测试卷"); if (this.ck_IsIntell.Checked) { centerList = commonUse.GetCenterPointListFromBitmapByWhiteArea(this.ib_original.Image.Bitmap, new List <CVArea>() { area }); } else if (this.ckb_ANN.Checked) { centerList = commonUse.GetCenterPointListFromBitmapByWhiteAreaByML(this.ib_original.Image.Bitmap, new List <CVArea>() { area }); } Mat tmpMat = new Image <Bgr, byte>(this.ib_original.Image.Bitmap).Mat; centerList.ForEach(p => { CvInvoke.Circle(tmpMat, p, 6, new MCvScalar(0, 0, 255), 2); }); this.ib_result.Image = tmpMat; this.CurrentArea = area; return; } }