I am working on wpf project, I want to know how to measure the angle of the stroke on the ink canvas? whether the stroke is right inclined or left inclined?
When you get a mouse down on the canvas, save away the current mouse position. Later, when you receive a mouse up event, you know the stroke is finished. Get the mouse position a second time and calculate the difference between the second position and the first position. Then you just have some simple math to do, and there are a couple cases to consider.
Note that this will not work so well for complex, curved strokes (because you will only receive a mouse-up event at the end of the stroke). This will work best in the case of single, straight lines.
Related
My current project has required me to learn face detection/tracking and image processing, given my experience in c#, I chose Emgu CV as my choice library for face detection and tracking. From what I've learned so far, I can do face detection and tracking, and basic image processing.
My goal is to be able to place virtual hair on the detected face. What I want to achieve is similar to [this video]: http://www.youtube.com/watch?v=BdPmECfUFcI.
What I would like to know is the technique(s) to use in handling hair placement for different kind of hairstyles on the detected face. In what image format do I store the the hair?
After watching the video I noticed it considers the head as a flat rectangle and not as a rectangular prism (the 3D object), so it doesn't consider the use of perspective transformations and I will not consider it too. This is a limitation but serves as a decent first step in doing such placements. Note that it is not a simply matter of taking perspective into consideration, your face tracking algorithm also needs to be able to handle more complicated configurations (the eyes might not be fully visible, for example).
So, the first thing you want is a bounding rectangle aligned according to the angle the eyes make with the x axis, illustrated in the following right figure (the red segment indicates the connection between the eyes). The left figure shows a typical bounding box aligned to the axis, which doesn't serve for this problem.
The problem is also simplified after you consider the head is symmetric, so you know the top middle point in the above figure is the middle of the top of your head. Also, considering that a typical head will likely be larger at top than at bottom, then you have something like in the following figure where the width of the rectangle is close to the width of the forehead. You could also consider a bounding rectangle on only upper half of the head, for example.
Now all that is left is positioning some object in this rectangle. For that, you need to augment the description of this object to be positioned so it is not purely pixels. We can define "entrance width" (EW) and "entrance middle point" (EM). This EW establishes the width needed in the other rectangle (the head one) to position it. So, if EW is smaller than the needed value, you upscale this object, respectively for when EW is larger. Note that the full width of the head's rectangle is usually an overestimation to position this object, so you can experiment with percentages of the width. The EM value is useful to know how you will position this object over the head. In the following figure, EW is the horizontal blue dashed horizontal, and EM is the middle point on it. The vertical blue line indicates how much over the EM you want to move this object inside the top segment of head's rectangle.
The only other special thing this object needs is a value that is considered as background. So when painting this object it is easy to know whether to make a point fully transparent (the background value) or fully opaque (anything else). This was the sketch I had in mind of what needs to be basically done.
I need a way to track the mouse coordinates more precisely than this code. If you run this code and move your mouse really fast or change directions fast, the coordinates might look this: 50 and then 40. It seems that when the mouse moves fast it doesn't track all the points the pointer covers, like in this example there are 10 coordinates that it skips. If I move it slowly, there's no problem. I need a way to track all the pointer coordinates with no skips. I've tried the sample on Code Project that uses global hooks, with the same result. How can I do this? Is there a registry change that can be made that forces windows to track all the coordinates. Is it possible? I would prefer to do it with C# but will consider other ways too. Thanks.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (num != 1)
{
listBox1.Items.Add(e.X.ToString());
}
}
Are you sure that the mouse covers all the coordinates that lie on the journey from A to B?
The mouse input from the OS is sampled, discreet data. This means that the mouse pointer can skip coordinates, just as you are seeing.
Consider interpolation over your data if it isn't hi-res enough.
IIRC, you can increase the sample-rate of the mouse. Can't remember how though. I'm sure the web can though.
The mouse pointer doesn't move across every pixel, if you move the mouse fast, it will move a whole bunch of pixels between events. The hardware simply doesn't send a signal for each pixel that the mouse moves, it reports the distance that the mouse has moved since the last report.
Instead of trying to track where the mouse is, use the Cursor.Clip property to limit the movement of the mouse:
var rect = someControl.RectangleToScreen(new Rectangle(Point.Empty, someControl.ClientSize));
Cursor.Position = new Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
Cursor.Clip = rect;
Use an empty rectangle to release the mouse:
Cursor.Clip = new Rectangle(0, 0, 0, 0);
Iv been asked this several times over the years. You generally cant tell mouse event listeners to tell you every single pixel the mouse passes though. Even the OS may skip large areas of the screen if you are moving the mouse fast enough. The only reliable way would be to plot the points yourself from the history of mouse positions you are given. The simple method would be to draw a line between the current point and the last one. More complex solutions would involve storing the last few coordinates and using sine / tangent math to plot a smooth curve.
I got a problem to solve. Look at the picture:
Image.
Red filled Ellipse is an obstacle.
Black dots are beginning and end of "road".
Blue line, is shortest way from one point to another.
I draw obstacles using mouse (mouse click creates ellipse), then i put in cords of start and the end, and then i use DrawLine to draw line from point to point. The question here is, how can i check or count, how many pixels of line, goes trough obstacle?
I thought of putting every pixel color in a 2D array before drawing the line, and then check it somehow on the numbers, counting shortest way from point to point, and checking how many 255's (obstacle number) would it meet in that array? As you see, i can create obstacle that way, so line will go trough the middle, just some part of it, or even next to it. I am simply lacking any idea how to do that. I need it, so i could rate every "road". More it goes trough obstacle, the less rate it gets.
Any ideas? Any algorithm?
Using the equations of the ellipse and the line, find the intersection points of the line with the outline of the ellipse (there may be 2, 1 or no intersections). You can find a worked example of this here.
If there are indeed 2 intersection points, use the distance formula to calculate the distance between them.
Is there a way to smooth out the mouse movements? I want to remove all the small jitter in normal mouse moving with the hand, like you can never draw a clean line in paint because of you hand doing small jittering movements.
This might be hard to understand what I mean, but if you know zbrush they have a feature that is called lazy mouse http://www.pixologic.com/docs/index.php/Lazy_Mouse im looking for a way to recreate this inside my app. I can read the mouse position with Cursor.Position but I don't find a way to average out these numbers before they get sent to the pointer on the screen.
This is only possible if you can delay the effect of the mouse movement slightly. You record the points of the mouse movement at a certain frequency and then average them out to a line. Then use that line to draw whatever you need. You wont be able to directly set the mouse cursor to the averaged position as that would then feedback into your program as a new mouse movement.
Make sure you build it so you can tweak how long you delay the mouse movement, and how aggressive the averaging is (say by restricting the number of points it includes), and the frequency at which you record mouse movements (this is could affect cpu usage if its too frequent).
You will of course have to create some sort of abstraction for the mouse in your application and create a way for the application to get hold of it. (I would be trying to keep this as similar as possible to normal winforms/wpf so I could revert the change and just use mouse movement directly if needed).
I am a beginner in C#, so be gentle.
In C#, I want draw curves and polygons like in Paint; where you hold the left mouse button to draw. Can you give some advice or code on how to do that?
Thank you.
For the beginning, try just tracing the mouse with Graphics.LineTo() - then start to play with mouse down and up events, then go from there. There is much to explore in the graphics area, and it should be lots of fun!
As for the array:
List<Point> points=new List<Point>();
later, on mouse move:
points.Add(new Point(mouse.X,mouse.Y));
and much later, if you need real point array
Point[] pa=points.ToArray();
You need to handle the mouse down, move and mouse up events while persisting some data which is then drawn on the Paint event of whatever control you are painting into.
Take a look at this CodeProject article for a good example of what you need to do.
For connecting points with a curve you should look at this article for drawing Bezier curves from a set of points. Here's another one that does spline interpolation.
The CodeProject article referenced by Paul Sasik is an excellent starting point. For drawing curved lines, you might want to try using Graphics.DrawBezier(...), which takes an array of points as a parameter, and renders a curved line through the points. This will have some complexity issues, though, as you need to decide how many consecutive points to use for each segment, and how to handle the running overlaps.