分享

Cropping Particular Region In Image Using C#

 goodwangLib 2018-02-01

Introduction 

       Firstly,  I have to explain why we need this method to crop an image. I prepared and coded this simple application GUI for my requirement, because in our big project we need to focus some region of image that we’re interested in.  So when I start to investigate this requirement and how to do this, I have met only rectangular, circle or square region to crop image. If region that we’re interested in is not rectangular, square, circle etc , what we have to do? We should be able to choose all of points of our region in image.  After now, you can crop particular area in image that you want.  

Figure-1: Crop particular region of image GUI 

Background 

     Firstly, I have to explain theGUI. In this article, i share a screenshot of image crop GUI application. Asyou see the figure-1, we have to determine which area of our image sample. Inthis sample, we choose with a piece of wood on the rock in the lake and we wantto seperate this image as two parts. One is only piece of wood on the rock and theother is just the sample of image. As you see, labeled as cropped image is oneand labeled as original image is the other.  The picturebox is labeled as the name of imageis the field to choose crop points. Of course you have to choose the image thatyou want crop with open option from file menu strip. As you can see the redlines is consist of points choosed with mouse click. At the above GUI screen,the crop button is to serve creating cropped image after you select the points.If you don’t like that you select point, maybe you swapped your hand, the undobutton serves you to undo your selection and to provide select true points. Youcan choose this functions from the options menu strip, and you can save thecropped image to use save option from file menu strip. The mouse points textboxshows the coordinates of your mouse in the image and the polygon points textboxshows you maximum and minumum points that you’ll create.

     Let’s explain some methods tounderstand this simple GUI application. At the background, we use so simplemethod to crop image. We use so popular method of image processing is maskingtwo images. Source image is the original image and the second one is  just only full of black image( the blackimage has rgb values are ‘0’) has same size with original image. But the tip ofmethod is the polygon is formed by points that we choose. We have no just blackimage has rgb values are ‘0’, only inside the polygon has rgb values are ‘1’.And we multiply (mask) this two image, we have cropped image. So that’s it, itis so simple. 

Using the code

      The codes are seperated two mainparts. One of them is choosing polygonpoints, we use pictureBox1_Mouse_Click() event and pictureBox1_MouseMove()event. MouseMove event serves us to know which coordinates value that we are, whenwe drag mouse cursor on the picturebox that contains our image. MouseClickevent serves us to add polygon points with clicking mouse left button, and wecan undo with clicking mouse right button. We need  some temp variables to draw polygon.      

//to choose points of crop regionprivate void pictureBox1_MouseClick(object sender, MouseEventArgs e){ switch (e.Button) { case MouseButtons.Left: temp_point = new System.Drawing.Point(e.X, e.Y); temp_count = polygonPoints.Count; polygonPoints.Add(new System.Drawing.Point(e.X, e.Y)); if (polygonPoints.Count > 1) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); temp_im = bmp.Clone(rect, PixelFormat.Format24bppRgb); this.DrawLine(polygonPoints[polygonPoints.Count - 2], polygonPoints[polygonPoints.Count - 1]); } break; // in this point we can undo easily, with pushing mouse right click case MouseButtons.Right: if (polygonPoints.Count > 0) { undo(); } break; } pictureBox1.Image = bmp;} 

     The second main part is crop()method that we crop region of image. We fill rectangle first to create blackimage has same size with the original image and fill ‘1’ rgb values inside thepolygon. After of that, we multiply two images with using Cv.Mul(CvArrsrc1,CvArr src2, CvArr dst, double scale) method.  

  private void crop() { timer1.Stop(); IplImage accc = Cv.CreateImage(new CvSize(bmp.Width, bmp.Height), BitDepth.U8, 3); Graphics Ga = Graphics.FromImage(bmp); //the black image Ga.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, bmp.Width, bmp.Height)); //draw from the last point to first point Ga.DrawLine(new Pen(Color.Red, 3), polygonPoints[polygonPoints.Count - 1], polygonPoints[0]); //all of the rgb values are being set 1 inside the polygon SolidBrush Brush = new SolidBrush(Color.FromArgb(1, 1, 1)); //we have to prepare one mask of Multiplying operation for cropping region G.FillClosedCurve(Brush, polygonPoints.ToArray()); Cv.Mul(BitmapToIplImage(Source), BitmapToIplImage(bmp), accc, 1); computecrop(); croplast = accc.ToBitmap().Clone(rectcrop, Source.PixelFormat);//just show cropped region part of image pictureBox2.Image = croplast; // crop region of image

     Of course, we need a method toconvert from bitmap to IplImage so BitmapToIplImage(Bitmap src) method worksfor this purpose.   

// we have to conversion from bitmap to IplImage to use OpenCvSharp methodspublic static IplImage BitmapToIplImage(Bitmap bitmap){ IplImage tmp, tmp2; Rectangle bRect = new Rectangle(new System.Drawing.Point(0, 0), new Size((int)bitmap.Width, (int)bitmap.Height)); BitmapData bmData = bitmap.LockBits(bRect, ImageLockMode.ReadWrite, bitmap.PixelFormat); tmp = Cv.CreateImage(Cv.Size(bitmap.Width, bitmap.Height), BitDepth.U8, 3); tmp2 = Cv.CreateImage(Cv.Size(bitmap.Width, bitmap.Height), BitDepth.U8, 1); tmp.ImageData = bmData.Scan0; ; bitmap.UnlockBits(bmData); // Cv.CvtColor(tmp, tmp2, ColorConversion.RgbToGray); return tmp;} 

     And then, we have to use some refresh method to free ram and to use sometemp variables. The temp variables are for using graphics class methods andcreate masking object to multiply two images. 

// we have refresh some assignments operators for new image or cropping new regionprivate void first_refresh_im(){ Rectangle rect = new Rectangle(0, 0, resim.Width, resim.Height); resimi = resim.ToBitmap() ; bmp = resimi.Clone(rect, resimi.PixelFormat); Source = resimi.Clone(rect, resimi.PixelFormat); pictureBox1.Image = bmp; G = Graphics.FromImage(bmp); polygonPoints.Clear();}// when we undo point of region, we have to create new rectangular object with using ex-points// and we have to need new bmp Source objectprivate void refresh_im(){ Rectangle rect = new Rectangle(0, 0, resim.Width, resim.Height); bmp = resimi.Clone(rect, resimi.PixelFormat); Source = resimi.Clone(rect, resimi.PixelFormat); pictureBox1.Image = bmp;} 

Points of Interest

     I hope this GUI would be useful forpeople who interested in image processing methods. I used some opencvsharpmethods, because in image processing implementations most of people use thislibrary and source codes. This article will able to use first level tutorial ofopencvsharp simple methods for people.   

Conclusion 

     In this article, we learned some image processing methods and how to usethose in .NET platform and we already met opencvsharp methods. After now, wedon’t need some other programs for just a particular area image croppingprocesses. And we can crop particular region in a image that we want, not onlyrectangular, square, circle shapes. 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多