123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- using OpenCvSharp;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SmartCoalApplication.Base.CommTool
- {
- /// <summary>
- /// 防焊的图像预处理
- /// </summary>
- public class FangHanTools
- {
- /// <summary>
- /// 二值化右侧的值
- /// </summary>
- public static int rightV = 0;
- /// <summary>
- /// 二值化mat
- /// </summary>
- public static Mat mat_er, final, mat_er1;
- /// <summary>
- /// 颜色
- /// </summary>
- public static Vec4b vec4B = new Vec4b(255, 0, 0, 255);
- public unsafe static Mat RemoveCircle(Mat srcImage)
- {
- Mat gray = null;
- try
- {
- Mat clone = srcImage.Clone();
- gray = srcImage.CvtColor(ColorConversionCodes.BGR2GRAY);
- //计算灰度平均值
- Scalar mean_rgb = srcImage.Mean();
- Scalar mean = gray.Mean(gray);
- //计算直方图
- float[] hist_float = HistTools.CalcHist(gray);
- List<OpenCvSharp.Point[]> contoursAll = new List<OpenCvSharp.Point[]>();
- //循环直方图数据
- //for (int i = 0; i < mean[0]; i += 2)
- //{
- // if (hist_float[i] > 0)
- // {
- // rightV = i;
- // ////进行二值
- // //mat_er = new Mat(gray.Rows, gray.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
- // //gray.ForEachAsByte(GrayForEachAsByte);
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray_0\_" + rightV + ".png", mat_er);
- // ////去除边界对象
- // ////mat_er = BinaryTools.DeleteContours_New(mat_er);
- // ////去碎屑
- // //mat_er = BinaryTools.DebrisRemoval_New(mat_er);
- // ////空洞填充
- // //mat_er = BinaryTools.HoleFilling_NewWithView(mat_er, vec4B);
- // //Cv2.Dilate(mat_er, mat_er, null, null, 4);
- // //mat_er1 = mat_er.CvtColor(ColorConversionCodes.BGR2GRAY);
- // //mat_er1 = ~mat_er1;
- // ////然后寻找轮廓
- // //OpenCvSharp.Point[][] contours;
- // //HierarchyIndex[] hierachy;
- // //Cv2.FindContours(mat_er1, out contours, out hierachy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
- // ////然后找近似圆的
- // //if (contours.Length > 0)
- // //{
- // // int p = 0;
- // // foreach (OpenCvSharp.Point[] ps in contours)
- // // {
- // // if (hierachy[p].Parent == -1 && !BinaryTools.WithinContour(contoursAll, ps) && BinaryTools.CalcNodularity(ps) > 0.75)
- // // {
- // // contoursAll.Add(ps);
- // // }
- // // p++;
- // // }
- // //}
- // }
- //}
- //使用轮廓在空白图上绘制并填充,然后腐蚀
- mat_er = new Mat(gray.Rows, gray.Cols, MatType.CV_8UC4, new Scalar(0));
- Cv2.FillPoly(mat_er, contoursAll, new Scalar(255), LineTypes.Link4);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (2)_2.JPG", mat_er);
- mat_er = mat_er.CvtColor(ColorConversionCodes.BGR2GRAY);
- //对结果图进行二值提取和空洞填充
- //进行二值
- final = new Mat(mat_er.Rows, mat_er.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 255));
- mat_er.ForEachAsByte(GrayForEachAsByte_final);
- //空洞填充
- final = BinaryTools.HoleFilling_NewWithView(final, vec4B);
- //反色
- final = final.CvtColor(ColorConversionCodes.BGR2GRAY);
- //膨胀
- Cv2.Erode(final, final, null, null, 6);
- for (int h = 0; h < final.Height; h++)
- {
- for (int w = 0; w < final.Width; w++)
- {
- if (final.At<byte>(h, w) < 255)
- clone.Set<Vec4b>(h, w, new Vec4b((byte)mean_rgb[0], (byte)mean_rgb[1], (byte)mean_rgb[2], 255));
- }
- }
- return clone;
- }
- catch(Exception)
- {
- }
- finally
- {
- if(mat_er!=null && !mat_er.IsDisposed)
- {
- mat_er.Dispose();
- mat_er = null;
- }
- if (final != null && !final.IsDisposed)
- {
- final.Dispose();
- final = null;
- }
- if (gray != null && !gray.IsDisposed)
- {
- gray.Dispose();
- gray = null;
- }
- rightV = 0;
- System.GC.Collect();
- }
- return srcImage;
- }
- public unsafe static Mat RemoveCircle(Mat srcImage, out int mean0, out Scalar mean_rgb)
- {
- Mat gray = null;
- mean0 = 0; mean_rgb = new Scalar();
- try
- {
- Mat clone = srcImage.Clone();
- gray = srcImage.CvtColor(ColorConversionCodes.BGR2GRAY);
- //计算灰度平均值
- /*Scalar */mean_rgb = srcImage.Mean();
- Scalar mean = gray.Mean(gray);
- //计算直方图
- float[] hist_float = HistTools.CalcHist(gray);
- List<OpenCvSharp.Point[]> contoursAll = new List<OpenCvSharp.Point[]>();
- mean0 = (int)mean[0];
- //循环直方图数据
- //for (int i = 0; i < mean[0]; i += 2)
- //{
- // if (hist_float[i] > 0)
- // {
- // rightV = i;
- // ////进行二值
- // //mat_er = new Mat(gray.Rows, gray.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
- // //gray.ForEachAsByte(GrayForEachAsByte);
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray_0\_" + rightV + ".png", mat_er);
- // ////去除边界对象
- // ////mat_er = BinaryTools.DeleteContours_New(mat_er);
- // ////去碎屑
- // //mat_er = BinaryTools.DebrisRemoval_New(mat_er);
- // ////空洞填充
- // //mat_er = BinaryTools.HoleFilling_NewWithView(mat_er, vec4B);
- // //Cv2.Dilate(mat_er, mat_er, null, null, 4);
- // //mat_er1 = mat_er.CvtColor(ColorConversionCodes.BGR2GRAY);
- // //mat_er1 = ~mat_er1;
- // ////然后寻找轮廓
- // //OpenCvSharp.Point[][] contours;
- // //HierarchyIndex[] hierachy;
- // //Cv2.FindContours(mat_er1, out contours, out hierachy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
- // ////然后找近似圆的
- // //if (contours.Length > 0)
- // //{
- // // int p = 0;
- // // foreach (OpenCvSharp.Point[] ps in contours)
- // // {
- // // if (hierachy[p].Parent == -1 && !BinaryTools.WithinContour(contoursAll, ps) && BinaryTools.CalcNodularity(ps) > 0.75)
- // // {
- // // contoursAll.Add(ps);
- // // }
- // // p++;
- // // }
- // //}
- // }
- //}
- //使用轮廓在空白图上绘制并填充,然后腐蚀
- mat_er = new Mat(gray.Rows, gray.Cols, MatType.CV_8UC4, new Scalar(0));
- Cv2.FillPoly(mat_er, contoursAll, new Scalar(255), LineTypes.Link4);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (2)_2.JPG", mat_er);
- mat_er = mat_er.CvtColor(ColorConversionCodes.BGR2GRAY);
- //对结果图进行二值提取和空洞填充
- //进行二值
- final = new Mat(mat_er.Rows, mat_er.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 255));
- mat_er.ForEachAsByte(GrayForEachAsByte_final);
- //空洞填充
- final = BinaryTools.HoleFilling_NewWithView(final, vec4B);
- //反色
- final = final.CvtColor(ColorConversionCodes.BGR2GRAY);
- //膨胀
- Cv2.Erode(final, final, null, null, 6);
- for (int h = 0; h < final.Height; h++)
- {
- for (int w = 0; w < final.Width; w++)
- {
- if (final.At<byte>(h, w) < 255)
- clone.Set<Vec4b>(h, w, new Vec4b((byte)mean_rgb[0], (byte)mean_rgb[1], (byte)mean_rgb[2], 255));
- }
- }
- return clone;
- }
- catch (Exception)
- {
- }
- finally
- {
- if (mat_er != null && !mat_er.IsDisposed)
- {
- mat_er.Dispose();
- mat_er = null;
- }
- if (final != null && !final.IsDisposed)
- {
- final.Dispose();
- final = null;
- }
- if (gray != null && !gray.IsDisposed)
- {
- gray.Dispose();
- gray = null;
- }
- rightV = 0;
- System.GC.Collect();
- }
- return srcImage;
- }
- public unsafe static Mat RemoveCircle(Mat srcImage, int mean0, Scalar mean_rgb, out bool foundCircle)
- {
- Mat gray = null;
- foundCircle = false;
- //mean0 = 0;
- try
- {
- Mat clone = srcImage.Clone();
- gray = srcImage.CvtColor(ColorConversionCodes.BGR2GRAY);
- ////计算灰度平均值
- //Scalar mean_rgb = srcImage.Mean();
- //Scalar mean = gray.Mean(gray);
- //计算直方图
- float[] hist_float = HistTools.CalcHist(gray);
- List<OpenCvSharp.Point[]> contoursAll = new List<OpenCvSharp.Point[]>();
- //mean0 = (int)mean[0];
- //循环直方图数据
- for (int i = 0; i < mean0/*mean[0]*/; i += 2)
- {
- if (hist_float[i] > 0)
- {
- rightV = i;
- //进行二值
- mat_er = new Mat(gray.Rows, gray.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
- gray.ForEachAsByte(GrayForEachAsByte);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray_0\_" + rightV + ".png", mat_er);
- //去除边界对象
- //mat_er = BinaryTools.DeleteContours_New(mat_er);
- //去碎屑
- mat_er = BinaryTools.DebrisRemoval_New(mat_er);
- //空洞填充
- mat_er = BinaryTools.HoleFilling_NewWithView(mat_er, vec4B);
- Cv2.Dilate(mat_er, mat_er, null, null, 4);
- mat_er1 = mat_er.CvtColor(ColorConversionCodes.BGR2GRAY);
- mat_er1 = ~mat_er1;
- //然后寻找轮廓
- OpenCvSharp.Point[][] contours;
- HierarchyIndex[] hierachy;
- Cv2.FindContours(mat_er1, out contours, out hierachy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
- //然后找近似圆的
- if (contours.Length > 0)
- {
- int p = 0;
- foreach (OpenCvSharp.Point[] ps in contours)
- {
- if (hierachy[p].Parent == -1 && !BinaryTools.WithinContour(contoursAll, ps) && BinaryTools.CalcNodularity(ps) > 0.75)
- {
- contoursAll.Add(ps);
- }
- p++;
- }
- }
- }
- }
- //使用轮廓在空白图上绘制并填充,然后腐蚀
- mat_er = new Mat(gray.Rows, gray.Cols, MatType.CV_8UC4, new Scalar(0));
- Cv2.FillPoly(mat_er, contoursAll, new Scalar(255), LineTypes.Link4);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (2)_2.JPG", mat_er);
- mat_er = mat_er.CvtColor(ColorConversionCodes.BGR2GRAY);
- //对结果图进行二值提取和空洞填充
- //进行二值
- final = new Mat(mat_er.Rows, mat_er.Cols, MatType.CV_8UC4, new Scalar(0, 0, 0, 255));
- mat_er.ForEachAsByte(GrayForEachAsByte_final);
- //空洞填充
- final = BinaryTools.HoleFilling_NewWithView(final, vec4B);
- //反色
- final = final.CvtColor(ColorConversionCodes.BGR2GRAY);
- //膨胀
- Cv2.Erode(final, final, null, null, 6);
- for (int h = 0; h < final.Height; h++)
- {
- for (int w = 0; w < final.Width; w++)
- {
- if (final.At<byte>(h, w) < 255)
- {
- if (!foundCircle) foundCircle = true;
- break;
- //clone.Set<Vec4b>(h, w, new Vec4b((byte)mean_rgb[0], (byte)mean_rgb[1], (byte)mean_rgb[2], 255));
- }
- }
- if (foundCircle)
- {
- break;
- }
- }
- return foundCircle ? final.Clone() : clone;
- }
- catch (Exception)
- {
- }
- finally
- {
- if (mat_er != null && !mat_er.IsDisposed)
- {
- mat_er.Dispose();
- mat_er = null;
- }
- if (final != null && !final.IsDisposed)
- {
- final.Dispose();
- final = null;
- }
- if (gray != null && !gray.IsDisposed)
- {
- gray.Dispose();
- gray = null;
- }
- rightV = 0;
- System.GC.Collect();
- }
- return srcImage;
- }
- private unsafe static void GrayForEachAsByte_final(byte* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- byte v = *value;
- if (v > 0)
- {
- final.Set<Vec4b>(y, x, vec4B);
- }
- }
- private unsafe static void GrayForEachAsByte(byte* value, int* position)
- {
- int y = position[0];
- int x = position[1];
- byte v = *value;
- if (v > 0 && v <= rightV)
- {
- mat_er.Set<Vec4b>(y, x, vec4B);
- }
- /*else
- {
- mat_er.Set<Vec4b>(y, x, new Vec4b(0,0,0,0));
- }*/
- }
- }
- }
|