Click here to Skip to main content
12,697,783 members (26,210 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hello all,

I am working on object extraction from image, I can detect the object then convert to list of points (outer boundaries of the object), but the problem is when I tried to crop this object by its points I got many problems

I need to extract the object rounded by its points which I have.


this is my code
//////////////////////////////////////////////////////////////////////
 // create a graphic path to hold the shape data
 System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
 // add a set of points that define the shape
 path.AddLines(pts);
  

 // close the shape
 path.CloseAllFigures();
 // create graphics object
 Graphics graph = pictureBox2.CreateGraphics();
 // set the clop region of the forms graphic object to be the new shape
 graph.Clip = new Region(path);
 // draw the image cliped to the custom shape
 graph.DrawImage(pictureBox1.Image, new Point(0, 0));
 //////////////////////////////////////////////////////////////////



the region which is cliped is less than the object in the points in one pixel in all directions


please help!!!!!


what to do?????
Posted 15-Nov-12 5:40am
samerfs593
Updated 15-Nov-12 5:55am
digimanus58.5K
v2
Comments
Christian Graus 15-Nov-12 10:48am
   
Less in what way ? Do you need to include the same point at the beginning and end ? Is there a logic to how it clips right now, that is, can you see where it goes wrong ?
Sergey Alexandrovich Kryukov 15-Nov-12 11:11am
   
First of all, you should stop re-posting your questions. Use the page of your original question ("Improve question", comment on existing posts).

Also, please don't post using "Add your solution here" -- this is reserved just for the cases when you provide some help to someone who posted the question. Non-solution posts will be removed.

Also, with all wrong posts, you risk getting abuse reports from members.

--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Have a look at Graphics.SetClip[^]

You use it like this:

//////////////////////////////////////////////////////////////////////
// create a graphic path to hold the shape data
 System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
 // add a set of points that define the shape
 path.AddLines(pts);
  
 // close the shape
 path.CloseAllFigures();
 // create graphics object
 Graphics graph = pictureBox2.CreateGraphics();
 // set the clop region of the forms graphic object to be the new shape
 graph.SetClip(path, CombineMode.Replace);

 // draw the image cliped to the custom shape
 graph.DrawImage(pictureBox1.Image, new Point(0, 0));
 //////////////////////////////////////////////////////////////////



Best regards
Espen Harlinn
  Permalink  
Comments
Sergey Alexandrovich Kryukov 15-Nov-12 11:13am
   
My 5. (And it was a re-post; I did not remove it because this answer was already provided.)
--SA
Espen Harlinn 15-Nov-12 11:14am
   
Thank you, Sergey :-D
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

thank you for your reply

but the problem still, the problem exactly if you have a line in an image and the height of the line is 4 pixels and the width of the line is 100 pixel, after doing this function it return line with 3 pixels height and 99 pixels width,

I think the problem exactly on the graphicspath it fill what is inside the lines and the border of graphics path is not filled, so the returned image will lose these pixels


what to do??
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100