public void Resize(string outputPath, int newX_size, int height, string rootDirectoryName = "") { Image <Bgr, Byte> img1 = new Emgu.CV.Image <Bgr, Byte>(this.ImagePath); var oldX = img1.Width; var oldY = img1.Height; float ratio = (float)oldX / (float)newX_size; int newY = (int)Math.Round((double)((float)oldY / (float)ratio)); int newX = (int)Math.Round((double)((float)oldX / (float)ratio)); img1 = img1.Resize(newX, newY, Emgu.CV.CvEnum.Inter.LinearExact); var delta_w = newX_size - newX; var delta_h = height - newY; var top = delta_h / 2; var bottom = delta_h - top; var left = delta_w / 2; var right = delta_w - left; //img1.Save(@"C:\Users\lkathke\Desktop\EmguTest\resized.jpg"); Mat newImage = new Mat(); CvInvoke.CopyMakeBorder(img1, newImage, top, bottom, left, right, Emgu.CV.CvEnum.BorderType.Constant); newImage.Save(System.IO.Path.Combine(outputPath, System.IO.Path.GetFileName(this.ImagePath))); ResizeAnnotations(this.XMLPath, newX_size, height, newX, newY, oldX, oldY, top, left, outputPath, rootDirectoryName); }
public static void CropAndNormalizeObjects() { List <PSM4TxSample> samples = LoadSamples(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Original"); for (int i = 0; i < samples.Count; i++) { PSM4TxSample sample = samples[i]; Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(sample.imageFile); //{ //Aperture // //Extends in X direction to make width 240 pixels // double diff_x = APERTURE_ORIGINAL_WIDTH - sample.apertureW; // //Extends in Y direction to make 2000 pixels // double diff_y = APERTURE_ORIGINAL_HEIGHT - sample.apertureH; // Rectangle rect = new Rectangle((int)(sample.apertureX - diff_x / 2 + 0.5), (int)(sample.apertureY - diff_y / 2 + 0.5), APERTURE_ORIGINAL_WIDTH, APERTURE_ORIGINAL_HEIGHT); // image.ROI = rect; // Emgu.CV.Image<Gray, byte> normalized = image.Resize(APERTURE_HOG_WIDTH, APERTURE_HOG_HEIGHT, Inter.Linear); // string cropped_file = sample.imageFile.Replace("original", "Aperture"); // normalized.Save(cropped_file); //} //{ //arrayblock // //Extends in X direction to make width 1200 pixels // double diff_x = ARRAYBLOCK_ORIGINAL_WIDTH - sample.arrayblockW; // //Extends in Y direction to make 1600 pixels // double diff_y = ARRAYBLOCK_ORIGINAL_HEIGHT - sample.arrayblockH; // Rectangle rect = new Rectangle((int)(sample.arrayblockX - diff_x / 2 + 0.5), (int)(sample.arrayblockY - diff_y / 2 + 0.5), ARRAYBLOCK_ORIGINAL_WIDTH, ARRAYBLOCK_ORIGINAL_HEIGHT); // image.ROI = rect; // Emgu.CV.Image<Gray, byte> normalized = image.Resize(ARRAYBLOCK_HOG_WIDTH, ARRAYBLOCK_HOG_HEIGHT, Inter.Linear); // string cropped_file = sample.imageFile.Replace("original", "Arrayblock"); // normalized.Save(cropped_file); //} { //Isolator //Extends in X direction to make width 1000 pixels double diff_x = ISOLATOR_ORIGINAL_WIDTH - sample.isolatorW; //Extends in Y direction to make 1600 pixels double diff_y = ISOLATOR_ORIGINAL_HEIGHT - sample.isolatorH; Rectangle rect = new Rectangle((int)(sample.isolatorX - diff_x / 2 + 0.5), (int)(sample.isolatorY - diff_y / 2 + 0.5), ISOLATOR_ORIGINAL_WIDTH, ISOLATOR_ORIGINAL_HEIGHT); image.ROI = rect; Emgu.CV.Image <Gray, byte> normalized = image.Resize(ISOLATOR_HOG_WIDTH, ISOLATOR_HOG_HEIGHT, Inter.Linear); string cropped_file = sample.imageFile.Replace("original", "Isolator"); normalized.Save(cropped_file); } } }
public MarkerDetetection() { Emgu.CV.Image <Emgu.CV.Structure.Bgr, byte> m = new Image <Bgr, byte>("marker2.bmp"); Emgu.CV.Image <Gray, byte> gray = m.Convert <Gray, byte>(); gray = gray.Resize(30, 30, INTER.CV_INTER_LINEAR); gray._ThresholdBinary(new Gray(50), new Gray(255.0)); _marker = gray; _marker_size = _marker.Width; _dest = new PointF[] { new PointF(0, 0), new PointF(0, _marker_size), new PointF(_marker_size, _marker_size), new PointF(_marker_size, 0) }; _roi = new Image <Gray, byte>(_marker_size, _marker_size); _tmp = new Image <Gray, byte>(_marker_size, _marker_size); }
public static void NegativeIsolatorPatches() { List <PSM4TxSample> samples = LoadSamples(@"\users\jie\projects\Intel\data\PSM4-Tx\20160722\Original"); for (int i = 0; i < samples.Count; i++) { PSM4TxSample sample = samples[i]; Emgu.CV.Image <Gray, byte> image = new Emgu.CV.Image <Gray, byte>(sample.imageFile); int image_width = image.Width, image_height = image.Height; Random rand = new Random(); for (int k = 0; k < 2; k++) { int x, y, w = ISOLATOR_ORIGINAL_WIDTH, h = ISOLATOR_ORIGINAL_HEIGHT; x = rand.Next(0, image_width); y = rand.Next(0, image_height); if (x + w >= image_width || y + h >= image_height) { k--; continue; } Rectangle rect = new Rectangle(x, y, w, h); image.ROI = rect; Emgu.CV.Image <Gray, byte> neg_image = image.Resize(ISOLATOR_HOG_WIDTH, ISOLATOR_HOG_HEIGHT, Inter.Linear); string negative_image_file = sample.imageFile.Replace("original", @"IsolatorNegative"); negative_image_file = negative_image_file.Insert(negative_image_file.LastIndexOf('.'), "." + k.ToString()); string folder = Path.GetDirectoryName(negative_image_file); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } neg_image.Save(negative_image_file); } } }
/// <summary> /// Find the chessboard corners /// </summary> /// <param name="InputImage">Input image commpping from video camera.</param> /// <param name="OutputImage">Output image comming from image processor.</param> public static BoardConfiguration FindBoard(Bitmap InputImage, out Bitmap OutputImage) { #region Local variables // Figure Center Point figureCenter; // Figure BGR color Bgr bgrInFigureColor; // Vector image of figure CircleF circleFigure; // Color colorTransformedColor; // double hueChanel; double saturationChanel; double valueChanel; // Color circleColor; // Board configuration BoardConfiguration currentConfiguration = new BoardConfiguration(); #endregion #region Equalize the image // Convert a BGR image to HLS range Emgu.CV.Image <Hls, Byte> imageHsi = new Image <Hls, Byte>(InputImage); // Equalize the Intensity Channel imageHsi[1]._EqualizeHist(); imageHsi[2]._EqualizeHist(); // Convert the image back to BGR range Emgu.CV.Image <Bgr, Byte> DrawingImage = imageHsi.Convert <Bgr, Byte>(); // Geometric orientation image Emgu.CV.Image <Gray, Byte> BlobImage = imageHsi[1].Convert <Gray, Byte>(); // Create the font. MCvFont TextFont = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.5, 1.5); #endregion try { // Get bord marker points, MarkersList = Board.GetBoardMarkers(BlobImage.ToBitmap()); // If repers are 4 the proseed to describe the board. if (MarkersList.Length == MarkersCount) { // Set playboard size. Board.SetSize(BoardSize); #region Get perspective flat image. // Get perspective flat image. Image <Bgr, byte> newImage = SupportMethods.GetPerspectiveFlatImage(DrawingImage, BoardSize, MarkersList); #endregion for (int indexFigure = 0; indexFigure < 24; indexFigure++) { // Set the figure center figureCenter = Board.FigurePosition(indexFigure); // bgrInFigureColor = Board.GetFigureColor(newImage, Board.GetBoundingBox(figureCenter)); // colorTransformedColor = Color.FromArgb(255, (byte)bgrInFigureColor.Red, (byte)bgrInFigureColor.Green, (byte)bgrInFigureColor.Blue); // SupportMethods.ColorToHSV(colorTransformedColor, out hueChanel, out saturationChanel, out valueChanel); // Preset the color configuration valueChanel = 1.0; saturationChanel = 1.0; // Find the playr int figure = FigureColorDefinitions.WhoIsThePlayer((int)hueChanel); // Fill the board configuration currentConfiguration.Figure[indexFigure] = figure; #region Draw if (!((hueChanel > HueChanelMin) && (hueChanel < HueChanelMax)) && (figure != -1)) { circleColor = SupportMethods.ColorFromHSV(hueChanel, saturationChanel, valueChanel); // Create figure to draw. circleFigure = new CircleF(figureCenter, 10.0f); // Draw circle newImage.Draw(circleFigure, new Bgr(circleColor), 5); // Write text // #region Draw label to the processed image. newImage.Draw(String.Format("{0}", FigureColorDefinitions.WhoIsThePlayer((int)hueChanel)), ref TextFont, figureCenter, new Bgr(0, 0, 0)); //drawingImage.Draw(vectorWithSmallestY, RedColor, 2); #endregion } #endregion } // Show unperspective image //CvInvoke.cvShowImage("Perspective", newImage); DrawingImage = newImage; } } catch (Exception exception) { Console.WriteLine("Exception: {0}", exception.Message); } // Apply the output image. OutputImage = new Bitmap(DrawingImage.Resize(OutputImageWidth, OutputImageHeight, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR).ToBitmap()); return(currentConfiguration); }