Bitmap getLoctaionOfCircle(Bitmap bitmp, List <int> circleX, List <int> circle1Y, List <int> radius1, int count) { int leftMostItem = circleX[0]; int highestDot = circle1Y[0]; foreach (var item in circleX) { if (item < leftMostItem) { leftMostItem = item; } } foreach (var item in circle1Y) { if (item < highestDot) { highestDot = item; } } Debug.WriteLine("The highest point is " + highestDot); Debug.WriteLine("The LeftMost point is " + leftMostItem); int topPoint = (highestDot - radius1[0]) - 1; int leftMostPoint = (leftMostItem - (radius1[0])); int diameter = (radius1[0] * 3); Crop topLeftCorner = new Crop(new Rectangle(leftMostPoint, topPoint, diameter * 2, diameter * 4)); Bitmap m = topLeftCorner.Apply(bitmp); return(topLeftCorner.Apply(bitmp)); }
private double Fitness(MyRectangle rect, byte intensity) { Crop crop = new Crop(rect.Rectangle); var cropped = crop.Apply(GreyImage); ImageStatistics stat = new ImageStatistics(crop.Apply(GreyImage)); Histogram hist = stat.Gray; double sameColorRatio = (double)hist.Values[intensity] / hist.TotalCount; return(sameColorRatio == 1 ? -rect.Size : -sameColorRatio); }
/// <summary> /// This is the time consuming part. We try to find pen candidates in two pictures /// by their filtered difference in a specified area. /// </summary> /// <param name="previous">Previous video picture</param> /// <param name="current">Current video picture</param> /// <returns>Candidates</returns> private IEnumerable <PenCandidate> findPenCandidatesInArea(Bitmap previous, Bitmap current, Rectangle searchArea) { if (!searchArea.IsEmpty) { Crop cropFilter = new Crop(searchArea); previous = cropFilter.Apply(previous); current = cropFilter.Apply(current); } // calculate difference image this.Strategy.DifferenceFilter.OverlayImage = previous; Bitmap diffImage = current; //this.Strategy.DifferenceFilter.Apply(current); //diffImage.Save(@"c:\temp\images\diff-"+CurrentMillis.Millis+".png"); // translate red parts to gray image Bitmap grayImage = this.Strategy.GrayFilter.Apply(diffImage); //a.Save(@"c:\temp\images\r2_grey16.bmp"); // treshold the gray image Bitmap tresholdImage = this.Strategy.ThresholdFilter.Apply(grayImage); //tresholdImage.Save(@"c:\temp\images\blobs-" + CurrentMillis.Millis + ".png"); #if DEBUG if (DebugPicture != null) { DebugPicture(this, new DebugPictureEventArgs(new List <Bitmap>() { diffImage, grayImage, tresholdImage })); } #endif // count white blobs (TODO ev. kann man den thresholdFilter wegschmeissen, siehe ctr parameter von BlobCounter) this.Strategy.BlobCounter.ProcessImage(tresholdImage); // frame found blobs and add information Rectangle[] rawCandidates = this.Strategy.BlobCounter.GetObjectsRectangles(); List <PenCandidate> candidates = new List <PenCandidate>(); foreach (Rectangle r in rawCandidates) { r.Offset(searchArea.Location); // Adjust Blob Location to search area candidates.Add(new PenCandidate() { Rectangle = r, WeightedCenter = PointTools.CalculateCenterPoint(r) // TODO more precise weightening }); } return(candidates); }
public static void ExtractRegionsDevice2(Bitmap frame1, Bitmap frame2) { frame1 = greyscaleFilter2.Apply(frame1); frame2 = greyscaleFilter2.Apply(frame2); lock (_lock2) { cropFilter2.Rectangle = Region3; ROI3 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1))); diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2))); ROI3Diff = diffFilter2.Apply(ROI3); subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI3Diff)); ROI3DropDiff = subFilter2.Apply(ROI3Diff); cropFilter2.Rectangle = Region4; ROI4 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1))); diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2))); ROI4Diff = diffFilter2.Apply(ROI4); subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI4Diff)); ROI4DropDiff = subFilter2.Apply(ROI4Diff); cropFilter2.Rectangle = Region7; ROI7 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1))); diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2))); ROI7Diff = diffFilter2.Apply(ROI7); subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI7Diff)); ROI7DropDiff = subFilter2.Apply(ROI7Diff); cropFilter2.Rectangle = Region8; ROI8 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1))); diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2))); ROI8Diff = diffFilter2.Apply(ROI8); subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI8Diff)); ROI8DropDiff = subFilter2.Apply(ROI8Diff); } }
/// <summary> /// Function that crops top-right part of card image , with size 30x60 /// </summary> /// <returns>Cropped image</returns> public Bitmap GetTopLeftPart() { if (image == null) { return(null); } Crop crop = new Crop(new Rectangle(0, 0, 30, 84)); Bitmap im2 = crop.Apply(image); im2.Save("cropped", System.Drawing.Imaging.ImageFormat.Jpeg); return(crop.Apply(image)); }
public HandPositionTracker(String baseImageFile, String frameImagePath) { DirectoryInfo source = new DirectoryInfo(frameImagePath); if (Directory.Exists(source.FullName) == false) { Directory.CreateDirectory(source.FullName); } Bitmap baseBitmap = new Bitmap(baseImageFile); _baseImage = UnmanagedImage.FromManagedImage(baseBitmap); // Cut Bottom for later use Crop cropFilter = new Crop(BOTTOM_BASE_STRIPE); _bottomBaseImage = cropFilter.Apply(_baseImage); _bottomBaseImage.ToManagedImage().Save(@"c:\temp\recognition\bottombase.png"); // Load Images _images = new List <UnmanagedImage>(); foreach (FileInfo i in source.GetFiles("*.png")) { Bitmap b = new Bitmap(i.FullName); _images.Add(UnmanagedImage.FromManagedImage(b)); } }
/// <summary> /// 重置图片的指定大小并且居中 /// </summary> /// <param name="list"></param> /// <returns></returns> public static List <Bitmap> ToResizeAndCenterIt(List <Bitmap> list, int w = 20, int h = 20) { List <Bitmap> resizeList = new List <Bitmap>(); for (int i = 0; i < list.Count; i++) { //反转一下图片 list[i] = new Invert().Apply(list[i]); int sw = list[i].Width; int sh = list[i].Height; Crop corpFilter = new Crop(new Rectangle(0, 0, w, h)); list[i] = corpFilter.Apply(list[i]); //再反转回去 list[i] = new Invert().Apply(list[i]); //计算中心位置 int centerX = (w - sw) / 2; int centerY = (h - sh) / 2; list[i] = new CanvasMove(new IntPoint(centerX, centerY), Color.White).Apply(list[i]); resizeList.Add(list[i]); } return(resizeList); }
private void btnStop_Click(object sender, RoutedEventArgs e) { // StopCamera(); // create filter double x = Canvas.GetLeft(RectViewPoint); double y = Canvas.GetTop(RectViewPoint); double x1 = Canvas.GetLeft(videoPlayer); double y1 = Canvas.GetTop(videoPlayer); Crop filter = new Crop(new System.Drawing.Rectangle((int)x, (int)y, (int)RectViewPoint.Width, (int)RectViewPoint.Height)); // apply the filter BitmapImage bi = (BitmapImage)videoPlayer.Source; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bi.StreamSource); Bitmap newImage = filter.Apply(new Bitmap(bitmap)); if (imgcapture1.Source == null) { imgcapture1.Source = newImage.ToBitmapImage(); } else if (imgcapture2.Source == null) { imgcapture2.Source = newImage.ToBitmapImage(); btnCapture.IsEnabled = false; } else { } }
private void pictureBox1ImagePreview_MouseUp_1(object sender, MouseEventArgs e) { if (cropagree != true) { return; } mouseClicked = false; cropagree = false; if (endPoint.X != -1) { Point currentPoint = new System.Drawing.Point(e.X, e.Y); } endPoint.X = -1; endPoint.Y = -1; startPoint.X = -1; startPoint.Y = -1; pictureBox1ImagePreview.Refresh(); Bitmap sourceBitmap = new Bitmap(pictureBox1ImagePreview.Image, pictureBox1ImagePreview.Width, pictureBox1ImagePreview.Height); Crop filter = new Crop(rectCropArea); // apply the filter Bitmap newImage = filter.Apply(sourceBitmap); pictureBox1ImagePreview.Image = newImage; imgFile = pictureBox1ImagePreview.Image; }
private void calibrateButton_Click(object sender, RoutedEventArgs e) { Bitmap bmp = BitmapFromSource((BitmapSource)normalVideoImage.Source); BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); var filter = new Crop(new Rectangle((bmp.Width / 2) - (FilterWidth / 4), (bmp.Height / 2) - (FilterHeight / 4), FilterWidth / 2, FilterHeight / 2)); var croppedBitmap = filter.Apply(bitmapData); var stats = new ImageStatistics(croppedBitmap); redTextBox.Text = Convert.ToInt32(stats.Red.Median).ToString(); greenTextBox.Text = Convert.ToInt32(stats.Green.Median).ToString(); blueTextBox.Text = Convert.ToInt32(stats.Blue.Median).ToString(); var brushColor = new System.Windows.Media.Color(); brushColor.R = byte.Parse(redTextBox.Text); brushColor.G = byte.Parse(greenTextBox.Text); brushColor.B = byte.Parse(blueTextBox.Text); brushColor.A = byte.MaxValue; calibratedColorRect.Fill = new SolidColorBrush(brushColor); calibrated = true; testPassedLabel.Content = ""; Corners.Clear(); StartDetecting = false; }
private Bitmap cut(Bitmap source, int x, int width) { Rectangle rect = new Rectangle(x, 0, width, source.Height); Crop c = new Crop(rect); return(c.Apply(source)); }
public Bitmap GetSelectedPicture() { var crop = new Crop(Rect); var newBmp = crop.Apply(pictureBox1.Image as Bitmap); return(newBmp); }
// New frame received by the player private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) { //DateTime now = DateTime.Now; Graphics g = Graphics.FromImage(image); // paint current time //SolidBrush brush = new SolidBrush( Color.Red ); //g.DrawString( now.ToString( ), this.Font, brush, new PointF( 5, 5 ) ); if (radioButtonFixPicNo.Checked) { Crop filter = new Crop(new Rectangle(50, 300, 400, 400)); Bitmap newImage = filter.Apply(image); ResizeBilinear filter1 = new ResizeBilinear(200, 200); newImage = filter1.Apply(newImage); GrayscaleBT709 filter2 = new GrayscaleBT709(); Bitmap grayImage = filter2.Apply(newImage); ResizeBilinear filter3 = new ResizeBilinear(28, 28); Bitmap smallPic = filter3.Apply(grayImage); pictureBox1.Image = grayImage; pictureBox2.Image = smallPic; } //brush.Dispose( ); g.Dispose( ); }
public static Bitmap FileTo24bbpRgb(this string file, double ResizeRatio = 1.0d, double FrameCrop = 1.0d, double ImageZoom = 1.0d, int RotateDegree = 0) { try { StreamReader streamReader = new StreamReader(file); Bitmap tempOri = (Bitmap)Image.FromStream(streamReader.BaseStream, true); int CanvasWidth = (int)Math.Round(ResizeRatio * tempOri.Width); int CanvasHeight = (int)Math.Round(ResizeRatio * tempOri.Height); Bitmap Original = new Bitmap(CanvasWidth, CanvasHeight, PixelFormat.Format24bppRgb); using (Graphics graphics = Graphics.FromImage(Original)) { graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(tempOri, (int)Math.Round(ResizeRatio * (ImageZoom - 1.0d) * tempOri.Width / -2.0d), (int)Math.Round(ResizeRatio * (ImageZoom - 1.0d) * tempOri.Height / -2.0d), (int)Math.Round(ResizeRatio * ImageZoom * tempOri.Width), (int)Math.Round(ResizeRatio * ImageZoom * tempOri.Height)); } tempOri.Dispose(); streamReader.Dispose(); if (FrameCrop != 1.0d) { Crop cropfilter = new Crop(new Rectangle((int)Math.Round(FrameCrop * (1 - FrameCrop) * CanvasWidth), (int)Math.Round(FrameCrop * (1 - FrameCrop) * CanvasHeight), (int)Math.Round(FrameCrop * CanvasWidth), (int)Math.Round(FrameCrop * CanvasHeight))); Original = cropfilter.Apply(Original); } if (RotateDegree != 0) { RotateBicubic rotatefilter = new RotateBicubic(RotateDegree, keepSize: true); Original = rotatefilter.Apply(Original); } return(Original); } catch { throw new Exception("Failed to import the image files."); } }
/// <summary> /// 切割21位码的区域 /// </summary> /// <param name="source"></param> /// <param name="ticketType"></param> /// <returns></returns> public Bitmap ExtractTicketCodeImageArea(Bitmap source, int ticketType, int QRBottomY) { Bitmap temp = (Bitmap)source.Clone(); Bitmap retImg = null; if (Config.BLUE_TICKET == ticketType) { int leftX = Convert.ToInt16(temp.Width * Config.BLUE_CODE_X_CORP_RATIO); int topY = Convert.ToInt16(temp.Height * Config.BLUE_CODE_Y_CORP_RATIO); int w = Convert.ToInt16(temp.Width * Config.BLUE_CODE_W_CORP_RATIO); int h = Convert.ToInt16(temp.Height * Config.BLUE_CODE_H_CORP_RATIO); Rectangle rect = new Rectangle(leftX, QRBottomY, w, h); Crop corp = new Crop(rect); retImg = corp.Apply(temp); } else { int leftX = Convert.ToInt16(temp.Width * Config.RED_CODE_X_CORP_RATIO); int topY = Convert.ToInt16(temp.Height * Config.RED_CODE_Y_CORP_RATIO); int w = Convert.ToInt16(temp.Width * Config.RED_CODE_W_CORP_RATIO); int h = Convert.ToInt16(temp.Height * Config.RED_CODE_H_CORP_RATIO); int h1 = Convert.ToInt16(h * 1.5); Rectangle rect = new Rectangle(leftX, QRBottomY - h, w, h1); Crop corp = new Crop(rect); retImg = corp.Apply(temp); } return(retImg.Clone(new Rectangle(0, 0, retImg.Width, retImg.Height), PixelFormat.Format24bppRgb)); }
private void ZoomImg(ref Bitmap frame, double Factor) { if (rotateCam && (rotateAmount == 0 || rotateAmount == 2)) { int x = (int)(1280 * 1.7); int y = (int)(720 * 1.7); frame = ResizeImage(frame, new Size(x, y)); } if (Factor < 0.1) { return; } int centerX = frame.Width / 2; int centerY = frame.Height / 2; int OrgSizeX = frame.Width; int OrgSizeY = frame.Height; int fromX = centerX - (int)(centerX / Factor); int fromY = centerY - (int)(centerY / Factor); int SizeX = (int)(OrgSizeX / Factor); int SizeY = (int)(OrgSizeY / Factor); Crop CrFilter = new Crop(new Rectangle(fromX, fromY, SizeX, SizeY)); frame = CrFilter.Apply(frame); frame = ResizeImage(frame, new Size(OrgSizeX, OrgSizeY)); }
/// <summary> /// 图片分割 /// </summary> /// <param name="img"></param> /// <returns></returns> public static IList <Bitmap> Split(Bitmap img, int charCount) { IList <Bitmap> bmpList = new List <Bitmap>(); if (img == null) { return(bmpList); } List <int[]> xCutPointList = GetXCutPointList(img); List <int[]> yCutPointList = GetYCutPointList(xCutPointList, img); for (int i = 0; i < xCutPointList.Count(); i++) { int xStart = xCutPointList[i][0]; int xEnd = xCutPointList[i][1]; int yStart = yCutPointList[i][0]; int yEnd = yCutPointList[i][1]; if (i >= charCount) { break; } Rectangle rect = new Rectangle(xStart, yStart, xEnd - xStart + 1, yEnd - yStart + 1); Crop c = new Crop(rect); bmpList.Add(c.Apply(img)); } bmpList = ToResizeAndCenterIt(bmpList); return(bmpList); }
/// <summary> /// 等距分割21位码 /// </summary> /// <param name="img"></param> /// <param name="sourceWidth"></param> /// <param name="sourceHeight"></param> /// <returns></returns> public static IList <Bitmap> SplitTicketCodeByDefinedWidth(Bitmap img, int sourceWidth, int sourceHeight) { IList <Bitmap> bmpList = new List <Bitmap>(); int charW = Convert.ToInt16(sourceWidth * Config.BLUE_CODE_CHAR_WIDTH_RATIO); int h = img.Height; Rectangle rect = new Rectangle(0, 0, charW, h); Crop c; int numW = (img.Width - charW) / 20; for (int i = 0; i < 21; i++) { if (i < 14) { int x = numW * i; rect = new Rectangle(x, 0, numW, h); c = new Crop(rect); bmpList.Add(c.Apply(img)); } else if (i == 14) { int x = numW * i; rect = new Rectangle(x, 0, numW + 3, h); c = new Crop(rect); bmpList.Add(c.Apply(img)); } else { int x = numW * i + 3; rect = new Rectangle(x, 0, numW, h); c = new Crop(rect); bmpList.Add(c.Apply(img)); } } return(bmpList); }
/// <summary> /// 提取印刷票号图像 /// </summary> /// <param name="source"></param> /// <param name="ticketType"></param> /// <returns></returns> public static Bitmap ExtractTicketNoImage(Bitmap source, int ticketType) { Bitmap b = null; if (Config.BLUE_TICKET == ticketType) { int leftX = Convert.ToInt16(source.Width * Config.BLUE_TICKETNO_X_CORP_RATIO); int topY = Convert.ToInt16(source.Height * Config.BLUE_TICKETNO_Y_CORP_RATIO); int w = Convert.ToInt16(source.Width * Config.BLUE_TICKETNO_W_CORP_RATIO); int h = Convert.ToInt16(source.Height * Config.BLUE_TICKETNO_H_CORP_RATIO); Rectangle rect = new Rectangle(leftX, topY, w, h); Crop corp = new Crop(rect); b = corp.Apply(source); } else { int leftX = Convert.ToInt16(source.Width * Config.BLUE_TICKETNO_X_CORP_RATIO); int topY = Convert.ToInt16(source.Height * Config.BLUE_TICKETNO_Y_CORP_RATIO); int w = Convert.ToInt16(source.Width * Config.BLUE_TICKETNO_W_CORP_RATIO); int h = Convert.ToInt16(source.Height * Config.BLUE_TICKETNO_H_CORP_RATIO); Rectangle rect = new Rectangle(leftX, topY, w, h); Crop corp = new Crop(rect); b = corp.Apply(source); } return(b); }
/// <summary> /// IF options include split screen style then apply the applicable cropping else return the source /// </summary> /// <param name="source"></param> /// <param name="options"></param> /// <returns></returns> private Bitmap SplitSourceImage(Bitmap source, TemplateMatchOptions options) { if (options.SplitScreenPanel != Panel.All) { Crop f = new Crop(GetCrop(source, options)); //var ss = f.Apply(source); //ss.Save("croptest.png", ImageFormat.Png); //throw new Exception(); try { var output = f.Apply(source); if (options.SavePath != "") { output.Save(options.SavePath.UnCleanString(), ImageFormat.Png); } return(output); } catch { Compiler.ExceptionListener.Throw("[53]There was an unexpected issue getting Options for your source image cropping. Please " + "double check your additional options!"); return(source); } } else { return(source); } }
/// <summary> /// 截取二维码区域 /// </summary> /// <param name="source"></param> /// <param name="ticketType"></param> /// <returns></returns> public Tuple <Bitmap, Rectangle> ExtractQRCodeImageArea(Bitmap source, int ticketType) { Bitmap temp = (Bitmap)source.Clone(); Bitmap retImg = null; Rectangle rect = new Rectangle(); if (Config.BLUE_TICKET == ticketType) { int leftX = Convert.ToInt16(temp.Width * Config.BLUE_QRCODE_X_COPR_RATIO); int topY = Convert.ToInt16(temp.Height * Config.BLUE_QRCODE_Y_COPR_RATIO); int w = Convert.ToInt16(temp.Width * Config.BLUE_QRCODE_W_COPR_RATIO); int h = Convert.ToInt16(temp.Height * Config.BLUE_QRCODE_H_COPR_RATIO); rect = new Rectangle(leftX, topY, w, h); Crop corp = new Crop(rect); retImg = corp.Apply(temp); } else { int leftX = Convert.ToInt16(temp.Width * Config.RED_QRCODE_X_COPR_RATIO); int topY = Convert.ToInt16(temp.Height * Config.RED_QRCODE_Y_COPR_RATIO); int w = Convert.ToInt16(temp.Width * Config.RED_QRCODE_W_COPR_RATIO); int h = Convert.ToInt16(temp.Height * Config.RED_QRCODE_H_COPR_RATIO); rect = new Rectangle(leftX, topY, w, h); Crop corp = new Crop(rect); retImg = corp.Apply(temp); } return(Tuple.Create <Bitmap, Rectangle>(retImg, rect)); }
public List <Bitmap> cropImageIntoSegments(Bitmap imageToBeCropped) { Debug.WriteLine("I have been cropped"); int width = Convert.ToInt32(imageToBeCropped.Width * 0.5); int heightperSqaure = Convert.ToInt32(imageToBeCropped.Height * 0.3333); List <Bitmap> images = new List <Bitmap>(); Crop topLeftCorner = new Crop(new Rectangle(0, 0, width, heightperSqaure + 5)); Crop topRightCorner = new Crop(new Rectangle(width, 0, width, heightperSqaure + 5)); Crop leftMiddleCorner = new Crop(new Rectangle(0, heightperSqaure, width, heightperSqaure + 5)); Crop rightMiddleCorner = new Crop(new Rectangle(width, heightperSqaure, width, heightperSqaure + 5)); Crop bottomLeftCorner = new Crop(new Rectangle(0, heightperSqaure * 2, width, heightperSqaure + 5)); Crop bottomRightCorner = new Crop(new Rectangle(width, heightperSqaure * 2, width, heightperSqaure + 5)); images.Add(topLeftCorner.Apply(imageToBeCropped)); images.Add(topRightCorner.Apply(imageToBeCropped)); images.Add(leftMiddleCorner.Apply(imageToBeCropped)); images.Add(rightMiddleCorner.Apply(imageToBeCropped)); images.Add(bottomLeftCorner.Apply(imageToBeCropped)); images.Add(bottomRightCorner.Apply(imageToBeCropped)); return(images); }
// ========================================================= private void ZoomFunct(ref Bitmap frame, double Factor) { if (Factor < 0.1) { return; } int centerX = frame.Width / 2; int centerY = frame.Height / 2; int OrgSizeX = frame.Width; int OrgSizeY = frame.Height; int fromX = centerX - (int)(centerX / Factor); int fromY = centerY - (int)(centerY / Factor); int SizeX = (int)(OrgSizeX / Factor); int SizeY = (int)(OrgSizeY / Factor); try { Crop CrFilter = new Crop(new Rectangle(fromX, fromY, SizeX, SizeY)); frame = CrFilter.Apply(frame); ResizeBilinear RBfilter = new ResizeBilinear(OrgSizeX, OrgSizeY); frame = RBfilter.Apply(frame); } catch { } }
public string Recognize(ref Bitmap img) { var curImg = Crop(img); var stack = ""; Crop cr; while (true) { var symb = RecognizeSymbol(ref curImg); if (symb != null) { stack += symb; cr = new Crop(new Rectangle(symb.Width, 0, curImg.Width - symb.Width, curImg.Height)); if (symb.Width == curImg.Width) { return(stack); } curImg = CropLeft(cr.Apply(curImg)); } else { return(stack); } } }
Bitmap CropLeft(Bitmap img) { if (UseForeGround) { var leftCrop = 0; for (int i = 0; i < img.Width; i++) { var crop = true; for (int j = 0; j < img.Height; j++) { var pix = img.GetPixel(i, j); if ((pix.R == BgOrForeColor.R) && (pix.G == BgOrForeColor.G) && (pix.B == BgOrForeColor.B)) { crop = false; break; } } if (crop == false) { leftCrop = i; break; } } var cr = new Crop(new Rectangle(leftCrop, 0, img.Width - leftCrop, img.Height)); return(cr.Apply(img)); } else { var leftCrop = 0; for (int i = 0; i < img.Width; i++) { var crop = true; for (int j = 0; j < img.Height; j++) { var pix = img.GetPixel(i, j); if ((pix.R != BgOrForeColor.R) || (pix.G != BgOrForeColor.G) || (pix.B != BgOrForeColor.B)) { crop = false; break; } } if (crop == false) { leftCrop = i; break; } } var cr = new Crop(new Rectangle(leftCrop, 0, img.Width - leftCrop, img.Height)); return(cr.Apply(img)); } }
/// <summary> /// 按照 Y 轴线 切割 /// (丢弃等于号) /// </summary> /// <param name="?"></param> /// <returns></returns> public static List <Bitmap> Crop_Y(Bitmap b) { var list = new List <Bitmap>(); int[] cols = new int[b.Width]; for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { //获取当前像素点像素 var pixel = b.GetPixel(x, y); //说明是黑色点 if (pixel.R == 0) { cols[x] = ++cols[x]; } } } int left = 0, right = 0; for (int i = 0; i < cols.Length; i++) { //说明该列有像素值(为了防止像素干扰,去噪后出现空白的问题,所以多判断一下,防止切割成多个) if (cols[i] > 0 || (i + 1 < cols.Length && cols[i + 1] > 0)) { if (left == 0) { //切下来图片的横坐标left left = i; } else { //切下来图片的横坐标right right = i; } } else { //说明已经有切割图了,下面我们进行切割处理 if ((left > 0 || right > 0)) { Crop corp = new Crop(new Rectangle(left, 0, right - left + 1, b.Height)); var small = corp.Apply(b); //居中,将图片放在20*50的像素里面 list.Add(small); } left = right = 0; } } return(list); }
//crop the image private static Bitmap Crop(Bitmap image, Rectangle originalBounds) { int offset = (int)((image.Width - originalBounds.Width) / 2.0 + .5); Crop filter = new Crop(new Rectangle(offset, offset, originalBounds.Width - offset, originalBounds.Height - offset)); // apply the filter return(filter.Apply(image)); }
public IList <Bitmap> SplitTicketCodeByDefinedWidth(Bitmap img, int ticketType) { //Bitmap img = CutTicketCodeEdge(sourceImg, ticketType); int sourceWidth = 0; int charW = 0; int numW = 0; int offset = 0; if (Config.RED_TICKET == ticketType) { sourceWidth = Config.RED_TICKET_WIDTH; charW = Convert.ToInt16(sourceWidth * Config.RED_CODE_CHAR_WIDTH_RATIO); numW = Convert.ToInt16(sourceWidth * Config.RED_CODE_NUM_WIDTH_RATIO); offset = Convert.ToInt16(sourceWidth * Config.RED_CODE_CHAR_OFFSET_RATIO); } else { sourceWidth = Config.BLUE_TICKET_WIDTH; charW = Convert.ToInt16(sourceWidth * Config.BLUE_CODE_CHAR_WIDTH_RATIO); numW = Convert.ToInt16(sourceWidth * Config.BLUE_CODE_NUM_WIDTH_RATIO); offset = Convert.ToInt16(sourceWidth * Config.BLUE_CODE_CHAR_OFFSET_RATIO); } IList <Bitmap> bmpList = new List <Bitmap>(); int h = img.Height; Rectangle rect = new Rectangle(0, 0, charW, h); Crop c; //int numW = (img.Width - charW) / 20; for (int i = 0; i < 21; i++) { if (i < 14) { int x = numW * i; rect = new Rectangle(x, 0, numW, h); c = new Crop(rect); bmpList.Add(c.Apply(img)); } else if (i == 14) { int x = numW * i; rect = new Rectangle(x, 0, numW + offset, h); c = new Crop(rect); bmpList.Add(c.Apply(img)); } else { int x = numW * i + offset; rect = new Rectangle(x, 0, numW, h); c = new Crop(rect); bmpList.Add(c.Apply(img)); } } return(bmpList); }
/// <summary> /// 按照 X 轴线 切割 /// </summary> /// <param name="b"></param> /// <returns></returns> public static List <Bitmap> Crop_X(List <Bitmap> list) { var corplist = new List <Bitmap>(); //再对分割的图进行上下切割,取出上下的白边 foreach (var segb in list) { //统计每一行的“1”的个数,方便切除 int[] rows = new int[segb.Height]; /* * 横向切割 */ for (int y = 0; y < segb.Height; y++) { for (int x = 0; x < segb.Width; x++) { //获取当前像素点像素 var pixel = segb.GetPixel(x, y); //说明是黑色点 if (pixel.R == 0) { rows[y] = ++rows[y]; } } } int bottom = 0, top = 0; for (int y = 0; y < rows.Length; y++) { //说明该行有像素值(为了防止像素干扰,去噪后出现空白的问题,所以多判断一下,防止切割成多个) if (rows[y] > 0 || (y + 1 < rows.Length && rows[y + 1] > 0)) { if (top == 0) { //切下来图片的top坐标 top = y; } else { //切下来图片的bottom坐标 bottom = y; } } else { //说明已经有切割图了,下面我们进行切割处理 if ((top > 0 || bottom > 0) && bottom - top > 0) { Crop corp = new Crop(new Rectangle(0, top, segb.Width, bottom - top + 1)); var small = corp.Apply(segb); corplist.Add(small); } top = bottom = 0; } } } return(corplist); }
//method that crops the rectangle part of the image and returns it as a bitmap image public Bitmap CropImage() { //cropper that only crop the rectangle part Crop cropper = new Crop(new Rectangle(132, 82, 118, 78)); // apply the filter Bitmap croppedImg = cropper.Apply(preProcessImage); return(croppedImg); }