internal ExamImage(Bitmap im) { lock (syob) { orgimg = im; this.img = AForge.Imaging.Image.Clone(im, System.Drawing.Imaging.PixelFormat.Format24bppRgb); } //img = im; Invert inv = new Invert(); inv.ApplyInPlace(img); //BlobsFiltering bf = new BlobsFiltering(new MixFiltering()); //bf.ApplyInPlace(img); BlobCounter bc = new BlobCounter(); bc.BlobsFilter = new SyncRectangleFilter(); bc.FilterBlobs = true; bc.CoupledSizeFiltering = true; bc.ProcessImage(img); syncboxs = bc.GetObjects(img, true); if (isFlip()) { img.RotateFlip(RotateFlipType.Rotate180FlipNone); bc.ProcessImage(img); syncboxs = bc.GetObjects(img, true); } bc.BlobsFilter = new AnswerFilter(); bc.ProcessImage(img); answers = bc.GetObjects(img, true); stuNumber = getNumber(); }
//Metodo che da un immagine in input ricerca tutti i rettangoli presenti nell'immagine sotto un determinato filtro public Bitmap TrovaRettangoli(Bitmap immagine) { // Crea un istanza di un BlobCounter BlobCounterBase bc = new BlobCounter(); // Un set di filtri bc.FilterBlobs = true; bc.MinWidth = 5; bc.MinHeight = 5; Bitmap bit = new Bitmap(immagine); prova = bit; // Processo l'immagine bc.ProcessImage(prova); blobs = bc.GetObjects(prova, false); // processo i blob foreach (Blob blob in blobs) { rettangoliIndicatori.Add(blob.Rectangle); } //La funzione using ha solo funzione visiva(Non è necessaria per il funzionamento del codice) //Mi mostra in rosso quali rettangoli sono stati trovati using (var gfx = Graphics.FromImage(prova)) { foreach (Rectangle rect in rettangoliIndicatori) { if (rect.Height < 500 / 5 && rect.Width < 500 / 2) { gfx.FillRectangle(Brushes.Red, rect); } } gfx.Flush(); } return(prova); }
bool[] DetectBlobs() { Bitmap im1 = AForge.Imaging.Image.FromFile(@"C:\Users\jorgo\OneDrive\Documents\EGB340\Blobs\Blobs\Blobs\testImage.png"); //get image dimension int width = im1.Width; int height = im1.Height; int blobCount = 0; Bitmap rbmp = new Bitmap(im1); bool[] occupancy = new bool[4]; //red green blue image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { //get pixel value Color p = im1.GetPixel(x, y); //extract ARGB value from p int a = p.A; int r = p.R; //int g = p.G; //int b = p.B; //set red image pixel rbmp.SetPixel(x, y, Color.FromArgb(a, r, 0, 0)); } } //threshold & find blobs BlobCounter counter = new BlobCounter(); counter.BackgroundThreshold = Color.FromArgb(210, 255, 255); counter.ProcessImage(rbmp); Blob[] blobs = counter.GetObjects(rbmp, true); //determine output values float[] outputX = new float[blobs.Length]; float[] outputY = new float[blobs.Length]; for (int i = 0; i < blobs.Length; i++) { if (blobs[i].Area > 100) { // If you want to output the image of each blob with reference to the rest of the image Bitmap output = blobs[i].Image.ToManagedImage(); output.Save(@"C:\Users\jorgo\OneDrive\Documents\EGB340\Blobs\Blobs\Blobs\OutputBlobs\" + i.ToString() + ".png"); blobCount += 1; outputX[i] = blobs[i].CenterOfGravity.X; outputY[i] = blobs[i].CenterOfGravity.Y; Console.WriteLine("X: " + outputX[i].ToString() + " Y: " + outputX[i].ToString()); } } occupancy = NumPeopleAtTable(blobCount, outputX, outputY); return(occupancy); }
public void FindBlobs() { BlobCounterBase bc = new BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 5; bc.MinHeight = 5; // process binary image bc.ProcessImage(Img); Blob[] blobs = bc.GetObjects(Img, false); x = blobs[0].CenterOfGravity.X; y = blobs[0].CenterOfGravity.Y; }
/// <summary> /// Merges blobs that are close to each other. /// </summary> /// <param name="value">The blobs found after running connected componenets algorithm.</param> /// <returns></returns> // private ArrayList<ExtendedBlob> mergeBlobs(ArrayList<ExtendedBlob> value) // { // /* // * Using a very simple methology of merging. // * Search all blobs that in close proximity of x pixels. // */ // ICollection<ExtendedBlob> intermediateValues = // new ArrayList<ExtendedBlob>(value.Count); // int x = 10; // ExtendedBlob closeToMe = value.RemoveAt(0); // while(!value.IsEmpty) // { // for (int i = 0; i < value.Count; i++) // { // Rectangle mergeRectangle = closeToMe.Rectangle; // mergeRectangle.Width += x; // mergeRectangle.Height += x; // if (mergeRectangle.IntersectsWith(value[i].Rectangle)) // { // // closeToMe // intermediateValues.Add(value[i]); // } // } // } // // } /// <summary> /// Runs the conected components algorithm. /// </summary> /// <param name="image">The image on which to run the algorithms.</param> /// <returns></returns> private List <ExtendedBlob> runConectedComponentsAlgorithm(Bitmap image) { blobCounter = new BlobCounter(image); Blob[] blobs = blobCounter.GetObjects(image); Rectangle[] rects = blobCounter.GetObjectRectangles(); List <ExtendedBlob> returnValue = new List <ExtendedBlob>(blobs.Length); for (int i = 0; i < blobs.Length; i++) { // Use adapter method and convert blobs to extended blobs. returnValue.Add(new ExtendedBlob(blobs[i], null, Unknown.GetInstance(), rects[i])); } return(returnValue); }
private void button5_Click(object sender, EventArgs e) { BlobFilter.MaxHeight = Convert.ToInt16(textBox2.Text.ToString()); BlobFilter.MinHeight = Convert.ToInt16(textBox3.Text.ToString()); BlobFilter.MaxWidth = Convert.ToInt16(textBox4.Text.ToString()); BlobFilter.MinWidth = Convert.ToInt16(textBox5.Text.ToString()); BlobBmp = BlobFilter.Apply(BinBmp); BlobCountFilter.ProcessImage(BlobBmp); BlobArray = BlobCountFilter.GetObjects(BlobBmp, true); //apply blob counting process.Image = BlobBmp; this.Text += ";" + BlobBmp.Width.ToString(); //this.Text = BlobArray[0].Area.ToString(); }
public static IEnumerable <Bitmap> GetBlobObjects(this Bitmap iBitmap, Size iMinSize) { BlobCounter blobCounter = new BlobCounter(iBitmap); blobCounter.FilterBlobs = true; blobCounter.MinWidth = iMinSize.Width; blobCounter.MinHeight = iMinSize.Height; blobCounter.ObjectsOrder = ObjectsOrder.Size; blobCounter.ProcessImage(iBitmap); Blob[] blobs = blobCounter.GetObjects(iBitmap, false); foreach (Blob blob in blobs) { yield return(blob.Image.ToManagedImage()); } }
//Metodo che verifica se l'immagine è una pagina del libro oppure no public Boolean VerificaLato(Bitmap immagine) { Blob[] blobs1; List <Rectangle> presenzaRettangoli = new List <Rectangle>(); List <Rectangle> verificaRettangoli = new List <Rectangle>(); // Crea un istanza di un BlobCounter BlobCounterBase bc1 = new BlobCounter(); // Un set di filtri bc1.FilterBlobs = true; bc1.MinWidth = 5; bc1.MinHeight = 5; Bitmap verifica = new Bitmap(immagine); // Processo l'immagine bc1.ProcessImage(verifica); blobs1 = bc1.GetObjects(verifica, false); // processo i blob foreach (Blob blob in blobs1) { verificaRettangoli.Add(blob.Rectangle); } //Verifico se nell'immagine c'è un rettangolo "indicatore" foreach (Rectangle rect in verificaRettangoli) { if (rect.Height < 500 / 5 && rect.Width < 500 / 2) { presenzaRettangoli.Add(rect); } } //Se è presente n rettangolo indicatore vuol dire che l'immagine è una pagina del libro e restituisce //vero altrimenti falso if (presenzaRettangoli.Count == 0) { thisSide = false; } else { thisSide = true; } return(thisSide); }
public List <PoreAnalyzeData> FindShapes(Bitmap bitmap) { Bitmap reversedbmp = ReverseBitmapColors(bitmap); BlobCounter blobCounter = new BlobCounter(reversedbmp); Blob[] blobs = blobCounter.GetObjects(reversedbmp, false); PoreAnalyzeData[] poreData = new PoreAnalyzeData[blobs.Length]; OnStart?.Invoke(this, blobs.Length); Parallel.For(0, blobs.Length, index => { var edgePoints = blobCounter.GetBlobsEdgePoints(blobs[index]); poreData[index] = new PoreAnalyzeData(blobs[index], ReverseBitmapColors(blobs[index].Image.ToManagedImage()), edgePoints); OnProgress?.Invoke(this, new EventArgs()); }); return(poreData.ToList()); }
public void FindBlobs() { BlobCounterBase bc = new BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 1; bc.MinHeight = 1; bc.MaxHeight = 1000; bc.MaxWidth = 1000; // process binary image bc.ProcessImage(Img); Blob[] blobs = bc.GetObjects(Img, false); if (blobs.Length > 0) { X = blobs[0].Rectangle.X; Y = blobs[0].Rectangle.Y; Width = blobs[0].Rectangle.Width; Height = blobs[0].Rectangle.Height; } // x =blobs[0].CenterOfGravity.X; //y = blobs[0].CenterOfGravity.Y; }
/// <summary> /// 提取21位码图像 /// </summary> /// <param name="source"></param> /// <param name="ticketType"></param> /// <returns></returns> public Bitmap GetTicketCodeImgs(Bitmap source, int ticketType) { Bitmap tempBin = source.Clone() as Bitmap; //Clone image to keep original image int minHeight, maxHeight, charWidth, charHeight, charOffset; if (Config.BLUE_TICKET == ticketType) { minHeight = Convert.ToInt16(Config.BLUE_CODE_CHAR_HEIGHT_RATIO * 0.7 * Config.BLUE_TICKET_HEIGHT); maxHeight = Convert.ToInt16(Config.BLUE_CODE_CHAR_HEIGHT_RATIO * 1.1 * Config.BLUE_TICKET_HEIGHT); charHeight = Convert.ToInt16(Config.BLUE_CODE_CHAR_HEIGHT_RATIO * Config.BLUE_TICKET_HEIGHT); charWidth = Convert.ToInt16(Config.BLUE_CODE_CHAR_WIDTH_RATIO * Config.BLUE_TICKET_WIDTH); charOffset = Convert.ToInt16(Config.BLUE_CODE_CHAR_OFFSET_RATIO * Config.RED_TICKET_WIDTH); } else { minHeight = Convert.ToInt16(Config.RED_CODE_CHAR_HEIGHT_RATIO * 0.7 * Config.RED_TICKET_HEIGHT); maxHeight = Convert.ToInt16(Config.RED_CODE_CHAR_HEIGHT_RATIO * 1.1 * Config.RED_TICKET_HEIGHT); charHeight = Convert.ToInt16(Config.RED_CODE_CHAR_HEIGHT_RATIO * Config.RED_TICKET_HEIGHT); charWidth = Convert.ToInt16(Config.RED_CODE_CHAR_WIDTH_RATIO * Config.RED_TICKET_WIDTH); charOffset = Convert.ToInt16(Config.RED_CODE_CHAR_OFFSET_RATIO * Config.RED_TICKET_WIDTH); } tempBin = commonSeq.Apply(source); // Apply filters on source image Bitmap temp = tempBin.Clone() as Bitmap; temp = extractCodeSeq.Apply(temp); temp = ImageProcess.CutBlankEdge(temp); Bitmap bmp = new Bitmap(charWidth * 21, charHeight + 4); BlobCounter blobCounter = new BlobCounter(temp); //把图片上的联通物体都分离开来 Blob[] blobs = blobCounter.GetObjects(temp, false); IList <Bitmap> codeImgs = new List <Bitmap>(); var blobFilters = from o in blobs where o.Image.Height >= minHeight orderby o.Rectangle.Y descending select o; if (blobFilters.Count <Blob>() < 1) { return(bmp); } //var rectYList = from o in codeImgDic.Values orderby o.Rectangle.Y select o; int maxY = blobFilters.First <Blob>().Rectangle.Y; Graphics g = Graphics.FromImage(bmp); g.FillRectangle(new SolidBrush(Color.Black), 0, 0, charWidth * 21, charHeight + 4); int offset = 0; var imgList = from img in blobFilters where Math.Abs(img.Rectangle.Y - maxY) <= charHeight orderby img.Rectangle.X select img; var list = imgList.ToList <Blob>(); for (int i = 0; i < list.Count; i++) { Blob blob = list[i]; if (i > 0) { Blob preBlob = list[i - 1]; if (Math.Abs(blob.Rectangle.X - preBlob.Rectangle.X - preBlob.Rectangle.Width) <= charWidth) { Crop c = new Crop(blob.Rectangle); Bitmap b = c.Apply(tempBin); PointF p = new PointF(offset, 2); g.DrawImage(b, p); offset += blob.Rectangle.Width + 1; } else { Bitmap b = blob.Image.ToManagedImage(); Bitmap pb = preBlob.Image.ToManagedImage(); Console.WriteLine(Math.Abs(blob.Rectangle.X - preBlob.Rectangle.X - preBlob.Rectangle.Width)); } } else { Crop c = new Crop(blob.Rectangle); Bitmap b = c.Apply(tempBin); PointF p = new PointF(offset, 2); g.DrawImage(b, p); offset += blob.Rectangle.Width + 1; } } g.Dispose(); return(commonSeq.Apply(bmp)); //return bmp; }
private Bitmap ExtractPaperFromFlattened(Bitmap bitmap, Bitmap basicImage, int minBlobWidHei, int fillint, int contint, string OMRSheets) { BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // lock image // step 2 - locating objects BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = minBlobWidHei; blobCounter.MinWidth = minBlobWidHei; blobCounter.ProcessImage(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); bitmap.UnlockBits(bitmapData); Graphics g = Graphics.FromImage(bitmap); Pen yellowPen = new Pen(Color.Yellow, 2); // circles Pen redPen = new Pen(Color.Red, 2); // quadrilateral Pen brownPen = new Pen(Color.Brown, 2); // quadrilateral with known sub-type Pen greenPen = new Pen(Color.Green, 2); // known triangle Pen bluePen = new Pen(Color.Blue, 2); // triangle Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs2 = blobCounter.GetObjects(bitmap, false); System.Drawing.Image compImg = System.Drawing.Image.FromFile("lc.jpg"); UnmanagedImage compUMImg = UnmanagedImage.FromManagedImage((Bitmap)compImg); List <IntPoint> quad = new List <IntPoint>(); try { //g.DrawRectangles(yellowPen, rects); foreach (Blob blob in blobs2) { if ( ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) > 0.0001 && ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) < 0.005 && blob.Rectangle.X < (bitmap.Width) / 4) { if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) { compUMImg = UnmanagedImage.FromManagedImage(ImageUtilities.ResizeImage(compImg, blob.Rectangle.Width, blob.Rectangle.Height)); if (isSame(blob.Image, compUMImg)) { g.DrawRectangle(yellowPen, blob.Rectangle); quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } } catch (ArgumentException) { MessageBox.Show("No Blobs"); } try { if (quad[0].Y > quad[1].Y) { IntPoint tp = quad[0]; quad[0] = quad[1]; quad[1] = tp; } } catch { } compImg = System.Drawing.Image.FromFile("rc.jpg"); compUMImg = UnmanagedImage.FromManagedImage((Bitmap)compImg); try { //g.DrawRectangles(yellowPen, rects); foreach (Blob blob in blobs2) { if ( ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) > 0.0001 && ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) < 0.004 && blob.Rectangle.X > (bitmap.Width * 3) / 4) { if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) { compUMImg = UnmanagedImage.FromManagedImage(ImageUtilities.ResizeImage(compImg, blob.Rectangle.Width, blob.Rectangle.Height)); if (isSame(blob.Image, compUMImg)) { g.DrawRectangle(yellowPen, blob.Rectangle); quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } } catch (ArgumentException) { MessageBox.Show("No Blobs"); } try { if (quad[2].Y < quad[3].Y) { IntPoint tp = quad[2]; quad[2] = quad[3]; quad[3] = tp; } } catch { } yellowPen.Dispose(); redPen.Dispose(); greenPen.Dispose(); bluePen.Dispose(); brownPen.Dispose(); g.Dispose(); //// put new image to clipboard //Clipboard.SetDataObject(bitmap); // and to picture box if (quad.Count == 4) { if (((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) < .75 || ((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) > 1.25) { quad.Clear(); } else if (quad[0].X > bitmap.Width / 2 || quad[1].X > bitmap.Width / 2 || quad[2].X < bitmap.Width / 2 || quad[3].X < bitmap.Width / 2) { quad.Clear(); } } if (quad.Count != 4) { if (contint <= 60) { if (contint >= 0) { contint += 5; contint *= -1; return(ExtractOMRSheet(basicImage, fillint, contint, OMRSheets)); } else { contint *= -1; contint += 10; return(ExtractOMRSheet(basicImage, fillint, contint, OMRSheets)); } } else { MessageBox.Show("Extraction Failed."); return(basicImage); } } else { IntPoint tp2 = quad[3]; quad[3] = quad[1]; quad[1] = tp2; QuadrilateralTransformation wrap = new QuadrilateralTransformation(quad); wrap.UseInterpolation = false; Rectangle sr = OMRSheetReader.GetSheetPropertyLocation(OMRSheets, OMREnums.OMRSheet.A550, OMREnums.OMRProperty.SheetSize); wrap.AutomaticSizeCalculaton = false; wrap.NewWidth = sr.Width; wrap.NewHeight = sr.Height; wrap.Apply(basicImage).Save("LastImg.jpg", ImageFormat.Jpeg); System.Drawing.Image imgl = (System.Drawing.Image)wrap.Apply(basicImage); Graphics gg = Graphics.FromImage(imgl); Pen pr = new Pen(Brushes.Red, 2); //gg.DrawRectangle(pr, MyXML.OMRSheetReader.GetSheetProperty(OMRSheets, MyXML.OMREnums.OMRSheet.A550, MyXML.OMREnums.OMRProperty.tensBlock1)); //gg.DrawRectangle(pr, MyXML.OMRSheetReader.GetSheetProperty(OMRSheets, MyXML.OMREnums.OMRSheet.A550, MyXML.OMREnums.OMRProperty.tensBlock2)); //gg.DrawRectangle(pr, MyXML.OMRSheetReader.GetSheetProperty(OMRSheets, MyXML.OMREnums.OMRSheet.A550, MyXML.OMREnums.OMRProperty.tensBlock3)); //gg.DrawRectangle(pr, MyXML.OMRSheetReader.GetSheetProperty(OMRSheets, MyXML.OMREnums.OMRSheet.A550, MyXML.OMREnums.OMRProperty.tensBlock4)); pr.Dispose(); gg.Dispose(); return((Bitmap)imgl); } }
static void Main(string[] args) { /* * //test array (replace with real data) * double[,] temps = new double[8, 8] { { 20, 20, 20, 20, 20, 20, 20, 20}, * { 20, 25, 26, 24, 20, 20, 20, 20}, * { 20, 27, 28, 26, 20, 20, 20, 20}, * { 20, 25, 27, 25, 20, 20, 20, 20}, * { 20, 20, 20, 20, 21, 23, 21, 20}, * { 20, 20, 20, 20, 21, 34, 22, 20}, * { 19, 20, 20, 20, 21, 24, 21, 20}, * { 18, 19, 20, 20, 20, 20, 20, 20} }; * * //find the average temp * double aveSum = 0; * for (int x = 0; x < temps.GetLength(0); x++) * { * for (int y = 0; y < temps.GetLength(1); y++) * { * aveSum += temps[x, y]; * } * } * double ave = aveSum / temps.Length; * * //turn binary array into image * Bitmap im1 = new Bitmap(temps.GetLength(0), temps.GetLength(1)); * * //threshhold by average temp (accept temps that are greater than the average + 2 degrees) * for (int x = 0; x < temps.GetLength(0); x++) * { * for (int y = 0; y < temps.GetLength(1); y++) * { * if (temps[x,y] > ave+2) * { * im1.SetPixel(x, y, Color.FromArgb(Int32.MaxValue)); * } * else * { * im1.SetPixel(x, y, Color.FromArgb(Int32.MinValue)); * } * } * }*/ //taking image from file instead Bitmap im1 = AForge.Imaging.Image.FromFile(@"C:\Users\jorgo\OneDrive\Documents\EGB340\Blobs\Blobs\Blobs\testImage.png"); //get image dimension int width = im1.Width; int height = im1.Height; //3 bitmap for red green blue image Bitmap rbmp = new Bitmap(im1); //Bitmap gbmp = new Bitmap(im1); //Bitmap bbmp = new Bitmap(im1); //red green blue image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { //get pixel value Color p = im1.GetPixel(x, y); //extract ARGB value from p int a = p.A; int r = p.R; //int g = p.G; //int b = p.B; //set red image pixel rbmp.SetPixel(x, y, Color.FromArgb(a, r, 0, 0)); //set green image pixel //gbmp.SetPixel(x, y, Color.FromArgb(a, 0, g, 0)); //set blue image pixel //bbmp.SetPixel(x, y, Color.FromArgb(a, 0, 0, b)); } } //threshold & find blobs BlobCounter counter = new BlobCounter(); counter.BackgroundThreshold = Color.FromArgb(210, 255, 255); counter.ProcessImage(rbmp); Blob[] blobs = counter.GetObjects(rbmp, true); //determine output values int blobcount = 0; float[] outputX = new float[blobs.Length]; float[] outputY = new float[blobs.Length]; for (int i = 0; i < blobs.Length; i++) { if (blobs[i].Area > 100) { // If you want to output the image of each blob with reference to the rest of the image Bitmap output = blobs[i].Image.ToManagedImage(); output.Save(@"C:\Users\jorgo\OneDrive\Documents\EGB340\Blobs\Blobs\Blobs\OutputBlobs\" + i.ToString() + ".png"); blobcount += 1; outputX[i] = blobs[i].CenterOfGravity.X; outputY[i] = blobs[i].CenterOfGravity.Y; Console.WriteLine("X: " + outputX[i].ToString() + " Y: " + outputY[i].ToString()); } } Console.WriteLine("Number of Blobs: " + blobcount.ToString()); Console.ReadKey(); }
private void ProcessFrame() { UnmanagedImage unpreprocessedCopy = m_currentUnpreprocessed.Clone(); m_currentPreprocessed = m_preprocessingFilters.Apply(unpreprocessedCopy); Bitmap image = new Bitmap(m_currentPreprocessed.Width, m_currentPreprocessed.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(image); if (m_showMode == ShowImageMode.Source) { graphics.DrawImageUnscaled(m_currentUnpreprocessed.ToManagedImage(), 0, 0); } else { graphics.DrawImageUnscaled(m_currentPreprocessed.ToManagedImage(), 0, 0); } m_blobExtractor.ProcessImage(m_currentPreprocessed); var blobs = m_blobExtractor.GetObjects(m_currentPreprocessed, false); lock (m_findedContours) { m_findedContours.Clear(); foreach (var blob in blobs) { Contour contour = m_extractor.Extract(blob.Image); m_findedContours.Add(new FindedContourInfo() { Contour = contour.BringLength(m_targetContoursLenght), CenterOfGravity = blob.CenterOfGravity, Rect = blob.Rectangle }); } foreach (var finded in m_findedContours) { ContoursMatcher.Match bestMatch = m_matcher.GetMatch(finded.Contour); if (bestMatch != null) { var text = string.Format("{0}", bestMatch.FamiliarContour.Description, bestMatch.MaxNSP.AbsoluteValue()); var textPosition = new PointF(finded.Rect.Left + finded.Rect.Width / 2, finded.Rect.Bottom); graphics.DrawString(text, m_font, m_textBrush, textPosition); if (m_showAngle) { graphics.DrawString(string.Format("{0:F0}°", bestMatch.MaxNSP.ArgumentDegrees()), m_font, m_textBrush, PointF.Add(textPosition, new Size(0, 13))); } if (m_showBoundingRect) { graphics.DrawRectangle(Pens.Green, finded.Rect); } } } } ImageBox.Image = image; BlobsCountLabel.Text = string.Format("blobs count: {0}", blobs.Length); }
/// <summary> /// Applies blob Processing on the image and extracts blobs corresponding to the bubbles and return them as a blob array. /// <param name="bitmap">Pre Processed Image</param> /// </summary> public static List <Blob> ExtractBubbleCorrespondingBlobs(Bitmap bitmap, int minblobwidth, int minblobheight) { Bitmap bm = new Bitmap(bitmap.Size.Width, bitmap.Size.Height); Graphics g = Graphics.FromImage(bm); g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); Pen redpen = new Pen(Color.Red, 3); AForge.Math.Geometry.SimpleShapeChecker detectshape = new AForge.Math.Geometry.SimpleShapeChecker(); // lock the image BitmapData bitmapdata = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // Finding Blobs in the Bitmap image BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = minblobheight; blobCounter.MinWidth = minblobwidth; blobCounter.ProcessImage(bitmapdata); Blob[] blobs = blobCounter.GetObjectsInformation(); bitmap.UnlockBits(bitmapdata); //Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blob_objects = blobCounter.GetObjects(bitmap, false); List <Blob> bubblesblobs = new List <Blob>(); try { foreach (Blob blob in blob_objects) { //detect circles through aspect ratio if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) // filters out blobs having insanely wrong aspect ratio { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob); detectshape.MinAcceptableDistortion = 0.5f; //0.5f; detectshape.RelativeDistortionLimit = 0.079f; //0.057f; AForge.Point center; float radius; //detect circles only if (detectshape.IsCircle(edgePoints, out center, out radius)) { //detect filled bubbles only if ((blob.Fullness > 0.60) && ((double)blob.Image.Width / (double)bitmap.Size.Width > 0.014)) { //g.DrawEllipse(redpen, // (int)(center.X - radius), // (int)(center.Y - radius), // (int)(radius * 2), // (int)(radius * 2)); bubblesblobs.Add(blob); } } } } return(bubblesblobs); } catch (ArgumentException) { MessageBox.Show("Bubble Detection Failed"); } return(null); }
public virtual void ProccessImage(BackgroundWorker worker) { this.worker = worker; UpdateProgress(3); //Brightness and sharpen filters BrightnessCorrection cfilter = new BrightnessCorrection(Settings.Brightness); GaussianSharpen filter = new GaussianSharpen(4, 11); //Apply filters cfilter.ApplyInPlace(sourceBitmap); UpdateProgress(15); filter.ApplyInPlace(sourceBitmap); UpdateProgress(30); //Convert to gray var tmpImage = ConvertToGrayScale(sourceBitmap); UpdateProgress(35); //Cut edges tmpImage = CutEdgesAndInvert(tmpImage); UpdateProgress(40); //Get angle for rotating image var rotateAngle = DetectRotation(tmpImage); UpdateProgress(45); if (rotateAngle != 0) { RotateBilinear rotate = new RotateBilinear(rotateAngle, true); tmpImage = rotate.Apply(tmpImage); } //Build horizontal hough lines OCRLessonReport.Imaging.HoughLineTransformation lineTransform = new OCRLessonReport.Imaging.HoughLineTransformation(); HoughLineRequestSettings settings = new HoughLineRequestSettings { HorizontalLines = true, VerticalLines = false, HorizontalDeviation = 2 }; lineTransform.ProcessImage(tmpImage, settings); //Get horizontal line HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(Settings.HorizontalSensitivity); //Get half width and height for future calculations int hWidth = tmpImage.Width / 2; int hHeight = tmpImage.Height / 2; //Get line coordinates (Y axis only - horizontal lines) var lineCoordinates = lines.Select(line => hHeight - line.Radius); //Grouping coords by delta var groupedCoordinates = ImagingHelper.GroupingCoordinates(lineCoordinates, Settings.LineGroupingDelta); if (groupedCoordinates.Count <= Settings.HeaderStartLine) { throw new Exception("Invalid source. Can't be recognized"); } int headerLineY0 = groupedCoordinates[Settings.HeaderStartLine]; int headerLineY1 = groupedCoordinates[Settings.HeaderStartLine + 1]; //Copy header to new image var headerImage = tmpImage.Copy(new Rectangle(0, headerLineY0, tmpImage.Width, headerLineY1 - headerLineY0)); //Parse header to get header lines HoughLineRequestSettings headerSettings = new HoughLineRequestSettings { HorizontalLines = false, VerticalLines = true, VerticalDeviation = 1 }; lineTransform.ProcessImage(headerImage, headerSettings); Func <HoughLine, int, int> getRadius = (l, w) => { if (l.Theta > 90 && l.Theta < 180) { return(w - l.Radius); } else { return(w + l.Radius); } }; HoughLine[] headerLines = lineTransform.GetLinesByRelativeIntensity(Settings.VerticalSensitivity); //Get header vertical lines var headerLineCoordinates = headerLines.Select(line => getRadius(line, hWidth)); //Grouped lines var groupedheaderLineCoordinates = ImagingHelper.GroupingCoordinates(headerLineCoordinates, Settings.LineGroupingDelta); //Build cell map List <TableCell> cellMap = new List <TableCell>(); UpdateProgress(50); //Use tess engine for ocr using (TesseractEngine engine = new TesseractEngine(Settings.TessdataPath, Settings.TessdataLanguage)) { //Parse top header var x0 = groupedheaderLineCoordinates.FirstOrDefault(); var x1 = groupedheaderLineCoordinates.LastOrDefault(); var y0 = groupedCoordinates[0]; var y1 = groupedCoordinates[1]; int fullProgress = (groupedheaderLineCoordinates.Count - 1) * (groupedCoordinates.Count - Settings.BottomStartLine - 1 - Settings.HeaderStartLine); int curProgress = 0; var hImage = tmpImage.Copy(new Rectangle(x0, y0, x1 - x0, y1 - y0)); hImage = ProcessCell(hImage); using (var page = engine.Process(hImage, PageSegMode.SingleBlock)) { cellMap.Add(new TableCell(0, 0, TableCellType.MainHeader, hImage, page.GetText(), false)); } //Parse table for (int i = 0; i < groupedheaderLineCoordinates.Count - 1; i++) { int subjectArea = (i < Settings.ColumnSubjectStart - 1) ? 0 : 1; for (int j = Settings.HeaderStartLine; j < groupedCoordinates.Count - Settings.BottomStartLine - 1; j++) { int headerArea = (j == Settings.HeaderStartLine) ? 2 : 0; TableCellType cellType = (TableCellType)(subjectArea + headerArea); var cellImg = tmpImage.Copy(new Rectangle(groupedheaderLineCoordinates[i], groupedCoordinates[j], groupedheaderLineCoordinates[i + 1] - groupedheaderLineCoordinates[i], groupedCoordinates[j + 1] - groupedCoordinates[j])); if (cellType == TableCellType.Text || cellType == TableCellType.Header || cellType == TableCellType.HeaderRotated) { cellImg = ProcessCell(cellImg, i == Settings.NameStartLine); string text = String.Empty; if (cellType == TableCellType.HeaderRotated) { cellImg.RotateFlip(RotateFlipType.Rotate90FlipNone); } using (var page = engine.Process(cellImg, PageSegMode.SingleBlock)) { text = page.GetText(); } cellMap.Add(new TableCell(i, j, cellType, cellImg, text, false)); } else { cellImg = ProcessCell(cellImg); BilateralSmoothing bfilter = new BilateralSmoothing(); bfilter.KernelSize = 7; bfilter.SpatialFactor = 10; bfilter.ColorFactor = 60; bfilter.ColorPower = 0.5; bfilter.ApplyInPlace(cellImg); cellImg = FilterColors(cellImg, Settings.FilteringColor, ByteColor.Black, ByteColor.White); BlobCounter bcounter = new BlobCounter(); bcounter.ProcessImage(cellImg); var blobs = bcounter.GetObjects(cellImg, false); if (blobs.Length < 1) { continue; } var biggestBlob = blobs.OrderBy(b => b.Area).LastOrDefault(); var biggestBlobsImage = biggestBlob.Image.ToManagedImage(); cellMap.Add(new TableCell(i, j, cellType, biggestBlobsImage, GetMask(biggestBlobsImage).ToString(), GetMask(biggestBlobsImage))); } curProgress++; double reportProgress = (double)curProgress / (double)fullProgress * 50 + 50; UpdateProgress((int)reportProgress); } } } this.Cells = cellMap; UpdateProgress(100); }
private void Button_HSV_Filter_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(pictureBox1.Image); HSLFiltering filterHsl = new HSLFiltering(); Mean filterMean = new Mean(); filterHsl.Luminance = new AForge.Range(0.1f, 1); filterHsl.UpdateHue = false; filterHsl.UpdateSaturation = false; filterHsl.UpdateLuminance = true; filterHsl.ApplyInPlace(image); filterMean.ApplyInPlace(image); SISThreshold filterThresold = new SISThreshold(); GrayscaleBT709 filterGray = new GrayscaleBT709(); image = filterGray.Apply(image); Bitmap clone = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format8bppIndexed); filterThresold.ApplyInPlace(clone); image = clone; Bitmap clone2normal = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format32bppRgb); image = clone2normal; BlobCounter bc = new BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 5; bc.MinHeight = 5; bc.ProcessImage(image); Blob[] blobs = bc.GetObjects(image, false); var rectanglesToClear = from blob in blobs select blob.Rectangle; using (var gfx = Graphics.FromImage(image)) { foreach (var rect in rectanglesToClear) { if (rect.Height < 20 && rect.Width < 20) { gfx.FillRectangle(Brushes.White, rect); } } gfx.Flush(); } Dilatation filterDilation = new Dilatation(); image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format48bppRgb); filterDilation.ApplyInPlace(image); filterDilation.ApplyInPlace(image); Erosion filterErosion = new Erosion(); filterErosion.ApplyInPlace(image); pictureBox2.Image = image; }
/// <summary> /// Detects, wrapps and crops out OMR sheet from flattened camera/scanner image. /// Flattened image is got by using method, private Bitmap flatten(Bitmap bmp, int fillint, int contint); /// </summary> /// <param name="bitmap">Bitmap image to process</param> /// <param name="basicImage">Backup image in case extraction fails</param> /// <param name="minBlobWidHei">Pre-configured variable, to be queried from XML reader</param> /// <param name="fillint">Pre-configured int, to be queried from XML reader</param> /// <param name="contint">Pre-configured int, to be queried from XML reader</param> /// <param name="OMRSheets">Sheets XML File Address</param> /// <param name="tb">Textbox to give in'process details on</param> /// <param name="panel1">Panel to draw in-process changes on.</param> /// <param name="giveFB">True when In- Process Feedback is required.</param> /// <param name="sheet">Type of sheet from OMREnums</param> /// <returns>Cropped OMR sheet (if detected) from camera/scanner image.</returns> private Bitmap ExtractPaperFromFlattened(Bitmap bitmap, Bitmap basicImage, int minBlobWidHei, int fillint, int contint, string OMRSheets, ref TextBox tb, ref Panel panel1, bool giveFB, XML.OMREnums.OMRSheet sheet) { // lock image, Bitmap itself takes much time to be processed BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // step 2 - locating objects BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = minBlobWidHei; // both these variables have to be given when calling the blobCounter.MinWidth = minBlobWidHei; // method, the can also be queried from the XML reader using OMREnums blobCounter.ProcessImage(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); bitmap.UnlockBits(bitmapData); Graphics g = Graphics.FromImage(bitmap); Pen yellowPen = new Pen(Color.Yellow, 2); // create pen in case image extraction failes and we need to preview the //blobs that were detected Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs2 = blobCounter.GetObjects(bitmap, false); //Detection of paper lies within the presence of crossmark printed on the corneres of printed sheet. // First, detect left edge. System.Drawing.Image compImg = System.Drawing.Image.FromFile("lc.jpg"); // lc.jpg = Mirrored image sample as located on the corner of printed sheet UnmanagedImage compUMImg = UnmanagedImage.FromManagedImage((Bitmap)compImg); // this helps filtering out much smaller and much larger blobs depending upon the size of image. // can be queried from XML Reader double minbr = XML.OMRSheetReader.getProcessVariableD(OMRSheets, sheet, XML.OMREnums.OMRImageProcessVariables.MinBlobRatio); double maxbr = XML.OMRSheetReader.getProcessVariableD(OMRSheets, sheet, XML.OMREnums.OMRImageProcessVariables.MaxBlobRatio); List <IntPoint> quad = new List <IntPoint>(); // Store sheet corner locations (if anyone is detected ) if (giveFB) { showTimeStamp("Left edge detection started.", ref tb); } try { foreach (Blob blob in blobs2) { if ( ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) > minbr && ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) < maxbr && blob.Rectangle.X < (bitmap.Width) / 4) // filters oout very small or very larg blobs { if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) // filters out blobs having insanely wrong aspect ratio { compUMImg = UnmanagedImage.FromManagedImage(ImageUtilities.ResizeImage(compImg, blob.Rectangle.Width, blob.Rectangle.Height)); if (isSame(blob.Image, compUMImg)) { g.DrawRectangle(yellowPen, blob.Rectangle); quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } } catch (ArgumentException) { MessageBox.Show("No Blobs"); } if (giveFB) { showTimeStamp("Left edge detection finished.", ref tb); } try { // Sort out the list in right sequence, UpperLeft,LowerLeft,LowerRight,upperRight if (quad[0].Y > quad[1].Y) { IntPoint tp = quad[0]; quad[0] = quad[1]; quad[1] = tp; } } catch { } compImg = System.Drawing.Image.FromFile("rc.jpg"); compUMImg = UnmanagedImage.FromManagedImage((Bitmap)compImg); if (giveFB) { showTimeStamp("Right edge detection started.", ref tb); // jusst like left edge detection } try { foreach (Blob blob in blobs2) { if ( ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) > minbr && ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) < maxbr && blob.Rectangle.X > (bitmap.Width * 3) / 4) { if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) { compUMImg = UnmanagedImage.FromManagedImage(ImageUtilities.ResizeImage(compImg, blob.Rectangle.Width, blob.Rectangle.Height)); if (isSame(blob.Image, compUMImg)) { g.DrawRectangle(yellowPen, blob.Rectangle); quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } } catch (ArgumentException) { MessageBox.Show("No Blobs"); } if (giveFB) { showTimeStamp("Right edge detection finished.", ref tb); } try { if (quad[2].Y < quad[3].Y) { IntPoint tp = quad[2]; quad[2] = quad[3]; quad[3] = tp; } } catch { } yellowPen.Dispose(); g.Dispose(); //Again, filter out if wrong blobs pretended to our blobs. if (quad.Count == 4) { if (((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) < .75 || ((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) > 1.25) { quad.Clear(); // clear if, both edges have insanely wrong lengths } else if (quad[0].X > bitmap.Width / 2 || quad[1].X > bitmap.Width / 2 || quad[2].X < bitmap.Width / 2 || quad[3].X < bitmap.Width / 2) { quad.Clear(); // clear if, sides appear to be "wrong sided" } } if (quad.Count != 4) // sheet not detected, reccurrsive call. { if (contint <= 60) //try altering the contrast correction on both sides of numberline { if (contint >= 0) { contint += 5; contint *= -1; return(ExtractOMRSheet(basicImage, fillint, contint, OMRSheets, ref panel1, ref tb, giveFB, sheet)); } else { contint *= -1; contint += 10; return(ExtractOMRSheet(basicImage, fillint, contint, OMRSheets, ref panel1, ref tb, giveFB, sheet)); } } else // contrast correction yeilded no result { MessageBox.Show("Extraction Failed."); return(basicImage); } } else // sheet found { IntPoint tp2 = quad[3]; quad[3] = quad[1]; quad[1] = tp2; //sort the edges for wrap operation QuadrilateralTransformation wrap = new QuadrilateralTransformation(quad); wrap.UseInterpolation = false; //perspective wrap only, no binary. Rectangle sr = XML.OMRSheetReader.GetSheetPropertyLocation(OMRSheets, sheet, XML.OMREnums.OMRProperty.SheetSize); wrap.AutomaticSizeCalculaton = false; wrap.NewWidth = sr.Width; wrap.NewHeight = sr.Height; wrap.Apply(basicImage).Save("LastImg.jpg", ImageFormat.Jpeg); // creat file backup for future use. return(wrap.Apply(basicImage)); // wrap } }
public static object ExtractPaperFromPrepapred(this Bitmap PreparedImage, System.Drawing.Image originalImage, bool onlyExtractionPoints, Pattern pattern) { BitmapData bitmapData = PreparedImage.LockBits( new Rectangle(0, 0, PreparedImage.Width, PreparedImage.Height), ImageLockMode.ReadWrite, PreparedImage.PixelFormat); BlobCounter blobCounter = new BlobCounter(); int minBlobWidHei = (int)(0.001 * PreparedImage.Width); blobCounter.FilterBlobs = true; blobCounter.MinHeight = minBlobWidHei; blobCounter.MinWidth = minBlobWidHei; blobCounter.BackgroundThreshold = Color.FromArgb(10, 10, 10); blobCounter.ProcessImage(bitmapData); PreparedImage.UnlockBits(bitmapData); Blob[] blobs = blobCounter.GetObjects((new Bitmap(PreparedImage)), false); double minbr = pattern.MinSizeRatio; //trebuie setate in baza de date!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! read more about it double maxbr = pattern.MaxSizeRatio; List <IntPoint> quad = new List <IntPoint>(); List <Blob> suspectBlobs = new List <Blob>(); for (int i = 0; i < blobs.Length; i++) { int area = blobs[i].Image.Width * blobs[i].Image.Height; if ( ((double)area) / ((double)PreparedImage.Width * PreparedImage.Height) > minbr && ((double)area) / ((double)PreparedImage.Width * PreparedImage.Height) < maxbr && ((double)blobs[i].Rectangle.Width / (double)blobs[i].Rectangle.Height <1.3 && (double)(double)blobs[i].Rectangle.Width / (double)blobs[i].Rectangle.Height> .7) ) { suspectBlobs.Add(blobs[i]); } } blobs = suspectBlobs.ToArray(); System.Drawing.Image compImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/omrtemp/lc.jpg")); UnmanagedImage compUMImg = UnmanagedImage.FromManagedImage((Bitmap)compImg); foreach (Blob blob in blobs) { if (blob.Rectangle.X < (PreparedImage.Width) / 2) { compUMImg = UnmanagedImage.FromManagedImage(PreprocessingHelper.ResizeImage(compImg, blob.Rectangle.Width, blob.Rectangle.Height)); if (blob.Image.IsSameAs(compUMImg)) { quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } if (quad.Count > 1) { if (quad[0].Y > quad[1].Y) { IntPoint tp = quad[0]; quad[0] = quad[1]; quad[1] = tp; } } compImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/omrtemp/rc.jpg")); compUMImg = UnmanagedImage.FromManagedImage((Bitmap)compImg); foreach (Blob blob in blobs) { if (blob.Rectangle.X > (PreparedImage.Width * 3) / 4) { compUMImg = UnmanagedImage.FromManagedImage(PreprocessingHelper.ResizeImage(compImg, blob.Rectangle.Width, blob.Rectangle.Height)); if (blob.Image.IsSameAs(compUMImg)) { quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } if (quad.Count > 3) { if (quad[2].Y < quad[3].Y) { IntPoint tp = quad[2]; quad[2] = quad[3]; quad[3] = tp; } } if (quad.Count == 4) { if (((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) < .75 || ((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) > 1.25) { quad.Clear(); } else if (quad[0].X > PreparedImage.Width / 2 || quad[1].X > PreparedImage.Width / 2 || quad[2].X < PreparedImage.Width / 2 || quad[3].X < PreparedImage.Width / 2) { quad.Clear(); } } IntPoint tp2 = quad[3]; quad[3] = quad[1]; quad[1] = tp2; return(quad); }
//================method============================= public void Split(String Plate_Type) { input_image = PLATE; process_imginput(input_image, Plate_Type); //define array numline = 2; his_ver = new int[input_height]; his_hor = new int[numline, input_width]; num_ver = new int[numline, 2]; num_hor = new int[numline, 35, 2];//cap phat mang max=35 phan tu define_line(process_image, 10); if (Plate_Type == "1hang") { numline = 1; } else { numline = 2; } numword_eachline = new int[numline]; for (int i = 0; i < numline; i++) { if (i == 0) { if (Plate_Type == "1hang") { numword_eachline[i] = 9; } else { numword_eachline[i] = 4; } } else { numword_eachline[i] = 5; } } define_pos(process_image, 7); //xac dinh chinh xac so ki tu moi hang Bitmap[] imgArr = new Bitmap[9]; IMAGEARR = new Bitmap[9]; int k = 0; for (int i = 0; i < numline; i++) { for (int j = 0; j < numword_eachline[i]; j++) { imgArr[k] = extractImg(process_image, num_ver[i, 0], num_ver[i, 1], num_hor[i, j, 0], num_hor[i, j, 1]); k++; } } //tien xu ly, resize cac ky tu FiltersSequence filts = new FiltersSequence(); filts.Add(new Dilatation()); filts.Add(new Erosion()); IFilter resize = new ResizeBilinear(10, 20); for (int i = 0; i < imgArr.Length; i++) { if (imgArr[i].Width < 3 || imgArr[i].Height < 5) { imgArr[i] = resize.Apply(imgArr[i]); } } for (int i = 0; i < imgArr.Length; i++) { //resize imgArr[i] = filts.Apply(imgArr[i]); BlobCounter blobs = new BlobCounter(imgArr[i]); Blob[] words = blobs.GetObjects(imgArr[i]); foreach (Blob word in words) { imgArr[i] = word.Image; } imgArr[i] = resize.Apply(imgArr[i]); IMAGEARR[i] = imgArr[i]; } }
private void ReadAndDisplayDicomFile(string fileName, string fileNameOnly) { dd.DicomFileName = fileName; bool result = dd.dicomFileReadSuccess; if (result == true) { imageWidth = dd.width; imageHeight = dd.height; bitDepth = dd.bitsAllocated; winCentre = dd.windowCentre; winWidth = dd.windowWidth; StatusLabel1.Text = fileName + ": " + imageWidth.ToString() + " X " + imageHeight.ToString(); StatusLabel1.Text += " " + bitDepth.ToString() + " bits per pixel"; userControl11.NewImage = true; Text = "DICOM Image Viewer: " + fileNameOnly; if (bitDepth == 16) { pixels16.Clear(); pixels8.Clear(); dd.GetPixels16(ref pixels16); byte[] buffer = new byte[pixels16.Count * 2]; byte[] temp; ByteConverter d = new ByteConverter(); int j = 0; for (int i = 0; i < pixels16.Count; i++) { temp = System.BitConverter.GetBytes(pixels16[i]); buffer[j++] = temp[0]; buffer[j++] = temp[1]; } if (winCentre == 0 && winWidth == 0) { winWidth = 4095; winCentre = 4095 / 2; } string index = ""; foreach (string s in dd.dicomInfo) { if (s.Contains("Image Number")) { index = s; } } userControl11.SetParameters(ref pixels16, imageWidth, imageHeight, winWidth, winCentre, true, this); if (processI && int.Parse(index.Split(':')[1]) > 9) { AForge.Imaging.Filters.Grayscale g1 = new Grayscale(0.2125, 0.7154, 0.0721); AForge.Imaging.Filters.BrightnessCorrection bC = new AForge.Imaging.Filters.BrightnessCorrection(brightness); bC.ApplyInPlace(userControl11.bmp); Bitmap image = g1.Apply(userControl11.bmp); thresholding = (int)((dd.windowWidth - dd.windowCentre) * 255 / dd.windowWidth) - trackBar2.Value; label1.Text = thresholding.ToString(); AForge.Imaging.Filters.Threshold thf = new AForge.Imaging.Filters.Threshold(thresholding); Bitmap ther = thf.Apply(image); BlobCounter blobCounter = new BlobCounter(ther); Blob[] blobs = blobCounter.GetObjects(ther, false); ImageStatistics img; AForge.Imaging.Filters.GrayscaleToRGB d1 = new GrayscaleToRGB(); Bitmap bm = d1.Apply(image); Edges s = new Edges(); Graphics gg = Graphics.FromImage(bm); string ss = null; Bitmap myImage = null; Blob b; int count = 0; listView1.Items.Clear(); mylesions.Clear(); Crop cut; Bitmap Ilesion = null; //System.Threading.Tasks.Parallel.ForEach(blobs, blob => foreach (Blob blob in blobs) { img = new ImageStatistics(blob.Image); double perc = ((double)img.PixelsCountWithoutBlack / (double)img.PixelsCount) * 100; textBox2.Text = perc.ToString(); if (blob.Image.Size.Height > 20 && blob.Image.Size.Width > 20 && perc > 35) { b = blob; cut = new Crop(b.Rectangle); Ilesion = g1.Apply(cut.Apply(userControl11.bmp)); ImageStatistics st = new ImageStatistics(b.Image); Bitmap pp = s.Apply(b.Image); ChannelFiltering c = new ChannelFiltering(new IntRange(0, 255), new IntRange(0, 0), new IntRange(0, 0)); Bitmap pp2 = d1.Apply(pp); c.ApplyInPlace(pp2); pp2.MakeTransparent(Color.Black); gg.DrawImage(pp2, b.Rectangle); gg.Flush(); myImage = userControl11.bmp.Clone(b.Rectangle, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ss = ((double)(st.PixelsCountWithoutBlack) * (double)dd.pixelHeight * dd.pixelWidth).ToString(); ListViewItem lv = new ListViewItem(count.ToString()); lv.SubItems.Add(ss); lv.SubItems.Add(b.Rectangle.Location.X.ToString() + "," + b.Rectangle.Location.Y.ToString()); listView1.Items.Add(lv); Add adder = new Add(pp); Bitmap undashes = (Bitmap)Ilesion.Clone(); adder.ApplyInPlace(Ilesion); string locc = (b.Rectangle.Location.X * dd.pixelWidth).ToString() + "mm," + (b.Rectangle.Location.Y * dd.pixelHeight).ToString() + "mm"; mylesions.Add(new lesion((Bitmap)Ilesion.Clone(), ss, b.Rectangle.Location, locc, undashes)); count++; } } textBox1.Text = "tumor size= " + ss + " mm² *" + dd.pixelDepth.ToString() + "mm"; // host.NewDocument(bmp); // pictureBox2.Image = myImage; pictureBox1.Image = bm; pictureBox2.Image = Ilesion; } else { pictureBox1.Image = userControl11.bmp; } pictureBox1.Invalidate(); } //userControl11.increasecontrast(200); } else { if (dd.dicmFound == false) { MessageBox.Show("This does not seem to be a DICOM 3.0 file. Sorry, I can't open this."); } else if (dd.dicomDir == true) { MessageBox.Show("This seems to be a DICOMDIR file, and does not contain an image."); } else { MessageBox.Show("Sorry, I can't read a DICOM file with this Transfer Syntax\n" + "You may view the initial tags instead."); } //userControl11.SetParameters(ref pixels8, imageWidth, imageHeight, // winWidth, winCentre, true, this); } }
public static Bitmap ExtractPaperFromFlattened(Bitmap bitmap, Bitmap originalimage, int minblobwidth, int minblobheight, bool applyrotation) { Bitmap bm = new Bitmap(bitmap.Size.Width, bitmap.Size.Height); Graphics g = Graphics.FromImage(bm); g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); Pen redpen = new Pen(Color.Red, 2); AForge.Math.Geometry.SimpleShapeChecker detectshape = new AForge.Math.Geometry.SimpleShapeChecker(); if (applyrotation) { //rotate the image in case it is not properly oriented // lock the image BitmapData bitmapdata_rot = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); BlobCounter blobCounter_rot = new BlobCounter(); blobCounter_rot.FilterBlobs = true; blobCounter_rot.MinHeight = minblobheight; blobCounter_rot.MinWidth = minblobwidth; blobCounter_rot.ProcessImage(bitmapdata_rot); bitmap.UnlockBits(bitmapdata_rot); Blob[] blob_objects_rot = blobCounter_rot.GetObjects(bitmap, false); try { foreach (Blob blob in blob_objects_rot) { List <IntPoint> edgePoints = blobCounter_rot.GetBlobsEdgePoints(blob); //detect rotated paper based on left edge rectangular mark List <IntPoint> cornerPoints; if (detectshape.IsQuadrilateral(edgePoints, out cornerPoints)) { if (detectshape.CheckPolygonSubType(cornerPoints) == AForge.Math.Geometry.PolygonSubType.Rectangle) { if ((blob.Fullness > 0.7) && (((double)blob.Image.Width / (double)bitmap.Size.Width > 0.025) && ((double)blob.Image.Width / (double)bitmap.Size.Width < 0.037)) && (((double)blob.Image.Height / (double)bitmap.Size.Height > 0.005) && ((double)blob.Image.Height / (double)bitmap.Size.Height < 0.013))) { // A------p------B Suppose these are the Coordinates of the Rectangle (image) // | 2 | 1 | A,B,C,D are vertices and 1,2,3,4 are quadrants // s------O------q p,q,r,s midpoints // | 3 | 4 | O is the center of the rectangle // D------r------C let the blob be denoted by BL System.Drawing.Point cent_O = new System.Drawing.Point(bitmap.Size.Width / 2, bitmap.Size.Height / 2); GraphicsUnit units = GraphicsUnit.Point; System.Drawing.Point pnt_A = new System.Drawing.Point((int)bitmap.GetBounds(ref units).X, (int)bitmap.GetBounds(ref units).Y); System.Drawing.Point pnt_B = new System.Drawing.Point(bitmap.Size.Width, (int)bitmap.GetBounds(ref units).Y); System.Drawing.Point pnt_C = new System.Drawing.Point(bitmap.Size.Width, bitmap.Size.Height); System.Drawing.Point pnt_D = new System.Drawing.Point((int)bitmap.GetBounds(ref units).X, bitmap.Size.Height); System.Drawing.Point midpnt_P = new System.Drawing.Point((pnt_A.X + pnt_B.X) / 2, (pnt_A.Y + pnt_B.Y) / 2); System.Drawing.Point midpnt_Q = new System.Drawing.Point((pnt_B.X + pnt_C.X) / 2, (pnt_B.Y + pnt_C.Y) / 2); System.Drawing.Point midpnt_R = new System.Drawing.Point((pnt_C.X + pnt_D.X) / 2, (pnt_C.Y + pnt_D.Y) / 2); System.Drawing.Point midpnt_S = new System.Drawing.Point((pnt_A.X + pnt_D.X) / 2, (pnt_A.Y + pnt_D.Y) / 2); System.Drawing.Point blob_CENGRAV = new System.Drawing.Point((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y); if (ImageProcessor.IsPointInsideRegion(bitmap, pnt_A, midpnt_P, cent_O, midpnt_S, blob_CENGRAV)) { //do nothing...image is properly oriented break; //terminate the loop } else if (ImageProcessor.IsPointInsideRegion(bitmap, midpnt_P, pnt_B, midpnt_Q, cent_O, blob_CENGRAV)) { //blob is present in quadrant 1 //rotate image by three CW bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); originalimage.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } else if (ImageProcessor.IsPointInsideRegion(bitmap, cent_O, midpnt_Q, pnt_C, midpnt_R, blob_CENGRAV)) { //blob is present in quadrant 4 //rotate image by two CW bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); originalimage.RotateFlip(RotateFlipType.Rotate180FlipNone); break; } else if (ImageProcessor.IsPointInsideRegion(bitmap, cent_O, midpnt_R, pnt_D, midpnt_S, blob_CENGRAV)) { //blob is present in quadrant 3 //rotate image by one CW bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); originalimage.RotateFlip(RotateFlipType.Rotate90FlipNone); break; } } } } } } catch (ArgumentException) { MessageBox.Show("Bilinear Rotational Transformation Failed"); } } //After applying rotational process, extract the blobs once again to find the circular edge markers // lock the image BitmapData bitmapdata = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // Finding Blobs in the Bitmap image BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = minblobheight; blobCounter.MinWidth = minblobwidth; blobCounter.ProcessImage(bitmapdata); Blob[] blobs = blobCounter.GetObjectsInformation(); bitmap.UnlockBits(bitmapdata); //Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blob_objects = blobCounter.GetObjects(bitmap, false); // Paper Detection happens through the detection of the edge-markers placed on all four // edges of the OMR sheet List <IntPoint> quad = new List <IntPoint>(); // Store sheet corner locations (if anyone is detected ) try { foreach (Blob blob in blob_objects) { //detect edge circles if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) // filters out blobs having insanely wrong aspect ratio { List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob); //detect circles only if (detectshape.IsCircle(edgePoints)) { //detect filled circles only //the ratio of width(blob)/width(bitmap) should be in range of 0.031 - 0.042 if ((blob.Fullness > 0.7) && (((double)blob.Image.Width / (double)bitmap.Width) > 0.031) && (((double)blob.Image.Width / (double)bitmap.Width) < 0.042)) { //g.DrawRectangle(redpen, blob.Rectangle); quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } // filter out if wrong blobs pretend to be our blobs. if (quad.Count == 4) { if (!((quad[0].DistanceTo(quad[1]) / quad[0].DistanceTo(quad[2]) > 0.5) && (quad[0].DistanceTo(quad[1]) / quad[0].DistanceTo(quad[2]) < 1.5))) { quad.Clear(); } else { //rearrange the edge coordinates, if not in proper manner while ((quad[0].X > quad[1].X) || (quad[1].Y > quad[3].Y) || (quad[2].X > quad[3].X) || (quad[0].Y > quad[2].Y)) { if (quad[0].X > quad[1].X) { IntPoint tmp = quad[0]; quad[0] = quad[1]; quad[1] = tmp; } if (quad[1].Y > quad[3].Y) { IntPoint tmp = quad[1]; quad[1] = quad[3]; quad[3] = tmp; } if (quad[2].X > quad[3].X) { IntPoint tmp = quad[3]; quad[3] = quad[2]; quad[2] = tmp; } if (quad[0].Y > quad[2].Y) { IntPoint tmp = quad[0]; quad[0] = quad[2]; quad[2] = tmp; } } } } if (quad.Count != 4) { //call recursively as the sheet is not detected properly //todo: try altering the blob height and width, threshold etc, when program does not find edge markers } else { //rearrange the edges coordinates // 0----1 // | | // 2----3 //interchange 2 and 3 IntPoint tp2 = quad[3]; quad[3] = quad[2]; quad[2] = tp2; //sort the edges for wrap operation QuadrilateralTransformation wrap = new QuadrilateralTransformation(quad, 2100, 2970); wrap.UseInterpolation = false; wrap.AutomaticSizeCalculaton = true; Bitmap wrappedoriginal = wrap.Apply(originalimage); return(wrappedoriginal); } } catch (ArgumentException) { MessageBox.Show("No Blobs Found"); } return(null); }
private Bitmap BlobExtract(Bitmap image) { var blobCounter = new BlobCounter() { ObjectsOrder = ObjectsOrder.XY }; blobCounter.ProcessImage(image); Bitmap bitmap = MainForm.DrawBitmapsAlignBottom(( from blob in (IEnumerable <Blob>)blobCounter.GetObjects(image, false) select blob.Image.ToManagedImage()).ToList <Bitmap>()); return(bitmap); }
private void buttonAnalyzeImage_Click(object sender, EventArgs e) { buttonSaveImage.Enabled = true; var img = ExtractAntibodiesFromInitialImage.ProcessImage(filename); imageWidth = img.Width; imageHeight = img.Height; ImageStatistics imgStats = new ImageStatistics(img); double result = (float)(imgStats.Gray.Values[255]) / (float)(imgStats.Gray.Values[0]); labelAnalysis.Text = String.Format("Analysis: {0} / {1} = {2,8:c}%", imgStats.Gray.Values[255], imgStats.Gray.Values[0], result * 100); pictureBoxResultImage.Image = analyzedImage = img; BlobCounter blobCounter = new BlobCounter(analyzedImage); Blob[] blobs = blobCounter.GetObjects(analyzedImage, false); double averageWidth = 0; double averageHeight = 0; double averageArea = 0; List <Antibody> antibodies = new List <Antibody>(); foreach (var blob in blobs) { averageHeight += (blob.Rectangle.Height); averageWidth += (blob.Rectangle.Width); averageArea += (blob.Area); if ((blob.Rectangle.Width > 1 || blob.Rectangle.Width > 1)) //TO BE REVISED! { antibodies.Add(new Antibody(blob.Rectangle.Location.X, blob.Rectangle.Y)); } else { //g.DrawEllipse(new Pen(Color.Red, 10), blob.Rectangle.X, blob.Rectangle.Y, 10, 10); } } averageHeight /= blobs.Length; averageWidth /= blobs.Length; averageArea /= blobs.Length; Console.WriteLine(averageHeight); Console.WriteLine(averageWidth); Console.WriteLine(averageArea); var analyzedImage2 = Utilities.CreateNonIndexedImage(analyzedImage); var g = Graphics.FromImage(analyzedImage2); foreach (var b in blobs) { if (b.Area > averageArea * 100) { g.DrawEllipse(new Pen(Color.Red, 2), b.Rectangle.X, b.Rectangle.Y, b.Rectangle.Width, b.Rectangle.Height); } } int dist = 0; int total = 0; for (int i = 0; i < antibodies.Count; ++i) { for (int j = 0; j < antibodies.Count; j++) { if (i != j) { int differenceX = (int)(antibodies[j].X - antibodies[i].X); int differenceY = (int)(antibodies[j].Y - antibodies[i].Y); dist += (int)(Math.Sqrt((differenceX * differenceX + differenceY * differenceY))); total++; } } } double eps = (dist / total) / 6; Console.WriteLine("EPS: {0}", eps); int minPts = 20; List <List <Antibody> > clusters = Clustering.DBSCAN.GetClusters(antibodies, eps, minPts); pictureBoxResultImage.Image = analyzedImage2 = Tools.ColorClusters(clusters, antibodies, analyzedImage2); tabControlImages.TabPages.Add("analyzed"); PictureBox picBox = new PictureBox(); picBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); picBox.Location = new System.Drawing.Point(6, 7); picBox.Name = "pictureBoxResultImage"; picBox.SizeMode = PictureBoxSizeMode.Zoom; picBox.Size = pictureBoxResultImage.Size; picBox.Image = analyzedImage2; (tabControlImages.TabPages[0].Controls[0] as PictureBox).Image = analyzedImage2; tabControlImages.TabPages[1].Controls.Add(picBox); tabControlImages.SelectTab(1); Console.WriteLine("Average dist: {0}", dist / total); }
//In ogni bitamp ricevuto, cerco eventuali aree che possano interferire nell'OCR e le elimino public Bitmap MenageBox(Bitmap boxAdapted) { Blob[] blobs1; //Lista dei rettangoli(blob) presenti nell'immagine List <Rectangle> checkRect = new List <Rectangle>(); //Lista dei rettangoli(blob) presenti nella parte in alto dell'immagine List <Rectangle> topRect = new List <Rectangle>(); //Lista dei rettangoli(blob) presenti nella parte a sinistra dell'immagine List <Rectangle> leftRect = new List <Rectangle>(); //Lista dei rettangoli(blob) presenti nella parte a destra dell'immagine List <Rectangle> rightRect = new List <Rectangle>(); // Crea un istanza di un BlobCounter BlobCounterBase bc1 = new BlobCounter(); Bitmap verify = new Bitmap(boxAdapted); // Processo l'immagine bc1.ProcessImage(verify); blobs1 = bc1.GetObjects(verify, false); //Ogni blob presente nell immagine finisce in checkRect foreach (Blob blob in blobs1) { checkRect.Add(blob.Rectangle); } foreach (Rectangle rect in checkRect) { if (rect.Bottom < verify.Height / 6) { topRect.Add(rect); } if (rect.Right < verify.Width / 8) { leftRect.Add(rect); } if (rect.X > verify.Width - (verify.Width / 8)) { rightRect.Add(rect); } } //Ordino le liste in maniera decresente in base al parametro che cerco var rectTop = topRect.OrderByDescending(r => r.Bottom).ToList(); var rectLeft = leftRect.OrderByDescending(r => r.Right).ToList(); var rectRight = rightRect.OrderByDescending(r => r.X).ToList(); //Lista di tutti dei rettangoli cercati List <Rectangle> rectMax = new List <Rectangle>(); if (topRect.Count != 0) { rectMax.Add(rectTop[0]); Crop cutTop = new Crop(new Rectangle(0, rectTop[0].Bottom, verify.Width, verify.Height - (rectTop[0].Bottom))); verify = cutTop.Apply(verify); } if (leftRect.Count != 0) { rectMax.Add(rectLeft[0]); Crop cutLeft = new Crop(new Rectangle(rectLeft[0].Right, 0, verify.Width - (rectLeft[0].Right), verify.Height)); verify = cutLeft.Apply(verify); } if (rightRect.Count != 0) { rectMax.Add(rectRight[0]); Crop cutRight = new Crop(new Rectangle(0, 0, verify.Width - (rectRight[0].Width), verify.Height)); verify = cutRight.Apply(verify); } //La funzione using ha solo funzione visiva(Non è necessaria per il funzionamento del codice) //Mi mostra in rosso quali rettangoli sono stati trovati /* * using (var gfx = Graphics.FromImage(verify)) * { * foreach (Rectangle rect in rectMax) * { * gfx.FillRectangle(Brushes.Red, rect); * } * gfx.Flush(); * } */ return(verify); }
public Bitmap ExtractPaperFromFlattened(Bitmap bitmap, Bitmap basicImage, int minBlobWidHei, int fillint, int contint) { // lock image, Bitmap itself takes much time to be processed BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); // step 2 - locating objects BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = minBlobWidHei; // both these variables have to be given when calling the blobCounter.MinWidth = minBlobWidHei; // method, the can also be queried from the XML reader using OMREnums blobCounter.ProcessImage(bitmapData); Blob[] blobs = blobCounter.GetObjectsInformation(); bitmap.UnlockBits(bitmapData); Graphics g = Graphics.FromImage(bitmap); // Pen yellowPen = new Pen(Color.Yellow, 2); // create pen in case image extraction failes and we need to preview the //blobs that were detected Rectangle[] rects = blobCounter.GetObjectsRectangles(); Blob[] blobs2 = blobCounter.GetObjects(bitmap, false); //Detection of paper lies within the presence of crossmark printed on the corneres of printed sheet. // First, detect left edge. // lc.jpg = Mirrored image sample as located on the corner of printed sheet // this helps filtering out much smaller and much larger blobs depending upon the size of image. // can be queried from XML Reader List <IntPoint> quad = new List <IntPoint>(); // Store sheet corner locations (if anyone is detected ) if (blobs2.GetLength(0) < 4 && contint == 0) { lExtractResult = ExtractResults.FAILED; return(basicImage); } try { foreach (Blob blob in blobs2) { if ( ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) > minbr && ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) < maxbr && blob.Rectangle.X < (bitmap.Width) / 4) // filters oout very small or very larg blobs { if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) // filters out blobs having insanely wrong aspect ratio { cb1 = UnmanagedImage.FromManagedImage(ImageUtilities.ResizeImage(iMarkLeft, blob.Rectangle.Width, blob.Rectangle.Height)); if (isSame(blob.Image, cb1)) { quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } } catch (ArgumentException) { lExtractResult = ExtractResults.NOBLOB; } try { // Sort out the list in right sequence, UpperLeft,LowerLeft,LowerRight,upperRight if (quad[0].Y > quad[1].Y) { IntPoint tp = quad[0]; quad[0] = quad[1]; quad[1] = tp; } } catch { } try { foreach (Blob blob in blobs2) { if ( ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) > minbr && ((double)blob.Area) / ((double)bitmap.Width * bitmap.Height) < maxbr && blob.Rectangle.X > (bitmap.Width * 3) / 4) { if ((double)blob.Rectangle.Width / blob.Rectangle.Height < 1.4 && (double)blob.Rectangle.Width / blob.Rectangle.Height > .6) { cb2 = UnmanagedImage.FromManagedImage(ImageUtilities.ResizeImage(iMarkRight, blob.Rectangle.Width, blob.Rectangle.Height)); if (isSame(blob.Image, cb2)) { quad.Add(new IntPoint((int)blob.CenterOfGravity.X, (int)blob.CenterOfGravity.Y)); } } } } } catch (ArgumentException) { lExtractResult = ExtractResults.NOBLOB; } try { if (quad[2].Y < quad[3].Y) { IntPoint tp = quad[2]; quad[2] = quad[3]; quad[3] = tp; } } catch { } g.Dispose(); //Again, filter out if wrong blobs pretended to our blobs. if (quad.Count == 4) { if (((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) < .75 || ((double)quad[1].Y - (double)quad[0].Y) / ((double)quad[2].Y - (double)quad[3].Y) > 1.25) { quad.Clear(); // clear if, both edges have insanely wrong lengths } else if (quad[0].X > bitmap.Width / 2 || quad[1].X > bitmap.Width / 2 || quad[2].X < bitmap.Width / 2 || quad[3].X < bitmap.Width / 2) { quad.Clear(); // clear if, sides appear to be "wrong sided" } } if (quad.Count != 4) // sheet not detected, reccurrsive call. { if (contint <= 60) //try altering the contrast correction on both sides of numberline { if (contint >= 0) { contint += 5; contint *= -1; return(ExtractOMRSheet(basicImage, fillint, contint)); } else { contint *= -1; contint += 10; return(ExtractOMRSheet(basicImage, fillint, contint)); } } else // contrast correction yeilded no result { lExtractResult = ExtractResults.FAILED; return(basicImage); } } else // sheet found { IntPoint tp2 = quad[3]; quad[3] = quad[1]; quad[1] = tp2; if (!CheckSheetAR(quad)) { lExtractResult = ExtractResults.INVALIDAR; return(basicImage); } //sort the edges for wrap operation QuadrilateralTransformation wrap = new QuadrilateralTransformation(quad); wrap.UseInterpolation = false; //perspective wrap only, no binary. wrap.AutomaticSizeCalculaton = false; wrap.NewWidth = tSheetSize.Width; wrap.NewHeight = tSheetSize.Height; lExtractResult = ExtractResults.OK; //wrap.Apply(basicImage);//.Save("LastImg.jpg", ImageFormat.Jpeg); // creat file backup for future use. return(wrap.Apply(basicImage)); // wrap } }
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { if (FileUpload1.HasFile) { string filename = "C:\\Users\\darwesh\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\Images\\" + FileUpload1.FileName; string filedir = "C:\\Users\\darwesh\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\"; FileUpload1.SaveAs("C:\\Users\\darwesh\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\Images\\" + FileUpload1.FileName); pixels16 = new List <ushort>(); Imagemri im = new Imagemri(); DicomDecoder dd = new DicomDecoder(); dd.DicomFileName = filename; imageWidth = dd.width; imageHeight = dd.height; bitDepth = dd.bitsAllocated; winCentre = dd.windowCentre; winWidth = dd.windowWidth; bool result = dd.dicomFileReadSuccess; if (result == true) { im.NewImage = true; if (bitDepth == 16) { pixels16.Clear(); dd.GetPixels16(ref pixels16); byte[] buffer = new byte[pixels16.Count * 2]; byte[] temp; ByteConverter d = new ByteConverter(); int j = 0; for (int i = 0; i < pixels16.Count; i++) { temp = System.BitConverter.GetBytes(pixels16[i]); buffer[j++] = temp[0]; buffer[j++] = temp[1]; } if (winCentre == 0 && winWidth == 0) { winWidth = 4095; winCentre = 4095 / 2; } } im.SetParameters(ref pixels16, imageWidth, imageHeight, winWidth, winCentre, true); string index = ""; foreach (string stt in dd.dicomInfo) { if (stt.Contains("Patient's Weight")) { index = stt; } } string wii = index.Split(':')[1]; foreach (string stt in dd.dicomInfo) { if (stt.Contains("Patient's Name")) { index = stt; } } string pn = index.Split(':')[1];; AForge.Imaging.Filters.Grayscale g1 = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap imagew = g1.Apply(im.bmp); int thresholding = (int)((dd.windowWidth - dd.windowCentre) * 255 / dd.windowWidth); AForge.Imaging.Filters.Threshold thf = new AForge.Imaging.Filters.Threshold(thresholding); Bitmap ther = thf.Apply(imagew); BlobCounter blobCounter = new BlobCounter(ther); Blob[] blobs = blobCounter.GetObjects(ther, false); ImageStatistics img; AForge.Imaging.Filters.GrayscaleToRGB d1 = new GrayscaleToRGB(); Bitmap bm = d1.Apply(imagew); Edges s = new Edges(); Graphics gg = Graphics.FromImage(bm); string ss = null; Bitmap myImage = null; Blob b; int count = 0; string locc = ""; foreach (Blob blob in blobs) { img = new ImageStatistics(blob.Image); double perc = ((double)img.PixelsCountWithoutBlack / (double)img.PixelsCount) * 100; if (blob.Image.Size.Height > 20 && blob.Image.Size.Width > 20 && perc > 35) { b = blob; ImageStatistics st = new ImageStatistics(b.Image); Bitmap pp = s.Apply(b.Image); ChannelFiltering c = new ChannelFiltering(new IntRange(0, 255), new IntRange(0, 0), new IntRange(0, 0)); Bitmap pp2 = d1.Apply(pp); c.ApplyInPlace(pp2); pp2.MakeTransparent(Color.Black); gg.DrawImage(pp2, b.Rectangle); gg.Flush(); myImage = im.bmp.Clone(b.Rectangle, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ss = ((double)(st.PixelsCountWithoutBlack) * (double)dd.pixelHeight * dd.pixelWidth).ToString(); locc = (b.Rectangle.Location.X * dd.pixelWidth).ToString() + "mm," + (b.Rectangle.Location.Y * dd.pixelHeight).ToString() + "mm"; count++; } }//end foreach bm.Save(filedir + FileUpload1.FileName + ".png", ImageFormat.Png); records r = new records(); recordsTableAdapters.recordsTableAdapter ta = new recordsTableAdapters.recordsTableAdapter(); ta.InsertRecord(pn, wii, FileUpload1.FileName, FileUpload1.FileName + ".png", "", ss, locc); } } }