private static Image <Bgr, Byte> draw2DHistImg(DenseHistogram histDense, int width, int height) { float max_value = 0.0f; int[] a1 = new int[100]; int[] b1 = new int[100]; float ax = 0; int h_bins = histDense.BinDimension[0].Size; int s_bins = histDense.BinDimension[1].Size; CvInvoke.cvGetMinMaxHistValue(histDense, ref ax, ref max_value, a1, b1); if (max_value > 5000) { max_value -= 3000; } //如果設定的bins超過視窗設定的顯示範圍,另外給予可以符合用額外的彈出視窗顯示的值,因為要同時看到h與s的bin值,顯示的圖像寬可能會太寬 if (h_bins * s_bins > 466) { width = h_bins * s_bins * 2; } else { width = 466; } IntPtr hist_img = CvInvoke.cvCreateImage(new System.Drawing.Size(width, height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3); CvInvoke.cvZero(hist_img); IntPtr hsv_color = CvInvoke.cvCreateImage(new System.Drawing.Size(1, 1), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3); IntPtr rgb_color = CvInvoke.cvCreateImage(new System.Drawing.Size(1, 1), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3); int bin_w = width / (h_bins * s_bins); for (int h = 0; h < h_bins; h++) { for (int s = 0; s < s_bins; s++) { int i = h * s_bins + s; /** 取得直方圖的統計資料,計算值方圖中的所有顏色中最高統計的數值,作為實際顯示時的圖像高 */ //取得值方圖的數值位置,以便之後存成檔案 double bin_val = CvInvoke.cvQueryHistValue_2D(histDense, h, s); int intensity = (int)System.Math.Round(bin_val * height / max_value); /** 取得現在抓取的直方圖的hue顏色,並為了顯示成圖像可觀看,轉換成RGB色彩 */ CvInvoke.cvSet2D(hsv_color, 0, 0, new Emgu.CV.Structure.MCvScalar(h * 180.0f / h_bins, s * 255.0f / s_bins, 255, 0)); //這邊用來計算色相與飽和度的統計資料轉換到圖像上 hsv_color的數值 CvInvoke.cvCvtColor(hsv_color, rgb_color, COLOR_CONVERSION.HSV2BGR); //在把hsv顏色空間轉換為RGB Emgu.CV.Structure.MCvScalar color = CvInvoke.cvGet2D(rgb_color, 0, 0); CvInvoke.cvRectangle(hist_img, new System.Drawing.Point(i * bin_w, height), new System.Drawing.Point((i + 1) * bin_w, height - intensity), color, 2, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0); } } Image <Bgr, Byte> hist_emgu_img = new Image <Bgr, Byte>(new System.Drawing.Size(width, height)); CvInvoke.cvCopy(hist_img, hist_emgu_img.Ptr, IntPtr.Zero); CvInvoke.cvReleaseImage(ref hist_img); CvInvoke.cvReleaseImage(ref hsv_color); CvInvoke.cvReleaseImage(ref rgb_color); return(hist_emgu_img); }
public int encontrarDimensionLetra() { int tamano = 0; System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(ruta + "files/6imagenthresherode.bmp")); Emgu.CV.Image <Emgu.CV.Structure.Gray, Byte> imgt = new Emgu.CV.Image <Emgu.CV.Structure.Gray, Byte>(bmp); Emgu.CV.Structure.MCvScalar sc = new Emgu.CV.Structure.MCvScalar(); Size s = new Size(imgt.Width / 10, 1); Point p = new Point(-1, -1); Mat element = Emgu.CV.CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, s, p); Emgu.CV.CvInvoke.MorphologyEx(imgt, imgt, Emgu.CV.CvEnum.MorphOp.Close, element, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Constant, sc); guardarArchivo(imgt.Mat, "imagenCerrada"); List <int> valores = new List <int>(); Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint(); Emgu.CV.Mat hier = new Emgu.CV.Mat(); Emgu.CV.CvInvoke.FindContours(imgt, contours, hier, Emgu.CV.CvEnum.RetrType.Tree, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone); Emgu.CV.Util.VectorOfVectorOfPoint contours_poly = new Emgu.CV.Util.VectorOfVectorOfPoint(contours.Size); for (int i = 0; i < contours.Size; i++) { if (contours[i].Size > 100)//80 { Emgu.CV.CvInvoke.ApproxPolyDP((contours[i]), contours_poly[i], 3, true); Rectangle appRect = Emgu.CV.CvInvoke.BoundingRectangle(contours_poly[i]); //get the bounding rect if (appRect.Height > 2) { valores.Add(appRect.Height); } } } valores.Sort(); if (valores.Count % 2 == 0) { tamano = (valores[Convert.ToInt32(Math.Floor(valores.Count * 0.50))] + valores[Convert.ToInt32(Math.Floor(valores.Count * 0.50)) + 1]) / 2; } else { tamano = valores[Convert.ToInt32(Math.Round(valores.Count * 0.50))]; } return(tamano); }
/// <summary> ///产生均匀随机分布的随机数测试 /// </summary> public static void TestAverageRandomNumber() { int num = 10000; double min = 1e-5; double max = 3e-5; Matrix <double> m = new Matrix <double>(num, 1); for (int i = 0; i < num; i++) { m[i, 0] = GenerateRandomNumber.AverageRandom(min, max); } Emgu.CV.Structure.MCvScalar s = CvInvoke.Mean(m); Console.WriteLine("random average number generation: min is {0}, max is {1}, average is {2}", min, max, s.V0); }
/// <summary> /// 2D值方圖(色調與飽和度) 的繪製,使用emgucv提供的cvInvoke去調用opencv的函式 /// 繪製與範例的值方圖一致目前先採用 /// </summary> /// <param name="histDense"></param> /// <returns>回傳繪製值方圖的影像,直接顯示即可</returns> public static Image <Bgr, Byte> Generate2DHistogramImgForDraw(DenseHistogram histDense) { try { float max_value = 0.0f; int[] a1 = new int[100]; int[] b1 = new int[100]; float ax = 0; int h_bins = histDense.BinDimension[0].Size; int s_bins = histDense.BinDimension[1].Size; //1.使用Intptr // CvInvoke.cvGetMinMaxHistValue(histPtr, ref ax, ref max_value, a1, b1); //2.emgucv的DenseHistogram資料格式也可使用cvInvoke的openCV函式 CvInvoke.cvGetMinMaxHistValue(histDense, ref ax, ref max_value, a1, b1); /* 设置直方图显示图像 */ int height = 300; int width; //如果設定的bins超過視窗設定的顯示範圍,另外給予可以符合用額外的彈出視窗顯示的值 if (h_bins * s_bins > 800) { width = h_bins * s_bins * 2; } else { width = 800; } IntPtr hist_img = CvInvoke.cvCreateImage(new System.Drawing.Size(width, height), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3); CvInvoke.cvZero(hist_img); /* 用来进行HSV到RGB颜色转换的临时单位图像 */ IntPtr hsv_color = CvInvoke.cvCreateImage(new System.Drawing.Size(1, 1), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3); IntPtr rgb_color = CvInvoke.cvCreateImage(new System.Drawing.Size(1, 1), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 3); int bin_w = width / (h_bins * s_bins); for (int h = 0; h < h_bins; h++) { for (int s = 0; s < s_bins; s++) { int i = h * s_bins + s; /* 获得直方图中的统计次数,计算显示在图像中的高度 */ // //取得值方圖的數值位置,以便之後存成檔案 //1.Intptr //double bin_val = CvInvoke.cvQueryHistValue_2D(histPtr, h, s); //2.DenseHistogram double bin_val = CvInvoke.cvQueryHistValue_2D(histDense, h, s); int intensity = (int)System.Math.Round(bin_val * height / max_value); /* 获得当前直方图代表的颜色,转换成RGB用于绘制 */ CvInvoke.cvSet2D(hsv_color, 0, 0, new Emgu.CV.Structure.MCvScalar(h * 180.0f / h_bins, s * 255.0f / s_bins, 255, 0)); CvInvoke.cvCvtColor(hsv_color, rgb_color, COLOR_CONVERSION.CV_HSV2BGR); Emgu.CV.Structure.MCvScalar color = CvInvoke.cvGet2D(rgb_color, 0, 0); CvInvoke.cvRectangle(hist_img, new System.Drawing.Point(i * bin_w, height), new System.Drawing.Point((i + 1) * bin_w, height - intensity), color, -1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, 0); } } /* *使用openCV函式繪製 * CvInvoke.cvNamedWindow("Source"); * CvInvoke.cvShowImage("Source", this.srcImage); * CvInvoke.cvNamedWindow("H-S Histogram"); * CvInvoke.cvShowImage("H-S Histogram", hist_img); * CvInvoke.cvWaitKey(0); * */ return(EmguFormatConvetor.IplImagePointerToEmgucvImage <Bgr, Byte>(hist_img)); } catch (Exception ex) { throw new InvalidOperationException(ex.Message); } }
///<summary> Create a BGR color using the specific values</summary> ///<param name="blue"> The blue value for this color </param> ///<param name="green"> The green value for this color </param> ///<param name="red"> The red value for this color </param> public Bgr(double blue, double green, double red) { _scalar = new MCvScalar(blue, green, red); }
public static ColorDst[] ConvertColors <ColorSrc, ColorDst>(ColorSrc[] colors) where ColorSrc : struct, Emgu.CV.IColor where ColorDst : struct, Emgu.CV.IColor { if (colors == null || colors.Length == 0) { throw new ArgumentNullException("colors", "The array is null or empty."); } int dimensionSrc = colors[0].Dimension; int dimensionDst = new ColorDst().Dimension; Emgu.CV.Matrix <byte> matSrc = new Emgu.CV.Matrix <byte>(1, colors.Length, dimensionSrc); Emgu.CV.Matrix <byte> matDst = new Emgu.CV.Matrix <byte>(1, colors.Length, dimensionDst); Emgu.CV.CvEnum.COLOR_CONVERSION code = Emgu.CV.Util.CvToolbox.GetColorCvtCode( typeof(ColorSrc), typeof(ColorDst) ); // Copy colors into matSrc for (int i = 0; i < colors.Length; i++) { Emgu.CV.Structure.MCvScalar colorComp = colors[i].MCvScalar; if (typeof(Rgba).IsAssignableFrom(typeof(ColorSrc))) { double swap = colorComp.v0; colorComp.v0 = colorComp.v2; colorComp.v2 = swap; } if (dimensionSrc > 0) { matSrc.Data[0, i *dimensionSrc + 0] = (byte)colorComp.v0; } if (dimensionSrc > 1) { matSrc.Data[0, i *dimensionSrc + 1] = (byte)colorComp.v1; } if (dimensionSrc > 2) { matSrc.Data[0, i *dimensionSrc + 2] = (byte)colorComp.v2; } if (dimensionSrc > 3) { matSrc.Data[0, i *dimensionSrc + 3] = (byte)colorComp.v3; } } // Convert colors Emgu.CV.CvInvoke.cvCvtColor(matSrc, matDst, code); // Copy matDst into new color array ColorDst[] newColors = new ColorDst[colors.Length]; for (int i = 0; i < colors.Length; i++) { newColors[i] = new ColorDst(); Emgu.CV.Structure.MCvScalar colorComp = new Emgu.CV.Structure.MCvScalar(); if (dimensionDst > 0) { colorComp.v0 = matDst.Data[0, i *dimensionDst + 0]; } if (dimensionDst > 1) { colorComp.v1 = matDst.Data[0, i *dimensionDst + 1]; } if (dimensionDst > 2) { colorComp.v2 = matDst.Data[0, i *dimensionDst + 2]; } if (dimensionDst > 3) { colorComp.v3 = matDst.Data[0, i *dimensionDst + 3]; } newColors[i].MCvScalar = colorComp; } return(newColors); }
/// <summary> /// Create a Bgr color using the System.Drawing.Color /// </summary> /// <param name="winColor">System.Drawing.Color</param> public Bgr(System.Drawing.Color winColor) { _scalar = new MCvScalar(winColor.B, winColor.G, winColor.R); }
/// <summary> Create a Hls color using the specific values</summary> /// <param name="hue"> The hue value for this color ( 0 < hue < 180 ) </param> /// <param name="satuation"> The satuation for this color </param> /// <param name="lightness"> The lightness for this color </param> public Hls(double hue, double lightness, double satuation) { _scalar = new MCvScalar(hue, lightness, satuation); }
///<summary> Create a Gray color with the given intensity</summary> ///<param name="intensity"> The intensity for this color </param> public Gray(double intensity) { _scalar = new MCvScalar(intensity); }
/// <summary> /// Create a Bgr color using the System.Drawing.Color /// </summary> /// <param name="winColor">System.Drawing.Color</param> public Bgr(UnityEngine.Color winColor) { _scalar = new MCvScalar(winColor.b * 255.0, winColor.g * 255.0, winColor.r * 255.0); }
///<summary> Create a HSV color using the specific values</summary> ///<param name="hue"> The hue value for this color ( 0 < hue < 180 ) </param> ///<param name="satuation"> The satuation value for this color </param> ///<param name="value"> The value for this color </param> public Hsv(double hue, double satuation, double value) { _scalar = new MCvScalar(hue, satuation, value); }
///<summary> Create a BGRA color using the specific values</summary> ///<param name="blue"> The blue value for this color </param> ///<param name="green"> The green value for this color </param> ///<param name="red"> The red value for this color </param> ///<param name="alpha"> The alpha value for this color</param> public Bgra(double blue, double green, double red, double alpha) { _scalar = new MCvScalar(blue, green, red, alpha); }
/// <summary> Create a Xyz color using the specific values</summary> /// <param name="z"> The z value for this color </param> /// <param name="y"> The y value for this color </param> /// <param name="x"> The x value for this color </param> public Xyz(double x, double y, double z) { _scalar = new MCvScalar(x, y, z); }
public List <Rectangle> detectLetters(Emgu.CV.Image <Emgu.CV.Structure.Bgr, Byte> imgOrig) { List <Rectangle> boundRect = new List <Rectangle>(); Emgu.CV.Mat img_gray = new Emgu.CV.Mat(); Emgu.CV.Mat img_sobel = new Emgu.CV.Mat(); Emgu.CV.Mat img_threshold = new Emgu.CV.Mat(); Emgu.CV.Mat element = new Emgu.CV.Mat(); Emgu.CV.Mat mascara = new Emgu.CV.Mat(); Emgu.CV.Structure.MCvScalar sc = new Emgu.CV.Structure.MCvScalar(); Mat img = imgOrig.Mat; string layoutr = ruta + "files/" + layout + ".bmp"; System.Drawing.Bitmap bmplayout = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(layoutr)); Emgu.CV.Image <Emgu.CV.Structure.Gray, Byte> imageLayout = new Emgu.CV.Image <Emgu.CV.Structure.Gray, Byte>(bmplayout); guardarArchivo(img, "1ImageWork"); Emgu.CV.CvInvoke.CvtColor(img, img_gray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); Emgu.CV.CvInvoke.Threshold(img_gray, img_threshold, umbral, 255, Emgu.CV.CvEnum.ThresholdType.Otsu); Emgu.CV.CvInvoke.BitwiseNot(img_threshold, img_threshold); Emgu.CV.CvInvoke.BitwiseAnd(img_threshold, imageLayout, img_threshold); guardarArchivo(img_threshold, "2threshImage"); Emgu.CV.Mat verticalStructure = Emgu.CV.CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(2, 1), new Point(-1, -1)); Emgu.CV.CvInvoke.BitwiseNot(img_threshold, img_threshold); img_threshold = detectarLineas(img_threshold); Emgu.CV.CvInvoke.BitwiseNot(img_threshold, img_threshold); Emgu.CV.CvInvoke.Erode(img_threshold, img_threshold, verticalStructure, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new Emgu.CV.Structure.MCvScalar()); guardarArchivo(img_threshold, "6imagenthresherode"); dim = encontrarDimensionLetra(); verticalStructure = Emgu.CV.CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(2, 2), new Point(-1, -1)); Emgu.CV.CvInvoke.Dilate(img_threshold, img_threshold, verticalStructure, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new Emgu.CV.Structure.MCvScalar()); guardarArchivo(img_threshold, "7imagenthreshdilate"); //proporcion de letra 3/4 de altura cerradura aplicada entre 3 letras tomando en cuenta espacios Size s = new Size(Convert.ToInt32(Math.Round(dim * 0.75 * 3)), 2);//modificado 40-3 Point p = new Point(-1, -1); element = Emgu.CV.CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, s, p); guardarArchivo(element, "kernel"); Emgu.CV.CvInvoke.MorphologyEx(img_threshold, img_threshold, Emgu.CV.CvEnum.MorphOp.Close, element, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Constant, sc); guardarArchivo(img_threshold, "8morphoImage"); verticalStructure = Emgu.CV.CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(4, 4), new Point(-1, -1)); Emgu.CV.CvInvoke.Erode(img_threshold, img_threshold, verticalStructure, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new Emgu.CV.Structure.MCvScalar()); guardarArchivo(img_threshold, "9imagenthresherode"); Emgu.CV.CvInvoke.Dilate(img_threshold, img_threshold, verticalStructure, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new Emgu.CV.Structure.MCvScalar()); guardarArchivo(img_threshold, "10imagenthreshdilate"); Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint(); Emgu.CV.Mat hier = new Emgu.CV.Mat(); Emgu.CV.CvInvoke.FindContours(img_threshold, contours, hier, Emgu.CV.CvEnum.RetrType.Tree, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone); Emgu.CV.Util.VectorOfVectorOfPoint contours_poly = new Emgu.CV.Util.VectorOfVectorOfPoint(contours.Size); for (int i = 0; i < contours.Size; i++) { { Emgu.CV.CvInvoke.ApproxPolyDP((contours[i]), contours_poly[i], 3, true); Rectangle appRect = Emgu.CV.CvInvoke.BoundingRectangle(contours_poly[i]); //get the bounding rect if (appRect.Height > dim / 2) { appRect.Height = appRect.Height + dim / 2; appRect.Width = appRect.Width + dim * 3 / 4; appRect.Y = appRect.Y - dim / 4; appRect.X = appRect.X - dim * 3 / 4; boundRect.Add(appRect); } } } return(boundRect); }
/// <summary> /// Return true if the two color equals /// </summary> /// <param name="other">The other color to compare with</param> /// <returns>true if the two color equals</returns> public bool Equals(Bgr other) { return(MCvScalar.Equals(other.MCvScalar)); }
public int findGesture(List <double[]> lInGeste, List <List <double[]> > references) { int ind = -1; double minFEMD = 9999.0; int refIndex = 0; textBox1.FontSize = 36; textBox1.Text = ""; foreach (List <double[]> refGeste in references) { int nInGeste = lInGeste.Count; int nRefGeste = refGeste.Count; IntPtr dMat = CvInvoke.cvCreateMat(nInGeste, nRefGeste, Emgu.CV.CvEnum.MAT_DEPTH.CV_32F); Matrix <float> mFlow = new Matrix <float>(nInGeste, nRefGeste); Matrix <float> mS1 = new Matrix <float>(nInGeste, 1); Matrix <float> mS2 = new Matrix <float>(nRefGeste, 1); for (int i = 0; i < nInGeste; i++) { mS1[i, 0] = (float)lInGeste[i][2]; for (int j = 0; j < nRefGeste; j++) { mS2[j, 0] = (float)refGeste[j][2]; double dist = Math.Min(Math.Abs(lInGeste[i][0] - refGeste[j][0]), Math.Abs(lInGeste[i][1] - refGeste[j][1])); Emgu.CV.Structure.MCvScalar mDist = new Emgu.CV.Structure.MCvScalar(dist); CvInvoke.cvSet2D(dMat, i, j, mDist); } } try { if ((mS1.Height != 0) && (mS2.Height != 0) && (mFlow.Height != 0)) { float emd = CvInvoke.cvCalcEMD2(mS1, mS2, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_USER, null, dMat, mFlow, new System.IntPtr(0), new System.IntPtr(0)); //Console.Write("EMD: "); //Console.WriteLine(emd); double F = mFlow.Sum; double wDiff = mS1.Sum - mS2.Sum; // Console.WriteLine(wDiff); //Console.WriteLine(F); double FEMD = 0.5 * emd + 0.5 * Math.Abs(wDiff) / F; //Console.Write("FEMD: "); //Console.WriteLine(FEMD); if (FEMD < minFEMD && FEMD < 0.3) { minFEMD = FEMD; ind = refIndex + 1; } refIndex++; } } catch (Emgu.CV.Util.CvException) { } } // textBox1.Text = ind.ToString(); return(ind); }
///<summary> Create a RGB color using the specific values</summary> ///<param name="blue"> The blue value for this color </param> ///<param name="green"> The green value for this color </param> ///<param name="red"> The red value for this color </param> public Rgb(double red, double green, double blue) { _scalar = new MCvScalar(red, green, blue); }