public static Image<Gray, float> GetQuantificationMatrix(bool LuminanceOrChrominance, int compfactor) { float[] LumQuant = { 16,11,10,16,24,40,51,61, 12,12,14,19,26,58,60,55, 14,13,16,24,40,57,69,56, 14,17,22,29,51,87,80,62, 18,22,37,56,68,109,103,77, 24,35,55,64,81,104,113,92, 49,64,78,87,103,121,120,101, 72,92,95,98,112,100,103,99}; float[] ChrQuant = {17,18,24,47,99,99,99,99, 18,21,26,66,99,99,99,99, 24,26,56,99,99,99,99,99, 47,66,99,99,99,99,99,99, 99,99,99,99,99,99,99,99, 99,99,99,99,99,99,99,99, 99,99,99,99,99,99,99,99, 99,99,99,99,99,99,99,99 }; Image<Gray, float> matrix = new Image<Gray, float>(8, 8); int idx = 0; float[] Quant = (LuminanceOrChrominance) ? LumQuant : ChrQuant; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { matrix[y, x] = new Gray(Quant[idx++] * 100 / compfactor); } } return matrix; }
public static List<Point>[] FindConturs(Image<Gray, Byte> src, Gray bg, Gray fg) { List<Point> upper = new List<Point>(); List<Point> lower = new List<Point>(); int height = src.Height, width = src.Width; Gray last = bg; for (int x = 0; x < width; ++x) { for (int y = 0; y <= height; ++y) { Gray current; if (y == height) current = bg; else current = src[y, x]; if (!last.Equals(current)) { Point p = new Point(x, y); if (last.Equals(bg)) { // 背景 -> 前景 upper.Add(p); } else { // 前景 -> 背景 lower.Add(p); } last = current; } } } return new List<Point>[2] { upper, lower }; }
public static Image<Gray, Byte> Equalize(this Image<Gray, Byte> src, int m, int n) { int[] hist_old = new int[n]; int grade = 256 / n; for (int y = 0; y < src.Height; ++y) { for (int x = 0; x < src.Width; ++x) { int g = 256 - grade - (int)src[y, x].Intensity; hist_old[g / grade]++; } } double sum = hist_old.Sum(); var p = hist_old.Select(h => h / sum).ToArray(); int[] index = new int[n]; index[0] = 0; for (int i = 1; i < n; ++i) { p[i] += p[i - 1]; index[i] = (int)Math.Round(p[i] * (m - 1)); } double[] newgraylevel = new double[m]; double newgrade = 255.0 / m; for (int i = 0; i < m; ++i) { newgraylevel[i] = 255 - newgrade - i * newgrade; } var dst = new Image<Gray, Byte>(src.Width, src.Height); for (int y = 0; y < src.Height; ++y) { for (int x = 0; x < src.Width; ++x) { int g = 256 - grade - (int)src[y, x].Intensity; int level = g / grade; dst[y, x] = new Gray(newgraylevel[index[level]]); } } return dst; }
private void button_threshold_Click(object sender, EventArgs e) { Gray thresholdValue = new Gray(55); Image <Gray, byte> thresholdImage = imgTemp.ToImage <Gray, byte>().ThresholdBinary(thresholdValue, new Gray(255)); imgTemp = thresholdImage.Mat; //pictureBox.Image = thresholdImage.ToBitmap(); }
/// <summary> /// Computes moments for the provided image. /// </summary> /// <param name="image">Image.</param> /// <param name="area">Area</param> public void Compute(Gray <float>[,] image, Rectangle area) { RawMoments rawMoments = new RawMoments(Order); rawMoments.Compute(image, area); this.Compute(rawMoments); }
/// <summary> /// Rectification filter for projective transformation. /// <para>Accord.NET internal call. Please see: <see cref="Accord.Imaging.Filters.Rectification"/> for details.</para> /// </summary> /// <param name="img">Image.</param> /// <param name="homography">The homography matrix used to map a image passed to the filter to the overlay image.</param> /// <param name="fillColor">The filling color used to fill blank spaces.</param> /// <returns>Rectified image.</returns> public static Gray <byte>[,] Rectification(this Gray <byte>[,] img, double[,] homography, Gray <byte> fillColor) { Rectification r = new Rectification(homography); r.FillColor = fillColor.ToColor(); return(img.ApplyBaseTransformationFilter(r)); }
public static Image<Gray, Byte> stripBorder(Image<Gray, Byte> image, Gray threshold) { bool found; int left; found = false; for (left = 0; left < image.Cols; left++) { for (int r = 0; r < image.Rows && !found; ++r) if (image[r, left].Intensity >= threshold.Intensity) found = true; if (found) break; } int right; found = false; for (right = image.Cols - 1; right >= 0; right--) { for (int r = 0; r < image.Rows && !found; ++r) if (image[r, right].Intensity >= threshold.Intensity) found = true; if (found) break; } int top; found = false; for (top = 0; top < image.Rows; top++) { for (int c = 0; c < image.Cols && !found; ++c) if (image[top, c].Intensity >= threshold.Intensity) found = true; if (found) break; } int bottom; found = false; for (bottom = image.Rows - 1; bottom >= 0; bottom--) { for (int c = 0; c < image.Cols && !found; ++c) if (image[bottom, c].Intensity >= threshold.Intensity) found = true; if (found) break; } if (right < left) { left = 0; right = 0; } if (bottom < top) { top = 0; bottom = 0; } return image.Copy(new System.Drawing.Rectangle(left, top, right-left + 1, bottom-top + 1)); }
private void FaceMonitoring(object sender, EventArgs e) { try { // Get the current frame form capture device CurrentFrame = Grabber.QueryFrame().Resize(320, 240, INTER.CV_INTER_CUBIC); //Video.Serve(CurrentFrame.Bytes); //System.Diagnostics.Debug.Print(Convert.ToBase64String(CurrentFrame.Bytes)); // Convert it to Grayscale Gray = CurrentFrame.Convert <Gray, byte>(); // Face Detector MCvAvgComp[][] facesDetected = Gray.DetectHaarCascade(Face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20)); if (facesDetected == null) { return; } // Action for each element detected foreach (MCvAvgComp face in facesDetected[0]) { Result = CurrentFrame.Copy(face.rect).Convert <Gray, byte>().Resize(100, 100, INTER.CV_INTER_CUBIC); // Draw the face detected in the 0th (gray) channel with blue color CurrentFrame.Draw(face.rect, new Bgr(Color.Red), 2); // Check if there is trained images to find a match if (TrainingImages.ToArray().Length != 0) { var eigenDistanceThreshold = 3000; var eps = 0.001; // TermCriteria for face recognition with numbers of trained images like maxIteration MCvTermCriteria termCrit = new MCvTermCriteria(TrainedFacesCounter, eps); // Eigen face recognizer FaceRecognitionEngine recognizer = new FaceRecognitionEngine(TrainingImages.ToArray(), LabelList.ToArray(), eigenDistanceThreshold, ref termCrit); var imgLabel = recognizer.Recognize(Result); LogStats($"{imgLabel} - Recognized"); // Draw the label for each face detected and recognized // CurrentFrame.Draw(imgLabel, ref Font, new Point(face.rect.X - 2, face.rect.Y - 2), new Bgr(Color.White)); MethodInvoker inv = delegate { lblLabelName.Text = imgLabel.Replace("_", " ").ToUpper(); }; Invoke(inv); } } // Show the face procesed and recognized imageBoxFrameGrabber.Image = CurrentFrame; } catch { } }
/// <summary> /// Detect edge of the hand shape in this image /// </summary> /// <param name="inputImage">The original input image</param> /// <returns></returns> public static Image <Gray, Byte> EdgeDetection(Image <Gray, Byte> inputImage) { Gray cannyThreshold = new Gray(127); Gray cannyThresholdLinking = new Gray(120); Image <Gray, Byte> cannyEdges = inputImage.Canny(cannyThreshold, cannyThresholdLinking); return(cannyEdges); }
/// <summary> /// Creates template from the input image by using provided parameters. /// </summary> /// <param name="sourceImage">Input image.</param> /// <param name="minFeatureStrength">Minimum gradient value for the feature.</param> /// <param name="maxNumberOfFeatures">Maximum number of features per template. The features will be extracted so that their locations are semi-uniformly spread.</param> /// <param name="classLabel">Template class label.</param> public virtual void Initialize(Bgr <byte>[,] sourceImage, int minFeatureStrength, int maxNumberOfFeatures, string classLabel) { Gray <int>[,] sqrMagImg; Gray <int>[,] orientationImg = GradientComputation.Compute(sourceImage, out sqrMagImg, minFeatureStrength); Func <Feature, int> featureImportanceFunc = (feature) => sqrMagImg[feature.Y, feature.X].Intensity; Initialize(orientationImg, maxNumberOfFeatures, classLabel, featureImportanceFunc); }
public Image <Gray, byte> blur(Image <Bgr, byte> image, int n1, int n2) { var grayImage = image.Convert <Gray, byte>(); var bluredImage = grayImage.SmoothGaussian(n1); var color = new Gray(255); var binarizedImage = bluredImage.ThresholdBinary(new Gray(n2), color); return(binarizedImage); }
//This algorithm creates the YCbCr and HSV copies of the pictures, then applies the rules to them. public Image <Gray, Byte> GetSkinRegion() { result = source.Clone(); Image <Gray, Byte> resultImage = result.ToImage <Gray, Byte>(); Mat srcYCbCr = new Mat(); Mat srcHSV = new Mat(); CvInvoke.CvtColor(source, srcYCbCr, ColorConversion.Bgr2YCrCb); //The hue value has to be scaled from 0 to 255 source.ConvertTo(srcHSV, DepthType.Cv32F); CvInvoke.CvtColor(srcHSV, srcHSV, ColorConversion.Bgr2Hsv); CvInvoke.Normalize(srcHSV, srcHSV, 0.0, 255.0, NormType.MinMax, DepthType.Cv32F); //Image copies from Mat so EmguCV can work with them Image <Bgr, Byte> srcImage = source.ToImage <Bgr, Byte>(); Image <Ycc, Byte> srcYCbCrImage = srcYCbCr.ToImage <Ycc, Byte>(); Image <Hsv, Single> srcHSVImage = srcHSV.ToImage <Hsv, Single>(); //Nested for loop goes through every pixel of the images and applies the rules to determine skin region for (int i = 0; i < source.Rows; i++) { for (int j = 0; j < source.Cols; j++) { double R = srcImage[i, j].Red; double G = srcImage[i, j].Green; double B = srcImage[i, j].Blue; bool rgb = RuleRGB(R, G, B); double Y = srcYCbCrImage[i, j].Y; double Cb = srcYCbCrImage[i, j].Cb; double Cr = srcYCbCrImage[i, j].Cr; bool yCbCr = RuleYCbCr(Y, Cb, Cr); double H = srcHSVImage[i, j].Hue; double S = srcHSVImage[i, j].Satuation; double V = srcHSVImage[i, j].Value; bool hsv = RuleHSV(H, S, V); //If not skin region -> black pixel, if skin region -> white pixel if (!(rgb && yCbCr && hsv)) { resultImage[i, j] = new Gray(0); } else { resultImage[i, j] = new Gray(255); } } } return(resultImage); }
/// <summary> /// Compute a bounding box of the largest object with a determined colour. /// </summary> /// <param name="image">a binary image</param> /// <param name="high">the colour to find in the image</param> /// <returns>The rectangle of the biggest object in the foreground</returns> public static Rectangle GetMaxBoundingBox(Image <Gray, Byte> image, Gray high) { var cols = image.Cols; var rows = image.Rows; var dilated = image.Dilate(1); Range maxRangeX = maxRangeIn(rows, cols, (x, y) => dilated[x, y], high); Range maxRangeY = maxRangeIn(cols, rows, (x, y) => dilated[y, x], high); return(new Rectangle(maxRangeX.Start(), maxRangeY.Start(), maxRangeX.Size(), maxRangeY.Size())); }
/// <summary> /// Creates linearized maps pyramid. /// </summary> /// <param name="sourceImage">Source image.</param> /// <param name="minGradientMagnitude">Minimal gradient value threshold.</param> /// <param name="neigborhoodPerLevel">Neighborhood per level. If null default neighborhood is going to be used.</param> /// <returns>Pyramid of linearized maps.</returns> public static LinearizedMapPyramid CreatePyramid(Gray <byte>[,] sourceImage, int minGradientMagnitude = 35, params int[] neigborhoodPerLevel) { return(CreatePyramid(sourceImage, source => { Gray <int>[,] sqrMagImg; return GradientComputation.Compute(sourceImage, out sqrMagImg, minGradientMagnitude); }, neigborhoodPerLevel)); }
/// <summary> /// Creates template from the input image by using provided parameters. /// </summary> /// <param name="sourceImage">Input image.</param> /// <param name="minFeatureStrength">Minimum gradient value for the feature.</param> /// <param name="maxNumberOfFeatures">Maximum number of features per template. The features will be extracted so that their locations are semi-uniformly spread.</param> /// <param name="classLabel">Template class label.</param> /// <param name="angle"></param> public virtual void Initialize(Bgr <byte>[,] sourceImage, int minFeatureStrength, int maxNumberOfFeatures, string classLabel, float angle) { Gray <int>[,] sqrMagImg; // so at this point oriImage is a image full of degrees? not gradient? Gray <int>[,] orientationImg = GradientComputation.Compute(sourceImage, out sqrMagImg, minFeatureStrength); Func <Feature, int> featureImportanceFunc = (feature) => sqrMagImg[feature.Y, feature.X].Intensity; Initialize(orientationImg, maxNumberOfFeatures, classLabel, angle, featureImportanceFunc); }
public Image <Gray, byte> edit_Noise_and_Brightness(Image <Bgr, byte> sourceImage, double k2) { var grayImage = sourceImage.Convert <Gray, byte>(); var bluredImage = grayImage.SmoothGaussian(5); var color = new Gray(255); var binarizedImage = bluredImage.ThresholdBinary(new Gray(k2), color); return(binarizedImage); }
/// <summary> /// Computes moments for the provided image. /// </summary> /// <param name="image">Image.</param> /// <param name="area">Area</param> public void Compute(Gray <float>[,] image, Rectangle area) { RawMoments raw = new RawMoments(Order); raw.Compute(image, area); CentralMoments center = new CentralMoments(raw); this.Compute(center); }
/// <summary> /// Creates linear response maps. /// </summary> /// <param name="orientationDegImg">Orientation image (in degrees).</param> /// <param name="neigborhood">Spread neighborhood size.</param> public LinearizedMaps(Gray <int>[,] orientationDegImg, int neigborhood) { this.NeigborhoodSize = neigborhood; this.ImageSize = orientationDegImg.Size(); this.LinearMapSize = new Size(orientationDegImg.Width() / neigborhood, orientationDegImg.Height() / neigborhood); this.ImageValidSize = new Size(this.LinearMapSize.Width * neigborhood, this.LinearMapSize.Height * neigborhood); this.LinearMaps = calculate(orientationDegImg); }
public static void DoGray() { GameObject gameObject = BattleCameraMgr.Instance.BattleCamera.gameObject; if (gameObject != null && PostEffectX.gr == null) { PostEffectX.gr = gameObject.AddComponent <Gray>(); } PostEffectX.gr.DoGrayOnEffect(1f); }
public Image <Bgr, byte> Lab4Func(int t = 0, double trhld = 80, double minzone = 50) { var grayImage = MainImageExp.Convert <Gray, byte>(); int kernelSize = 5; // радиусразмытия var bluredImage = grayImage.SmoothGaussian(kernelSize); var threshold = new Gray(trhld); // пороговоезначение var color = new Gray(255); // этим цветомбудут закрашены пиксели,имеющие значение >threshold var binarizedImage = bluredImage.ThresholdBinary(threshold, color); var contours = new VectorOfVectorOfPoint(); // контейнер для хранения контуров CvInvoke.FindContours( binarizedImage, // исходное чёрно-белое изображение contours, // найденные контуры null, // объект для хранения иерархии контуров (в данном случае не используется) RetrType.List, // структура возвращаемых данных (в данном случае список) ChainApproxMethod.ChainApproxSimple); var contoursImage = MainImageExp.CopyBlank(); var approxContour = new VectorOfPoint(); ShT = 0; ShR = 0; for (int i = 0; i < contours.Size; i++) { CvInvoke.ApproxPolyDP( contours[i], // исходныйконтур approxContour, // контурпослеаппроксимации CvInvoke.ArcLength(contours[i], true) * 0.05, // точностьаппроксимации, прямо//пропорциональнаяплощадиконтура true); // контур становится закрытым (первая и последняя точки соединяются)} if (CvInvoke.ContourArea(approxContour, false) > minzone) { if (approxContour.Size == 3 && (t == 3 || t == 0)) { ShT++; var points = approxContour.ToArray(); contoursImage.Draw(new Triangle2DF(points[0], points[1], points[2]), new Bgr(Color.GreenYellow), 2); } if (approxContour.Size == 4 && (t == 4 || t == 0)) { var points = approxContour.ToArray(); if (isRectangle(points) == true) { ShR++; contoursImage.Draw(CvInvoke.MinAreaRect(approxContour), new Bgr(Color.GreenYellow), 2); } } } } MainImageExp = contoursImage.Convert <Bgr, byte>(); return(MainImageExp); }
/// <summary> /// Binarize the image /// </summary> /// <param name="inputImage">The original input image</param> /// <param name="maxintensity">The maximum intensity of the binarized image.</param> /// <returns>The binarized image</returns> public static Image <Gray, Byte> Binarization(Image <Gray, Byte> inputImage, Gray maxintensity) { IntPtr grayimage = inputImage.Ptr; IntPtr processed_image; Image <Gray, Byte> imagetemp = inputImage.CopyBlank(); processed_image = imagetemp.Ptr; CvInvoke.cvThreshold(grayimage, processed_image, 160, maxintensity.Intensity, THRESH.CV_THRESH_BINARY | THRESH.CV_THRESH_OTSU); imagetemp.Ptr = processed_image; return(imagetemp); }
public void CalculateIntegerBinaryCodeForWindow_1Window() { // arrange - load frame Image <Gray, byte> frame = new Image <Gray, byte>(new Size(2, 2)); frame[0, 0] = new Gray(1); frame[0, 1] = new Gray(2); frame[1, 0] = new Gray(3); frame[1, 1] = new Gray(4); // arrange - create scanning window generator with 1 scanning window ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); IBoundingBox bb = new BoundingBox(new PointF(0.5f, 0.5f), new SizeF(2, 2)); bb.ScanningWindow = bb.CreateScanningWindow(); swg.ScanningWindows = new IBoundingBox[] { bb, }; // arrange - create pixel comparison scheduler with for 1 base classifier with 4 comparisons PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub(); PixelComparisonGroupF pcg = new PixelComparisonGroupF(); pcg.Value = new PixelComparisonF[] { new PixelComparisonF(new PointF(0, 0), new PointF(1, 0)), new PixelComparisonF(new PointF(0, 1), new PointF(1, 1)), new PixelComparisonF(new PointF(0, 1), new PointF(0, 0)), new PixelComparisonF(new PointF(1, 1), new PointF(1, 0)) }; pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[] { pcg }; // arrange - create base classifier BaseClassifier classifier = new BaseClassifier(swg, 0, pcs); classifier.PostInstantiation(); classifier.Initialize(); // define expected int expected = 3; // get actual classifier.SetCurrentFrame(frame); int actual = classifier.CalculateIntegerBinaryCodeForWindow(0); // assert Assert.AreEqual(expected, actual); }
/// <summary> /// Creates template from the input image by using provided parameters. /// </summary> /// <param name="sourceImage">Input image.</param> /// <param name="minFeatureStrength">Minimum gradient value for the feature.</param> /// <param name="maxNumberOfFeatures">Maximum number of features per template. The features will be extracted so that their locations are semi-uniformly spread.</param> /// <param name="classLabel">Template class label.</param> /// <param name="angle"></param> public override void Initialize(Gray<byte>[,] sourceImage, int minFeatureStrength, int maxNumberOfFeatures, string classLabel, float angle) { base.Initialize(sourceImage, minFeatureStrength, maxNumberOfFeatures, classLabel, angle); this.BinaryMask = sourceImage.Clone(BoundingRect); if (this.BinaryMask[0, 0].Intensity != 0) //background should be black BinaryMask = this.BinaryMask.Not(); this.BinaryMask = this.BinaryMask.ThresholdToZero((byte)(255 * 0.75), (byte)255); //if Gauss kernel was applied... }
/// <summary> /// Creates image templates pyramid from the black-white image by using the provided parameters. /// </summary> /// <param name="sourceImage">Input image.</param> /// <param name="classLabel">Templates class label.</param> /// <param name="minFeatureStrength">Minimum feature's gradient strength.</param> /// <param name="minNumberOfFeatures">Minimum number of features. If the number of features is less that minimum null will be returned.</param> /// <param name="maxNumberOfFeaturesPerLevel">Maximum number of features per template. The features will be scattered semi-uniformly.</param> /// <returns>Image template pyramid.</returns> public static ImageTemplatePyramid <T> CreatePyramidFromPreparedBWImage(Gray <byte>[,] sourceImage, string classLabel, int minFeatureStrength = 40, int minNumberOfFeatures = 30, int[] maxNumberOfFeaturesPerLevel = null) { return(CreatePyramid <Gray <byte> >(sourceImage, classLabel, (image, minFeatures, maxFeatures, label) => { var t = new T(); t.Initialize(sourceImage, minFeatureStrength, maxFeatures, label); return t; }, minNumberOfFeatures, maxNumberOfFeaturesPerLevel)); }
public static Image <Gray, byte> IsotropicSmoothing(Image <Gray, byte> im, int smooth) { double eps = .1; Image <Gray, byte> f = new Image <Gray, byte>(im.Size.Width, im.Size.Height); bool first = true; double tmp, min = 0, max = 0; for (int y = 0; y < f.Size.Height; y++) { for (int x = 0; x < f.Size.Width; x++) { tmp = im[y, x].Intensity; if (x > 0 && x < im.Size.Width - 1 && y > 0 && y < im.Size.Height - 1) { double A = tmp; double E = im[y + 1, x].Intensity; double S = im[y, x + 1].Intensity; double N = im[y, x - 1].Intensity; double W = im[y - 1, x].Intensity; double Lw = A - W; double Le = A - E; double Ln = A - N; double Ls = A - S; double pw = Math.Min(A, W) / (Math.Abs(A - W) + eps); double pe = Math.Min(A, E) / (Math.Abs(A - E) + eps); double pn = Math.Min(A, N) / (Math.Abs(A - N) + eps); double ps = Math.Min(A, S) / (Math.Abs(A - S) + eps); tmp = A + smooth * (Ln + Ls + Le + Lw); f[y, x] = new Gray(tmp); if (first) { min = max = tmp; first = false; } else { if (tmp < min) { min = tmp; } else { max = tmp; } } } } } return(f); }
// Hough transform private void PerformHoughTransform(Image <Gray, Byte> ImageToProcess, int HoughCircleThreshold, int MinAccumulator, int MaxAccumulator, double Resolution, double MinDistance, int MinRadius, int MaxRadius, HoughTransformFlag Mode) { //Accumulator value int currentAccumulator = MinAccumulator; //Threshold value Gray threshold = new Gray(HoughCircleThreshold); //start incrementing the accumilator till we find a proper circle while (currentAccumulator < MaxAccumulator) { Gray accumulator = new Gray(currentAccumulator); //apply hough circle CircleF[] detectedCircles = ImageToProcess.HoughCircles(threshold, accumulator, Resolution, MinDistance, MinRadius, MaxRadius)[0]; if (detectedCircles.Length == 1) { foreach (CircleF circle in detectedCircles) { switch (Mode) { //Hough Transform for pupil case HoughTransformFlag.Pupil: ImageToProcess.Draw(circle, new Gray(200), 2); ContourDetectedPupilImageColor.Draw(circle, new Bgr(0, 0, 255), 2); PupilCenter.X = (int)circle.Center.X; PupilCenter.Y = (int)circle.Center.Y; break; //For outer Boundary case HoughTransformFlag.IrisOuterBoundary: IrisOuterBoundaryImageColor = ContourDetectedPupilImageColor.Clone(); ImageToProcess.Draw(circle, new Gray(200), 2); //Radius of the outer boundary OuterBoundaryRadius = (int)circle.Radius; CvInvoke.cvCircle(IrisOuterBoundaryImageColor, PupilCenter, OuterBoundaryRadius, IrisConstants.WhiteColor, 2, Emgu.CV.CvEnum.LINE_TYPE.CV_AA, 0); break; } } break; } else if (detectedCircles.Length != 1) { currentAccumulator++; } } }
static public CircleF _getBlindSpotHough(Image <Gray, Byte> image) { Image <Gray, Byte> image_to_play = image.Copy(); image_to_play = image_to_play.Dilate(15); //image_to_play = image_to_play.Canny(new Gray(50), new Gray(20)); Gray cannyThreshold = new Gray(50); Gray circleAccumulatorThreshold = new Gray(80); CircleF[] circles1 = image_to_play.HoughCircles( cannyThreshold, circleAccumulatorThreshold, 2.0, //Resolution of the accumulator used to detect centers of the circles 10.0, //min distance 30, //min radius 140 //max radius )[0]; //Get the circles from the first channel int i = 0; foreach (CircleF circle in circles1) { float left = circle.Center.X - circle.Radius; float right = circle.Center.X + circle.Radius; float top = circle.Center.Y - circle.Radius; float down = circle.Center.Y + circle.Radius; if (left < 0) { continue; } if (top < 0) { continue; } if (right > image_to_play.Width) { continue; } if (down > image_to_play.Height) { continue; } //image_to_play.Draw(circle, new Gray(255), 3); //if (i++ > 5) break; return(circle); } //CvInvoke.cvShowImage("pes", image_to_play); return(new CircleF()); }
public static (uint, uint, uint, uint) RGBA(this Gray c) { uint r = default; uint g = default; uint b = default; uint a = default; var y = uint32(c.Y); y |= y << (int)(8L); return(y, y, y, 0xffffUL); }
/// <summary> /// Creates integral image. /// </summary> /// <param name="img">Image.</param> /// <returns>Integral image.</returns> public static Gray<double>[,] MakeIntegral(this Gray<double>[,] img) { var dstImg = new Gray<double>[img.Height() + 1, img.Width() + 1]; using (var uImg = img.Lock()) using (var uDstImg = dstImg.Lock()) { makeIntegral_Double(uImg, uDstImg); } return dstImg; }
public static void DeleteHorizonatalLines(Image <Gray, Byte> image) { CvInvoke.BitwiseNot(image, image); List <int> yList = HistLine(image); List <int> rList = new List <int>(); if (yList.Count != 0) { foreach (int t in yList) { int cols = image.Rows / 2; int count = 0; if (t + count >= image.Rows && t + count < 0) { while (image[t + count, cols].Intensity == 255) { if (NotList(rList, t + count) == false) { rList.Add((int)(t) + count); } ++count; if (t + count >= image.Rows && t + count < 0) { break; } } } count = 1; if (t - count >= image.Rows && t - count < 0) { while (image[t - count, cols].Intensity == 255) { if (NotList(rList, t - count) == false) { rList.Add((int)(t) - count); } ++count; if (t - count >= image.Rows && t - count < 0) { break; } } } } } foreach (int t in rList) { for (int k = 0; k < image.Cols; ++k) { image[t, k] = new Gray(0); } } }
/// <summary> /// Creates integral image. /// </summary> /// <param name="img">Image.</param> /// <returns>Integral image.</returns> public static Gray <double>[,] MakeIntegral(this Gray <double>[,] img) { var dstImg = new Gray <double> [img.Height() + 1, img.Width() + 1]; using (var uImg = img.Lock()) using (var uDstImg = dstImg.Lock()) { makeIntegral_Double(uImg, uDstImg); } return(dstImg); }
private void Menu_logic_xor_Click(object sender, EventArgs e) { Gray gray = new Gray(106.0); // Gray(106.0)은 이진수 01101010에 해당하기에 // 이를 이용하여 로직 연산을 수행하는 것이다. // 단순 106이라면 01101010이지만 double형이기 때문에 // 바이어스에 해당하는 0100000(64)을 더하면 결국에는 // 10101010으로 변환되기때문에 이를 통하여 로직 연산을 수행하는 것이다. imageObj = imageObj.Xor(gray); imageBox_canvas.Image = imageObj; }
/// <summary> /// Calculates features map. First orientations are quantized, the stable ones are selected and then they are spread. /// </summary> /// <param name="orientationDegImg">Orientation map. Each location represents angle in degrees [0..360].</param> /// <param name="spreadNeigborhood">Spreading neighborhood. If 1 no spreading is done.</param> /// <param name="minSameOrientations">Minimal number of the same orientations in [3 x 3] neighborhood to proclaim the orientation stable.</param> /// <returns>Feature map.</returns> public static Gray <byte>[,] Calculate(Gray <int>[,] orientationDegImg, int spreadNeigborhood, int minSameOrientations = 4) { Gray <byte>[,] quantizedOrient = FeatureMap.QuantizeOrientations(orientationDegImg); Gray <byte>[,] importantQuantizedOrient = FeatureMap.RetainImportantQuantizedOrientations(quantizedOrient, minSameOrientations); Gray <byte>[,] sprededQuantizedOrient = importantQuantizedOrient; if (spreadNeigborhood > 1) { sprededQuantizedOrient = FeatureMap.SpreadOrientations(importantQuantizedOrient, spreadNeigborhood); } return(sprededQuantizedOrient); }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog filename = new OpenFileDialog(); if (filename.ShowDialog() == DialogResult.OK) { img = new Image <Gray, byte>(filename.FileName); imageBox1.Image = img; threshold = new Gray(0); //二值化的閥值 thresholdimg = img.ThresholdBinary(threshold, new Gray(255)); //取得二值化影像 imageBox2.Image = thresholdimg; } }
public static Image <Gray, byte> GeneralBluredBinaryImage(Image <Bgr, byte> image) { Gray avg = image.Convert <Gray, byte>().GetAverage(); using (Image <Bgr, byte> bluredImage = image.CopyBlank()) { CvInvoke.MedianBlur(image, bluredImage, 31); using (Image <Gray, byte> mask = bluredImage.Copy().Convert <Gray, byte>()) { return(mask.ThresholdBinary(new Gray((int)avg.Intensity), new Gray(255))); } } }
public static void Floor(this Image<Gray, byte> image, int floor) { for (int x = 0; x < image.Width; x++) { for (int y = 0; y < image.Height; y++) { if (image[y, x].Intensity < floor) { image[y, x] = new Gray(); } } } }
private void findFoes() { bool ok = true; Target tmpTarget; //Convert the image to grayscale and filter out the noise Image<Gray, Byte> gray = img.Copy().Convert<Gray, Byte>().PyrDown().PyrUp(); Gray cannyThreshold = new Gray(180); Gray cannyThresholdLinking = new Gray(120); Gray circleAccumulatorThreshold = new Gray(120); CircleF[] circles = gray.HoughCircles( cannyThreshold, circleAccumulatorThreshold, resolution, //Resolution of the accumulator used to detect centers of the circles minDistance, //min distance minRadius, //min radius maxRadius //max radius )[0]; //Get the circles from the first channel foreach (CircleF circle in circles) { foreach (Target t in targets) { if (Math.Abs(circle.Center.X - t.Point.X) < centerThreshold) { ok = false; } } if (ok) { Bgr color = img[(int)circle.Center.Y, (int)circle.Center.X]; if (color.Red > color.Blue && color.Red > color.Green && color.Red > 150) { tmpTarget = new Target(); tmpTarget.Color = Color.Red; tmpTarget.IsFriend = false; tmpTarget.Point = circle.Center; tmpTarget.Distance = 0; tmpTarget.IsMoving = false; targets.Add(tmpTarget); } } else { ok = true; } } }
public static Image<Gray, Byte> ToGrayN(this Image<Gray, Byte> src, int level) { int height = src.Height, width = src.Width, grade = 256 / level; Image<Gray, Byte> dst = new Image<Gray, byte>(width, height); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int gray = 256 - grade - (int)src[y, x].Intensity / grade * grade; dst[y, x] = new Gray(gray); } } return dst; }
public static Image<Bgr, Byte> ColorContours(Image<Gray, Byte> src, Gray bg, Gray fg) { int height = src.Height, width = src.Width; Image<Bgr, Byte> dst = new Image<Bgr, byte>(width, height); var ll = FindConturs(src, bg, fg); foreach (var upper in ll[0]) { dst[upper.Y, upper.X] = new Bgr(0, 0, 255); } foreach(var lower in ll[1]) { dst[lower.Y, lower.X] = new Bgr(0, 255, 0); } return dst; }
public Image<Gray, byte> Process(Image<Gray, byte> src) { var dst = src.Clone(); Connectivity.Connect8 = this.Connect8; var cl = src.connectLevel(1, 8, 0); foreach (var dm in cl.Domains) { if (dm.Area < Threshold) { foreach (var p in dm.Points) { dst[p] = new Gray(0); } } } return dst; }
public Dictionary<Card, Point> LocateCards(Image<Bgr, Byte> table, Settings settings) { #region process image //Convert the image to grayscale and filter out the noise Image<Gray, Byte> gray = table.Convert<Gray, Byte>(); Gray cannyThreshold = new Gray(180); Gray cannyThresholdLinking = new Gray(120); Gray circleAccumulatorThreshold = new Gray(120); Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking); StructuringElementEx el = new StructuringElementEx(3, 3, 1, 1, CV_ELEMENT_SHAPE.CV_SHAPE_RECT); cannyEdges = cannyEdges.MorphologyEx(el, CV_MORPH_OP.CV_MOP_CLOSE, 1); #endregion Contour<Point> contours = cannyEdges.FindContours( CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, //was CV_CHAIN_APPROX_SIMPLE RETR_TYPE.CV_RETR_TREE); ContourNode tree = new ContourNode(contours); Dictionary<Card, Point> cardlocs = new Dictionary<Card, Point>(); foreach (KeyValuePair<Card, CardContour> pair in GiveCards(tree)) { ContourNode node = pair.Value.Node; Card card = pair.Key; PointF fcenter = node.Contour.GetMinAreaRect().center; Point center = new Point((int)fcenter.X, (int)fcenter.Y); cardlocs.Add(card, center); } #region draw #if DEBUG TreeViz.VizualizeTree(tree); ContourAnalyzer.DrawContours(tree, table); //ImageViewer.Show(table); #endif #endregion return cardlocs; }
private VideoParameters() { GrayFrameThresholdLow = new Gray(150); GrayFrameThresholdHigh = new Gray(500); CamshiftMaskLow = new Gray(100); CamshiftMaskHigh = new Gray(360); CannyThreshold = 100; CannyThresholdLinking = 100; RedThresholdRed = 300; RedThresholdGreen = 35; RedThresholdBlue = 35; BlueThresholdBlue = 300; BlueThresholdGreen = 35; BlueThresholdRed = 35; }
public List<FaceScored> FindFaces(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image, CascadeClassifier cascadeClassifierFace, CascadeClassifier cascadeClassifierEye) { List<FaceScored> currentFaces = new List<FaceScored>(); using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) { gray._EqualizeHist(); Size minFaceSize = new Size(minSizeFace , minSizeFace ); Size maxFaceSize = new Size(maxSizeFace , maxSizeFace ); Size minEyeSize = new Size(minSizeEye , minSizeEye ); Size maxEyeSize = new Size(maxSizeEye , maxSizeEye ); Rectangle[] facesDetected = cascadeClassifierFace.DetectMultiScale(gray, scaleFace , neighborsFace , minFaceSize,maxFaceSize); foreach (Rectangle f in facesDetected) { if (f.Width<35) break; gray.ROI = f; Rectangle[] eyesDetected = cascadeClassifierEye.DetectMultiScale(gray, scaleEye, neighborsEye, minEyeSize, maxEyeSize); if (eyesDetected.Count() >0){ FaceScored faceModel = new FaceScored(); faceModel.FaceImage = gray.Bitmap; faceModel.FaceImageFullColr = image.GetSubRect(f).Bitmap; faceModel.Height = faceModel.FaceImage.Height; faceModel.Width = faceModel.FaceImage.Width; faceModel.EyesCount = eyesDetected.Count(); Gray avgf = new Gray(); MCvScalar avstd = new MCvScalar(); gray.AvgSdv(out avgf, out avstd); faceModel.StdDev = avstd.V0; currentFaces.Add(faceModel); if(currentFaces.Count%5==0) Console.WriteLine("FaceDetect Add every 5 faceModel" + faceModel.Width); break; } gray.ROI = Rectangle.Empty; } } return currentFaces; }
public Blob Detect(Image<Bgr, byte> frame) { Image<Bgr, byte> bgr = frame; Image<Gray, byte> b = bgr.Split()[0].InRange(new Gray(Minimum.Blue), new Gray(Maximum.Blue)); Image<Gray, byte> g = bgr.Split()[1].InRange(new Gray(Minimum.Green), new Gray(Maximum.Green)); Image<Gray, byte> r = bgr.Split()[2].InRange(new Gray(Minimum.Red), new Gray(Maximum.Red)); Image<Gray, byte> finalResult = new Image<Gray, byte>(frame.Width, frame.Height); Gray black = new Gray(0); Gray white = new Gray(255); for (int x = 0; x < frame.Height; x++) { for (int y = 0; y < frame.Width; y++) { if (b[x, y].Intensity == 255 && g[x, y].Intensity == 255 && r[x, y].Intensity == 255) { finalResult[x, y] = white; } else { finalResult[x, y] = black; } } } Contour<Point> largestContour = null; // TODO: Find reference for this for (Contour<Point> c = finalResult.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL); c != null; c = c.HNext) { if (largestContour == null || c.Area > largestContour.Area) { largestContour = c; } } return largestContour == null ? null : new Blob(largestContour); }
private void ConvertImageButton_Click(object sender, RoutedEventArgs e) { if (bmp != null) { img = new Image<Bgr, byte>(bmp); // Split the image into the 3 channels (R, G, B) Image<Gray, Byte>[] splitImg = img.Split(); // We want the blue channel as this make the text closer to white singleChannelImage = splitImg[2]; // Now we are going to run a binary threshold to try and remove the background // The threshold is set high at the moment to remove as much guff as possible try { int thresholdString = int.Parse(ThresholdValue.Text); Gray grayThreshold = new Gray(thresholdString); processedImage = singleChannelImage.ThresholdToZero(grayThreshold).Not(); // Load the bitmap into a BitmapSource for the image controll to display it BitmapSource bmpSource = Utils.BitmapToBitmapSource(processedImage.ToBitmap()); // Update the image display to show the processed image DisplayImage.Source = bmpSource; } catch (Exception exception) { if (exception is FormatException || exception is ArgumentNullException) { MessageBox.Show("Error: Exception caught: " + exception.Message); } } } else { MessageBox.Show("Error: Image has not been loaded", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public DetectorResult Process(Image<Bgr, byte> rawFrame, Image<Gray, byte> grayFrame) { grayFrame.PyrDown().PyrUp(); var processedFrame = rawFrame.Copy(); Gray cannyThreshold = new Gray(180); Gray circleAccumulatorThreshold = new Gray(500); CircleF[] circles = grayFrame.HoughCircles( cannyThreshold, circleAccumulatorThreshold, 4.0, //Resolution of the accumulator used to detect centers of the circles 15.0, //min distance 5, //min radius 0 //max radius )[0]; //Get the circles from the first channel foreach (CircleF circle in circles) { processedFrame.Draw(circle, new Bgr(Color.DarkRed), 1); } return new DetectorResult() {RawImage = rawFrame, GrayImage = grayFrame, ProcessedImage = processedFrame}; }
public Image<Gray, Byte> Process(Image<Gray, Byte> src) { int width = src.Width, height = src.Height, windowsize_half = (WindowSize-1)/2; var dst = new Image<Gray, Byte>(width, height); for (int i = windowsize_half; i < height - windowsize_half; ++i) { for (int j = windowsize_half; j < width - windowsize_half; ++j) { if (src[i, j].Intensity == 255) { int sum = 0; for (int ii = -windowsize_half; ii <= windowsize_half; ++ii) { for (int jj = -windowsize_half; jj <= windowsize_half; ++jj) { if (src[i + ii, j + jj].Intensity == 255) ++sum; } } if (sum > (windowsize_half + windowsize_half + 1) * (windowsize_half + windowsize_half + 1) / 2) { dst[i, j] = new Gray(255); } else { dst[i, j] = new Gray(0); } } } } return dst; }
public void testShapeDetection() { while (true) { cannyThreshold = new Gray(threshold); cannyThresholdLinking = new Gray(thresholdLinking); Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking); LineSegment2D[] lines = cannyEdges.HoughLinesBinary( 1, //Distance resolution in pixel-related units Math.PI / 45.0, //Angle resolution measured in radians. 20, //threshold - default 20 10, //min Line width - default 10 10 //gap between lines - default 10 )[0]; //Get the lines from the first channel Image<Bgr, Byte> lineImage = img.CopyBlank(); foreach (LineSegment2D line in lines) lineImage.Draw(line, new Bgr(Color.BlanchedAlmond), 2); viewer.Image = lineImage.ConcateHorizontal(img).ConcateVertical(cannyEdges.ConcateHorizontal(gray).Convert<Bgr, Byte>()); } }
private void Binarisation() { for (int i = 1; i < img3.Height - 1; i++) { for (int j = 1; j < img3.Width - 1; j++) { if (img3[i, j].Intensity > 130) { Gray temp = new Gray(); temp.Intensity = 255; img3[i, j] = temp; } else { Gray temp = new Gray(); temp.Intensity = 0; img3[i, j] = temp; } } } //imageBox4.Image = img3; }
private Image<Gray, byte> Binarization(Image<Gray, byte> img4) { Image<Gray, byte> img5 = img4.Clone(); for (int i = 1; i < img4.Height - 1; i++) { for (int j = 1; j < img4.Width - 1; j++) { if (img4[i, j].Intensity > 128) { Gray temp = new Gray(); temp.Intensity = 255; img5[i, j] = temp; } else { Gray temp = new Gray(); temp.Intensity = 0; img5[i, j] = temp; } } } return img5; }
public Image<Gray, byte> Process(Image<Gray, byte> src) { Connectivity.Connect8 = this.Connect8; var cl = src.connectLevel(1, 8, 0).Domains.ToList(); var dst = src.Clone(); if (cl.Count > 1) { Vector2[] vs = cl.Select(i => new Vector2(i.Area, 0)).ToArray(); KMeansClustering.MaxIterate = this.MaxIteration; var result = KMeansClustering.AnnealCluster(vs, 2); int foreCluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1; for (int i = 0; i < cl.Count; ++i) { if (result.ClusterIndex[i] != foreCluster) { foreach (var p in cl[i].Points) { dst[p] = new Gray(0); } } } } return dst; }
public void ProcesseFramesToFindSquares(Image<Bgr, Byte> pImage) { pbBad.Visible = false; pbGood.Visible = false; scala_x = (float)(pImage.Width * 1.0 / pictureBox1.Width); scala_y = (float)(pImage.Height * 1.0 / pictureBox1.Height); #region square if (pImage != null) { Image<Bgr, Byte> pImg = pImage.Copy(); Image<Gray, Byte> gray = pImg.Convert<Gray, Byte>().PyrDown().PyrUp(); ibHoughCircles.Image = null; ibHoughCircles.Image = gray; Gray cannyThreshold = new Gray(180); Gray cannyThresholdLinking = new Gray(120); Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking); ibHoughCircles2.Image = null; ibHoughCircles2.Image = cannyEdges; LineSegment2D[] lines = cannyEdges.HoughLinesBinary(1, Math.PI / 45.0, 20, 30, 10)[0]; List<MCvBox2D> boxList = new List<MCvBox2D>(); listSquares = new List<MCvBox2D>(); int squares = 0; using (MemStorage storage = new MemStorage()) { for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext) { //Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); if (contours.Area > 1000 )// Int32.Parse(txtTotalApplicants.Text)) { if (currentContour.Total == 4 || currentContour.Total == 2 || currentContour.Total == 6) { bool isRectangle = true; Point[] pts = currentContour.ToArray(); LineSegment2D[] edges = PointCollection.PolyLine(pts, true); //using edges i found coordinates. for (int i = 0; i < edges.Length; i++) { double angle = Math.Abs(edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i])); if (angle < 85 || angle > 95) //if (angle < 80 || angle > 110) //if (angle != 90) { isRectangle = false; break; } if (isRectangle) { boxList.Add(currentContour.GetMinAreaRect()); // added MCvBox2D box = contours.GetMinAreaRect(); listSquares.Add(box); pImg.Draw(box, new Bgr(System.Drawing.Color.Red), 9); squares++; } } } } } } ibOriginal.Image = pImg; txtTotalApplicants.Text = listSquares.Count.ToString(); //squares.ToString(); bool a, b, c, d; a = b = c = d = false; for (int i = 0; i < listSquares.Count; i++) { //set the x,y position of the 4 squares guides of the scanned papaer float xp = listSquares[i].center.X / scala_x; float yp = listSquares[i].center.Y / scala_y; if (listSquares[i].center.X < pImg.Width / 2 && listSquares[i].center.Y < pImg.Height / 2) { mayas_corner[3].circles[0].set_X(xp); mayas_corner[3].circles[0].set_Y(yp); a = true; } if (listSquares[i].center.X > pImg.Width / 2 && listSquares[i].center.Y < pImg.Height / 2) { mayas_corner[3].circles[1].set_X(xp); mayas_corner[3].circles[1].set_Y(yp); b = true; } if (listSquares[i].center.X < pImg.Width / 2 && listSquares[i].center.Y > pImg.Height / 2) { mayas_corner[3].circles[2].set_X(xp); mayas_corner[3].circles[2].set_Y(yp); c = true; } if (listSquares[i].center.X > pImg.Width / 2 && listSquares[i].center.Y > pImg.Height / 2) { mayas_corner[3].circles[3].set_X(xp); mayas_corner[3].circles[3].set_Y(yp); d = true; } } //cleanList(); Image img_gb = null; if (!(a && b && c && d)) { //if was found just 3 points PointF missingPoint = findOneMissingPointFromSquare(mayas_corner[3], a, b, c, d); if (missingPoint.X != 0 && missingPoint.Y != 0) { if (!a) { mayas_corner[3].circles[0].set_X(missingPoint.X); mayas_corner[3].circles[0].set_Y(missingPoint.Y); } else if (!b) { mayas_corner[3].circles[1].set_X(missingPoint.X); mayas_corner[3].circles[1].set_Y(missingPoint.Y); } else if (!c) { mayas_corner[3].circles[2].set_X(missingPoint.X); mayas_corner[3].circles[2].set_Y(missingPoint.Y); } else { mayas_corner[3].circles[3].set_X(missingPoint.X); mayas_corner[3].circles[3].set_Y(missingPoint.Y); } pbGood.Visible = true; } else { for (int i = 0; i < mayas_corner[3].circles.Count; i++) { mayas_corner[3].circles[i].set_X(0); mayas_corner[3].circles[i].set_Y(0); } pbBad.Visible = true; } //for (int i = 0; i < mayas_corner[3].circles.Count; i++) //{ // mayas_corner[3].circles[i].set_X(0); // mayas_corner[3].circles[i].set_Y(0); //} //pbBad.Visible = true; } else { pbGood.Visible = true; } listSquares.Clear(); updateMayasCornerPosition(); PointF p1 = new PointF(lstAbsBigMaya[894].get_x1(), lstAbsBigMaya[894].get_y1()); PointF p2 = new PointF(lstAbsBigMaya[1223].get_x1(), lstAbsBigMaya[1223].get_y1()); PointF p3 = new PointF(lstAbsBigMaya[903].get_x1(), lstAbsBigMaya[903].get_y1()); PointF p4 = new PointF(lstAbsBigMaya[1232].get_x1(), lstAbsBigMaya[1232].get_y1()); set_drag_points_of_mayas_dni(p1, p2, p3, p4); p1 = new PointF(lstAbsBigMaya[663].get_x1(), lstAbsBigMaya[663].get_y1()); p2 = new PointF(lstAbsBigMaya[710].get_x1(), lstAbsBigMaya[710].get_y1()); p3 = new PointF(lstAbsBigMaya[667].get_x1(), lstAbsBigMaya[667].get_y1()); p4 = new PointF(lstAbsBigMaya[714].get_x1(), lstAbsBigMaya[714].get_y1()); set_drag_points_of_mayas_options(p1, p2, p3, p4); p1 = new PointF(lstAbsBigMaya[13].get_x1(), lstAbsBigMaya[13].get_y1()); p2 = new PointF(lstAbsBigMaya[1235].get_x1(), lstAbsBigMaya[1235].get_y1()); p3 = new PointF(lstAbsBigMaya[47].get_x1(), lstAbsBigMaya[47].get_y1()); p4 = new PointF(lstAbsBigMaya[1269].get_x1(), lstAbsBigMaya[1269].get_y1()); set_drag_points_of_mayas_answers(p1, p2, p3, p4); //p1 = new PointF(lstAbsBigMaya[12].get_x1(), lstAbsBigMaya[12].get_y1()); //p2 = new PointF(lstAbsBigMaya[1234].get_x1(), lstAbsBigMaya[1234].get_y1()); //p3 = new PointF(lstAbsBigMaya[47].get_x1(), lstAbsBigMaya[47].get_y1()); //p4 = new PointF(lstAbsBigMaya[1269].get_x1(), lstAbsBigMaya[1269].get_y1()); //set_drag_points_of_mayas_answers(p1, p2, p3, p4); updateMayasCornerPosition(); } #endregion // put inside an event }
private void bfsSetColor(Image<Gray, Byte> img, Point start, int padding, Gray backColor, Gray foreColor) { Queue<Point> queue = new Queue<Point>(); queue.Enqueue(start); int[] dy, dx; if (Connective == 8) { dy = dy8; dx = dx8; } else { dy = dy4; dx = dx4; } while (queue.Count > 0) { Point p = queue.Dequeue(); img[p] = foreColor; for (int i = 0; i < Connective; ++i) { int ny = p.Y + dy[i], nx = p.X + dx[i]; if (ny < padding || ny >= img.Height - padding) continue; if (nx < padding || nx >= img.Width - padding) continue; if (img[ny, nx].Equals(backColor)) { queue.Enqueue(new Point(nx, ny)); } } } }
public static DetectionData DetectSquares(Image<Gray, byte> src, string detectionWindow = "") { for (int i = 0; i < 1; i++) { src = src.PyrDown(); src = src.PyrUp(); } src = src.Erode(1); Gray cannyThreshold = new Gray(255); Gray cannyThresholdLinking = new Gray(1); Image<Gray, Byte> cannyEdges = src.Canny(cannyThreshold.Intensity,cannyThresholdLinking.Intensity,3); LineSegment2D[] lines = cannyEdges.HoughLinesBinary( 1, //Distance resolution in pixel-related units Math.PI / 45.0, //Angle resolution measured in radians. 20, //threshold 30, //min Line width 10 //gap between lines )[0]; //Get the lines from the first channel List<Rectangle> rectanglesList = new List<Rectangle>(); using (var storage = new MemStorage()) { for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext) { Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); if (currentContour.BoundingRectangle.Height * currentContour.BoundingRectangle.Width > 50) //only consider contours with area greater than 250 { if (currentContour.Total >= 4) //The contour has more than 4 vertices. { var boundingRectangle = currentContour.BoundingRectangle; if (!rectanglesList.Exists(rect => rect.IntersectsWith(boundingRectangle))) rectanglesList.Add(boundingRectangle); } } } } ShowInNamedWindow(cannyEdges, detectionWindow); return new DetectionData(rectanglesList, src); }
public List<Face> FindFaces(Image<Bgr, byte> image, string faceFileName, string eyeFileName, double scale, int neighbors, int minSize) { List<Face> faces = new List<Face>(); List<Rectangle> facesRect = new List<Rectangle>(); List<Rectangle> eyesRect = new List<Rectangle>(); try { //Console.WriteLine(" FaceDetectGPU FindFaces faceFileName=" + faceFileName + " cuda = " + CudaInvoke.HasCuda); using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName)) { using (CudaImage<Bgr, Byte> CudaImage = new CudaImage<Bgr, byte>(image)) using (CudaImage<Gray, Byte> CudaGray = CudaImage.Convert<Gray, Byte>()) using (GpuMat region = new GpuMat()) { face.DetectMultiScale(CudaGray, region); Rectangle[] faceRegion = face.Convert(region); facesRect.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage<Gray, Byte> faceImg = CudaGray.GetSubRect(f)) { using (CudaImage<Gray, Byte> clone = faceImg.Clone(null)) { Face facemodel = new Face(); eyesRect = new List<Rectangle>(FindEyes(eyeFileName, clone)); if (eyesRect != null) { facemodel.EyesRects = eyesRect; facemodel.EyesCount = eyesRect.Count; } else { continue; } facemodel.FaceImage = clone.Bitmap; facemodel.Height = facemodel.FaceImage.Height; facemodel.Width = facemodel.FaceImage.Width; facemodel.FaceRect = f; facemodel.FramePosX = f.X; facemodel.FramePosY = f.Y; facemodel.ImageFrameSize = image.Size; Gray avgf = new Gray(); MCvScalar avstd = new MCvScalar(); clone.ToImage().AvgSdv(out avgf, out avstd); facemodel.StdDev = avstd.V0; faces.Add(facemodel); if (facemodel.FaceScore > 39) Console.WriteLine("FaceDetect USING gpuCUDA Add faceModel" + facemodel.FaceScore); break; } } } } } } catch (Exception cudaerrJones) { Console.WriteLine("cudaerrJones = " + cudaerrJones); } return faces; }
public Image<Gray, byte> DrawDetection(string debugWindow = "") { Image<Gray,byte> img = new Image<Gray,byte>(width,height,new Gray(0)); Gray color = new Gray(255); foreach (var rectangleList in ColorBoundingRectangles){ foreach (var rectangle in rectangleList) { img.Draw(rectangle, color, 1); } } ImageTools.ShowInNamedWindow(img, debugWindow); return img; }
private void dfsSetColor(Image<Gray, Byte> img, int y, int x, int padding, Gray backColor, Gray foreColor) { img[y, x] = foreColor; int[] dy, dx; if (Connective == 8) { dy = dy8; dx = dx8; } else { dy = dy4; dx = dx4; } for (int i = 0; i < Connective; ++i) { int ny = y + dy[i], nx = x + dx[i]; if (ny < padding || ny >= img.Height - padding) continue; if (nx < padding || nx >= img.Width - padding) continue; if (img[ny, nx].Equals(backColor)) dfsSetColor(img, ny, nx, padding, backColor, foreColor); } }
/* //------------------------------------------------// public static Image<Gray, float> GetGradientMagnitude(Image<Gray, float> gX, Image<Gray, float> gY) { Image<Gray, float> gradient = new Image<Gray, float>(gX.Width, gX.Height); for (int i = 0; i < gradient.Height; i++) { for (int j = 0; j < gradient.Width; j++) { float gradVal = (float)Math.Sqrt(Math.Pow(gX[i, j].Intensity, 2.0) + Math.Pow(gY[i, j].Intensity, 2.0)); gradient[i, j] = new Gray(gradVal); } } return gradient; } //------ public static Image<Gray, float> GetGradientX(Image<Gray, byte> gray) { var grayFloat = gray.Convert<Gray, float>(); return grayFloat.Sobel(1, 0, 3);//To obtain gY: grayFloat.Sobel(0, 1, 3) } //------ //------------------------- public static Image<Gray, float> GetGradientDirection(Image<Gray, byte> gray) { var gradientDirection = new Image<Gray, float>(gray.Width, gray.Height); var gX = GetGradientX(gray); var gY = GetGradientY(gray); for (int i = 0; i < gradientDirection.Height; i++) { for (int j = 0; j < gradientDirection.Width; j++) { // gradientDirection[i, j] = new Gray(Math.Atan2(gY[i, j].Intensity, gX[i, j].Intensity)); } } return gradientDirection; } private static object GetGradientY(Image<Gray, byte> gray) { throw new NotImplementedException(); } //------------------------- //-----------------------------------------------// */ public void PerformShapeDetection() { //StringBuilder msgBuilder = new StringBuilder("Performance: "); Gray cannyThreshold = new Gray(180); Gray cannyThresholdLinking = new Gray(120); Gray circleAccumulatorThreshold = new Gray(120); Image<Bgr, Byte> photo5; photo5 = new Image<Bgr, byte>((Bitmap)pictureBox1.Image); Image<Gray, Byte> gray = photo5.Convert<Gray, byte>(); CircleF[] circles = gray.HoughCircles( cannyThreshold, circleAccumulatorThreshold, 5.0, //Resolution of the accumulator used to detect centers of the circles 10.0, //min distance 5, //min radius 0 //max radius )[0]; //Get the circles from the first channel Image<Gray, Byte> cannyEdges = gray.Canny(180, 120); LineSegment2D[] lines = cannyEdges.HoughLinesBinary( 1, //Distance resolution in pixel-related units Math.PI / 45.0, //Angle resolution measured in radians. 20, //threshold 30, //min Line width 10 //gap between lines )[0]; //Get the lines from the first channel #region Find triangles and rectangles List<Triangle2DF> triangleList = new List<Triangle2DF>(); List<MCvBox2D> boxList = new List<MCvBox2D>(); //double largestarea = 0; using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext) { Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); double largestarea = contours.Area; string p = largestarea.ToString(); // จะแสดงค่าขนาดพื้นที่ของรูปภาพแต่ทำไม่ได้ textBox1.Text = p; //MessageBox.Show(largestarea); if (contours.Area > 250) //only consider contours with area greater than 250 //MessageBox.Show(contours.Area); { if (currentContour.Total == 3) //The contour has 3 vertices, it is a triangle { Point[] pts = currentContour.ToArray(); triangleList.Add(new Triangle2DF( pts[0], pts[1], pts[2] )); // String text = _ocr.GetText(); หาขนาดแล้วไปแสดงใน text box // ocrTextBox.Text = text; } else if (currentContour.Total == 4) //The contour has 4 vertices. { #region determine if all the angles in the contour are within the range of [80, 100] degree bool isRectangle = true; Point[] pts = currentContour.ToArray(); LineSegment2D[] edges = PointCollection.PolyLine(pts, true); for (int i = 0; i < edges.Length; i++) { double angle = Math.Abs( edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i])); if (angle < 80 || angle > 100) { isRectangle = false; break; } } #endregion if (isRectangle) boxList.Add(currentContour.GetMinAreaRect()); } } } #endregion //pictureBox6.Image = photo5.ToBitmap(); pictureBox6.Image = cannyEdges.ToBitmap(); #region draw triangles and rectangles Image<Bgr, Byte> triangleRectangleImage = photo5.CopyBlank(); foreach (Triangle2DF triangle in triangleList) triangleRectangleImage.Draw(triangle, new Bgr(Color.DarkBlue), 2); foreach (MCvBox2D box in boxList) triangleRectangleImage.Draw(box, new Bgr(Color.DarkOrange), 2); pictureBox7.Image = triangleRectangleImage.ToBitmap(); #endregion #region draw circles Image<Bgr, Byte> circleImage = photo5.CopyBlank(); foreach (CircleF circle in circles) circleImage.Draw(circle, new Bgr(Color.Brown), 2); // circleImageBox.Image = circleImage; pictureBox8.Image = circleImage.ToBitmap(); #endregion /* #region draw lines Image<Bgr, Byte> lineImage = img.CopyBlank(); foreach (LineSegment2D line in lines) lineImage.Draw(line, new Bgr(Color.Green), 2); lineImageBox.Image = lineImage; #endregion */ }
public static void ApplyColormap(ref Emgu.CV.Image<Gray, double> source, out Emgu.CV.Image<Bgr, Byte> destination, ColorMapType colorMapType, bool invert = false) { destination = new Image<Bgr, Byte>(source.Width, source.Height); var max = 0.0; for (int y = 0; y < source.Height; y++) { for (int x = 0; x < source.Width; x++) { if (source[y, x].Intensity > max) { max = source[y, x].Intensity; } } } Image<Gray, byte> bufferImage = new Image<Gray, Byte>(source.Width, source.Height); for (int y = 0; y < source.Height; y++) { for (int x = 0; x < source.Width; x++) { if (invert) { bufferImage[y, x] = new Gray(255 - source[y, x].Intensity * 255 / max); } else { bufferImage[y, x] = new Gray(source[y, x].Intensity * 255 / max); } } } CvInvoke.ApplyColorMap(bufferImage, destination, colorMapType); }
public static void ApplyColormap(ref double[,] source, out Emgu.CV.Image<Bgr, Byte> destination, ColorMapType colorMapType) { double max = 0; for (int y = 0; y < source.GetLength(1); y++) { for (int x = 0; x < source.GetLength(0); x++) { if (source[y, x] > max) { max = source[y, x]; } } } Image<Gray, byte> buffer = new Image<Gray, Byte>(source.GetLength(0), source.GetLength(1)); for (int y = 0; y < source.GetLength(1); y++) { for (int x = 0; x < source.GetLength(0); x++) { buffer[y, x] = new Gray(source[y, x] * 255 / max); } } destination = new Image<Bgr, Byte>(source.GetLength(0), source.GetLength(1)); CvInvoke.ApplyColorMap(buffer, destination, colorMapType); }