private void searchOverImages() { if (chosenshapes.Count == 0) { return; } //reset tracked shapes to only include starting shapes trackedshapes.Clear(); trackedshapes.AddRange(chosenshapes); int index = listBox1.SelectedIndex; for (int i = 0; i < listBox1.Items.Count; i++) { for (int j = 0; j < trackedshapes.Count; j++) { if (trackedshapes.ElementAt(j).imIndex == i - 1) { List <ShapeColorObject> templist = new List <ShapeColorObject>(); templist = findSimilarShapeinPicture(trackedshapes.ElementAt(j), LoadedImages["Image:" + i], i); if (templist.Count != 0) { ShapeColorObject tempshape = findClosest(templist, trackedshapes.ElementAt(j)); //ShapeColorObject tempshape = templist.ElementAt(0); tempshape.previousPosition = trackedshapes.ElementAt(j).pos; tempshape.prev = trackedshapes.ElementAt(j); tempshape.imIndex = i; tempshape.objIndex = tempshape.prev.objIndex; tempshape.type = tempshape.prev.type; trackedshapes.Add(tempshape); } } } } for (int i = index; i < listBox1.Items.Count; i++) { Image <Hsv, byte> tempimage = LoadedImages["Image:" + i].Copy(); for (int j = 0; j < trackedshapes.Count; j++) { if (trackedshapes.ElementAt(j).imIndex == i) { trackedshapes.ElementAt(j).drawOnImg(ref tempimage); tempimage.Draw(new Cross2DF(trackedshapes.ElementAt(j).predictedPos(), 5, 5), new Hsv(29, 170, 255), 3); } } workImages["Image:" + i] = tempimage; } }
/// <summary> /// Finds shapes similar to template in the given image. Similarity calculated by ShapeColorObject.compare method /// </summary> /// <param name="template"></param> /// <param name="image"></param> /// <returns></returns> private List <ShapeColorObject> findSimilarShapeinPicture(ShapeColorObject template, Image <Hsv, byte> image, int index) { Image <Gray, Byte> threshhImage = thresholdHSVtoGray(image, template.getColor()); List <ShapeColorObject> samecoloredshapes = findShapesinGrayImg(threshhImage, image, index); List <ShapeColorObject> similar = new List <ShapeColorObject>(); for (int i = 0; i < samecoloredshapes.Count; i++) { //only adds found object if similar to template and not yet tracked if (template.compare(samecoloredshapes.ElementAt(i), 5, 30) && !trackedshapes.Contains(samecoloredshapes.ElementAt(i))) { similar.Add(samecoloredshapes.ElementAt(i)); } } return(similar); }
private ShapeColorObject findClosest(List <ShapeColorObject> list, ShapeColorObject template) { if (list.Count == 1) { return(list.ElementAt(0)); } else { Point predicted = template.predictedPos(); //--Find closest shape--------- ShapeColorObject temp = null; int dist = 0; foreach (ShapeColorObject shp in list) { int d = (shp.pos.X - predicted.X) * (shp.pos.X - predicted.X) + (shp.pos.Y - predicted.Y) * (shp.pos.Y - predicted.Y); if (temp == null || d < dist) { temp = shp; dist = d; } } return(temp); } }
/// <summary> /// Returns a List of Shapes found in the gray Image and returns Linesegments and color of shape /// </summary> /// <param name="inImg">Gray Image</param> /// <param name="refImg">Color Image for shapecolor</param> /// <param name="lines">outputarray for lines</param> /// <param name="colors">outputarry for linecolors</param> /// <returns></returns> public List <ShapeColorObject> findShapesinGrayImg(Emgu.CV.Image <Gray, Byte> inImg, Emgu.CV.Image <Hsv, Byte> refImg, int index) { if (inImg == null || refImg == null) { return(null); } List <ShapeColorObject> funkshapes = new List <ShapeColorObject>(); for (Contour <Point> contours = inImg.FindContours(); contours != null; contours = contours.HNext) { Contour <Point> current = contours.ApproxPoly(contours.Perimeter * 0.008); if (current.Area > 200) { Point[] points = current.ToArray(); int meanX = 0; int meanY = 0; foreach (Point p in points) { meanX += p.X; meanY += p.Y; } meanX /= current.Total; meanY /= current.Total; ShapeColorObject newObj; Hsv col; if (current.Total == 3) //3 vertices { col = new Hsv(0, 255, 220); newObj = new ShapeColorObject(current.Area, ShapeColorObject.shape.tri, refImg[meanY, meanX], meanX, meanY); newObj.imIndex = index; funkshapes.Add(newObj); } else if (current.Total == 4 && current.Convex) //4 vertices convex { col = new Hsv(45, 255, 220); newObj = new ShapeColorObject(current.Area, ShapeColorObject.shape.rect, refImg[meanY, meanX], meanX, meanY); newObj.imIndex = index; funkshapes.Add(newObj); } else if (current.Total > 8 && current.Convex) //8+ vertices (circle) convex { col = new Hsv(90, 255, 220); newObj = new ShapeColorObject(current.Area, ShapeColorObject.shape.circle, refImg[meanY, meanX], meanX, meanY); newObj.imIndex = index; funkshapes.Add(newObj); } else //other shapes { col = new Hsv(135, 255, 220); newObj = new ShapeColorObject(current.Area, ShapeColorObject.shape.undef, refImg[meanY, meanX], meanX, meanY); newObj.imIndex = index; funkshapes.Add(newObj); } for (int i = 0; i < current.Total - 1; i++) { newObj.lineSegments.Add(new LineSegment2D(points[i], points[i + 1])); //add contour line segments to new ShapeColorObject } newObj.lineSegments.Add(new LineSegment2D(points[current.Total - 1], points[0])); double cirumf = 0; foreach (LineSegment2D s in newObj.lineSegments) { cirumf += s.Length; } newObj.circularity = (float)(cirumf * cirumf / current.Area); } //ende if(200>area) } //ende von for(contours....) return(funkshapes); }
private void imageBox1_Click(object sender, EventArgs e) { if (im1.Image == null) { return; } //--Get Mouseposition in Pixelcoordinates-------- var mouseEventArgs = e as MouseEventArgs; int imWidth, imHeight, boxWidth, boxHeight; imWidth = im1.Image.Size.Width; imHeight = im1.Image.Size.Height; boxWidth = im1.Size.Width; boxHeight = im1.Size.Height; Point mouse = TranslateZoomMousePosition(mouseEventArgs.X, mouseEventArgs.Y, imWidth, imHeight, boxWidth, boxHeight); int x = mouse.X; //(int)(mouseEventArgs.X / im1.ZoomScale); int y = mouse.Y; //(int)(mouseEventArgs.Y / im1.ZoomScale); Emgu.CV.Image <Hsv, byte> original = LoadedImages[listBox1.SelectedItem.ToString()]; Hsv pcolor = original[y, x]; if (mouseEventArgs != null) { label1.Text = "@(" + x + "; " + y + "): " + pcolor.ToString(); } Emgu.CV.Image <Gray, byte> threshedimage; threshedimage = thresholdHSVtoGray(original, pcolor); imageBox2.Image = threshedimage; shapes.Clear(); Emgu.CV.Image <Hsv, byte> refImg = (Emgu.CV.Image <Hsv, byte>)im1.Image; Emgu.CV.Image <Gray, byte> inImg = (Emgu.CV.Image <Gray, byte>)im2.Image; Emgu.CV.Image <Hsv, byte> outImg = inImg.Convert <Hsv, byte>(); shapes.AddRange(findShapesinGrayImg(inImg, refImg, listBox1.SelectedIndex)); //--Find closest shape--------- ShapeColorObject temp = null; int dist = 0; foreach (ShapeColorObject shp in shapes) { int d = (shp.pos.X - mouse.X) * (shp.pos.X - mouse.X) + (shp.pos.Y - mouse.Y) * (shp.pos.Y - mouse.Y); if (ShapeColorObject.compareHues(shp.getColor().Hue, pcolor.Hue, dHue)) { if (temp == null || d < dist) { temp = shp; temp.imIndex = listBox1.SelectedIndex; dist = d; } } } if (temp != null && !chosenshapes.Contains(temp)) { temp.objIndex = chosenshapes.Count; chosenshapes.Add(temp); //trackedshapes.Add(temp); } foreach (ShapeColorObject shp in chosenshapes) { shp.drawOnImg(ref outImg); im2.Image = outImg; im2.Update(); } label2.Text = ""; foreach (ShapeColorObject shape in chosenshapes) { label2.Text += shape.toString() + "\n"; } }