I"m working on a wpf project using kinect SDK and OpenCV , what I need to do is to create a box that follow the blob that showing in the kinect camera and check if the blob is inside a rectangle that been added by choosing 4 dots (corners of the recangle)
MCvBox2D box = contours.GetMinAreaRect();
openCVImg.Draw(box, new Bgr(System.Drawing.Color.Red), 2);
POINT p = new POINT { x = (int)box.center.X, y = (int)box.center.Y };
double minX = Math.Min(Canvas.GetLeft(dot1), Math.Min(Canvas.GetLeft(dot2), Math.Min(Canvas.GetLeft(dot3), Canvas.GetLeft(dot4))));
double minY = Math.Min(Canvas.GetTop(dot1), Math.Min(Canvas.GetTop(dot2), Math.Min(Canvas.GetTop(dot3), Canvas.GetTop(dot4))));
double maxX = Math.Max(Canvas.GetLeft(dot1), Math.Max(Canvas.GetLeft(dot2), Math.Max(Canvas.GetLeft(dot3), Canvas.GetLeft(dot4))));
double maxY = Math.Max(Canvas.GetTop(dot1), Math.Max(Canvas.GetTop(dot2), Math.Max(Canvas.GetTop(dot3), Canvas.GetTop(dot4))));
if (p.x >= minX && p.x <= maxX && p.y >= minY && p.y <= maxY)
{
...
}
the box is what created around the blob , and the dot1,..,dot4 is the corners of the rectangle and the POINT p is the center of the box . the if statement checking if the box found inside the rectangle
the problem is when I checked the coordinates of the Dots , they showed different coordinates from the point p . for example when box.X was over minX , it show the the coordinate of box.X = 125 and the coordinate of maxX=634.
I"ll more than happy to have any ideas what I did wrong here and how to fix it.
thanks
You can use
Visual.PointFromScreento convert a point expressed in screen coordinates to the coordinate system of aVisual(in your case the hostingCanvaselement).You should also avoid mixing
System.DrawingandSystem.Windowsnamespaces (and even Win32 types likePOINT).Because this is a WPF application you should immediately convert all
System.Drawingtypes returned from the OpenCV API toSystem.Windows(WPF) types.You can further improve your code by using
Rect.Containsto test if aPointis contained within the rectangle defined by aRect. Handling aRectinstead of "loose" coordinate points at least improves the readability of the code.