private void button7_Click(object sender, EventArgs e) { MatImage m1 = new MatImage(mimg); m1.Convert(); mimgInGray = m1.Out(); MatImage m2 = new MatImage(mimgInGray); m2.SmoothGaussian(3); m2.ThresholdBinaryInv(245, 255); // this causes an exception, and I don't know how to fix that yet. //The good news is that this really doesn't matter for small images //m2.MorphologyEx(2); mimgInGray = m2.Out(); MatImage m3 = new MatImage(mimgInGray); VectorOfVectorOfPoint contours = m3.FindContours(); List <ColorfulContourMap> cmaps = ColorfulContourMap.getAllContourMap(mimg, 0); //CvInvoke.DrawContours(mimg, contours,-1, new Bgr(255,0,0).MCvScalar,2); Mat mimg2 = new Mat(new Size(mimg.Width, mimg.Height), DepthType.Cv8U, 3); foreach (ColorfulContourMap cmap in cmaps) { cmap.DrawColorTo(mimg2); } pictureBox1.Image = mimg.Bitmap; pictureBox2.Image = mimg2.Bitmap; }
private void button6_Click(object sender, EventArgs e) { OpenFileDialog Openfile = new OpenFileDialog(); if (Openfile.ShowDialog() == DialogResult.OK) { long start = DateTime.Now.ToFileTime() * 10 / 1000000; for (int i = 0; i < 1; i++) { mimg = new Mat(Openfile.FileName, LoadImageType.AnyColor); // block for using my helper class for Mat { // pipeline for using my helper class // load MatImage m1 = new MatImage(mimg); // operations m1.Resize(0.5); // other operations // output mimg = m1.Out(); } pictureBox2.Image = mimg.Bitmap; } Console.WriteLine(DateTime.Now.ToFileTime() * 10 / 1000000 - start); } }
private Mat generateThumbnail(Mat input) { MatImage m1 = new MatImage(input); m1.ResizeTo(150, 150); return(m1.Out()); }
private void button4_Click(object sender, EventArgs e) { long start = DateTime.Now.ToFileTime() * 10 / 1000000; for (int i = 0; i < 1000; i++) { MatImage m1 = new MatImage(mimg); m1.Translate(-1 + 2 * i % 2, -1 + 2 * i % 2); // don't let the picture move out of the box // engineering code Mat mim = m1.Out(); pictureBox2.Image = mim.Bitmap; } Console.WriteLine(DateTime.Now.ToFileTime() * 10 / 1000000 - start); }
private void button2_Click(object sender, EventArgs e) { long start = DateTime.Now.ToFileTime() * 10 / 1000000; for (int i = 0; i<1; i++) { MatImage m1 = new MatImage(mimg); m1.Rotate(45,new Bgr(255,255,255)); // engineering code //Mat mim = m1.Out(); //pictureBox2.Image = mim.Bitmap; // artistic code mimg = m1.Out(); pictureBox2.Image = mimg.Bitmap; } Console.WriteLine(DateTime.Now.ToFileTime() * 10 / 1000000 - start); }
private void button2_Click(object sender, EventArgs e) { long start = DateTime.Now.ToFileTime() * 10 / 1000000; for (int i = 0; i < 1; i++) { MatImage m1 = new MatImage(mimg); m1.Rotate(45, new Bgr(255, 255, 255)); // engineering code //Mat mim = m1.Out(); //pictureBox2.Image = mim.Bitmap; // artistic code mimg = m1.Out(); pictureBox2.Image = mimg.Bitmap; } Console.WriteLine(DateTime.Now.ToFileTime() * 10 / 1000000 - start); }
private void refresh(bool colored) { pic1Copy = pic1.Clone(); pic2Copy = pic2.Clone(); if (!colored) { map1.DrawTo(pic1Copy); map2.DrawTo(pic2Copy); } else { map1.DrawColorTo(pic1Copy); map2.DrawColorTo(pic2Copy); } MatImage m1 = new MatImage(pic1Copy); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = m1.Out().Bitmap; MatImage m2 = new MatImage(pic2Copy); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); pictureBox2.Image = m2.Out().Bitmap; }
private void displayFragments(PictureBox pb) { // determine the index of the first contour map for a image int firstAppear = 0; bool blackOrWhite = false; foreach (ColorfulContourMap cmap in Form1.blackContourMaps) { if (cmap.imageIndex == num) { blackOrWhite = false; goto black; } firstAppear++; } firstAppear = 0; foreach (ColorfulContourMap cmap in Form1.whiteContourMaps) { if (cmap.imageIndex == num - Form1.blackSourceImages.Count) { blackOrWhite = true; break; } firstAppear++; } black: if (!blackOrWhite) { Mat img1 = new Mat(); if (pb == pictureBox3) { ind1 = firstAppear + listBox1.SelectedIndex; img1 = Form1.blackCroppedImages[ind1].Clone(); Form1.blackContourMaps[ind1].DrawTo(img1); } else { ind2 = firstAppear + listBox1.SelectedIndex; img1 = Form1.blackCroppedImages[ind2].Clone(); Form1.blackContourMaps[ind2].DrawTo(img1); } { MatImage m2 = new MatImage(img1); m2.ResizeTo(pb.Width, pb.Height); img1 = m2.Out(); } pb.Image = img1.Bitmap; blackOWhite = false; } else { Mat img1 = new Mat(); if (pb == pictureBox3) { ind1 = firstAppear + listBox1.SelectedIndex; img1 = Form1.whiteCroppedImages[ind1].Clone(); Form1.whiteContourMaps[ind1].DrawTo(img1); } else { ind2 = firstAppear + listBox1.SelectedIndex; img1 = Form1.whiteCroppedImages[ind2].Clone(); Form1.whiteContourMaps[ind2].DrawTo(img1); } { MatImage m2 = new MatImage(img1); m2.ResizeTo(pb.Width, pb.Height); img1 = m2.Out(); } pb.Image = img1.Bitmap; blackOWhite = true; } }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { int firstAppear = 0; bool blackOrWhite = false; // false=black foreach (ColorfulContourMap cmap in Form1.blackContourMaps) { if (cmap.imageIndex == num) { blackOrWhite = false; goto black; } firstAppear++; } firstAppear = 0; foreach (ColorfulContourMap cmap in Form1.whiteContourMaps) { if (cmap.imageIndex == num - Form1.blackSourceImages.Count) { blackOrWhite = true; break; } firstAppear++; } black : if (!blackOrWhite) // black { Mat img1 = Form1.blackSourceImages[num].Clone(); Mat img2 = Form1.blackSourceImages[num].Clone(); img2.SetTo(new MCvScalar(255, 255, 255)); //Form1.blackContourMaps[firstAppear + listBox1.SelectedIndex].DrawTo(img1); //Form1.blackContourMaps[firstAppear + listBox1.SelectedIndex].DrawColorTo(img2); { MatImage m1 = new MatImage(img1); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); img1 = m1.Out(); } pictureBox1.Image = img1.Bitmap; { MatImage m2 = new MatImage(img2); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); img2 = m2.Out(); } pictureBox2.Image = img2.Bitmap; } else // white { Mat img1 = Form1.whiteSourceImages[num - Form1.blackSourceImages.Count].Clone(); Mat img2 = Form1.whiteSourceImages[num - Form1.blackSourceImages.Count].Clone(); img2.SetTo(new MCvScalar(0)); //Form1.whiteContourMaps[firstAppear + listBox1.SelectedIndex].DrawTo(img1); //Form1.whiteContourMaps[firstAppear + listBox1.SelectedIndex].DrawColorTo(img2); { MatImage m1 = new MatImage(img1); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); img1 = m1.Out(); } pictureBox1.Image = img1.Bitmap; { MatImage m2 = new MatImage(img2); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); img2 = m2.Out(); } pictureBox2.Image = img2.Bitmap; } }
private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked) { pic1Copy = pic1.Clone(); pic2Copy = pic2.Clone(); map1.DrawTo(pic1Copy); map2.DrawTo(pic2Copy); edgeMatch = DNAUtil.partialMatch(DNA1, DNA2); List<Point> pointToDraw1 = new List<Point>(); List<Point> pointToDraw2 = new List<Point>(); for (int i = edgeMatch.t11; i < edgeMatch.t12; i++) { pointToDraw1.Add(new Point((int)DNA1[i].x, (int)DNA1[i].y)); } for (int i = edgeMatch.t21; i < edgeMatch.t22; i++) { pointToDraw2.Add(new Point((int)DNA2[i].x, (int)DNA2[i].y)); } CvInvoke.Polylines(pic1Copy, pointToDraw1.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); CvInvoke.Polylines(pic2Copy, pointToDraw2.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); MatImage m1 = new MatImage(pic1Copy); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = m1.Out().Bitmap; MatImage m2 = new MatImage(pic2Copy); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); pictureBox2.Image = m2.Out().Bitmap; } else { pic1Copy = pic1.Clone(); pic2Copy = pic2.Clone(); map1.DrawTo(pic1Copy); map2.DrawTo(pic2Copy); edgeMatch = DNAUtil.partialColorMatch(DNA1, DNA2); List<Point> pointToDraw1 = new List<Point>(); List<Point> pointToDraw2 = new List<Point>(); if (edgeMatch.t11 > edgeMatch.t12) { for (int i = edgeMatch.t12; i < edgeMatch.t11; i++) { pointToDraw1.Add(new Point((int)DNA1[i].x, (int)DNA1[i].y)); } } else { for (int i = edgeMatch.t11; i < edgeMatch.t12; i++) { pointToDraw1.Add(new Point((int)DNA1[i].x, (int)DNA1[i].y)); } } if (edgeMatch.t21 > edgeMatch.t22) { for (int i = edgeMatch.t22; i < edgeMatch.t21; i++) { pointToDraw2.Add(new Point((int)DNA2[i].x, (int)DNA2[i].y)); } } else { for (int i = edgeMatch.t21; i < edgeMatch.t22; i++) { pointToDraw2.Add(new Point((int)DNA2[i].x, (int)DNA2[i].y)); } } CvInvoke.Polylines(pic1Copy, pointToDraw1.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); CvInvoke.Polylines(pic2Copy, pointToDraw2.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); MatImage m1 = new MatImage(pic1Copy); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = m1.Out().Bitmap; MatImage m2 = new MatImage(pic2Copy); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); pictureBox2.Image = m2.Out().Bitmap; } }
private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked) { pic1Copy = pic1.Clone(); pic2Copy = pic2.Clone(); map1.DrawTo(pic1Copy); map2.DrawTo(pic2Copy); edgeMatch = DNAUtil.partialMatch(DNA1, DNA2); List <Point> pointToDraw1 = new List <Point>(); List <Point> pointToDraw2 = new List <Point>(); for (int i = edgeMatch.t11; i < edgeMatch.t12; i++) { pointToDraw1.Add(new Point((int)DNA1[i].x, (int)DNA1[i].y)); } for (int i = edgeMatch.t21; i < edgeMatch.t22; i++) { pointToDraw2.Add(new Point((int)DNA2[i].x, (int)DNA2[i].y)); } CvInvoke.Polylines(pic1Copy, pointToDraw1.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); CvInvoke.Polylines(pic2Copy, pointToDraw2.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); MatImage m1 = new MatImage(pic1Copy); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = m1.Out().Bitmap; MatImage m2 = new MatImage(pic2Copy); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); pictureBox2.Image = m2.Out().Bitmap; } else { pic1Copy = pic1.Clone(); pic2Copy = pic2.Clone(); map1.DrawTo(pic1Copy); map2.DrawTo(pic2Copy); edgeMatch = DNAUtil.partialColorMatch(DNA1, DNA2); List <Point> pointToDraw1 = new List <Point>(); List <Point> pointToDraw2 = new List <Point>(); if (edgeMatch.t11 > edgeMatch.t12) { for (int i = edgeMatch.t12; i < edgeMatch.t11; i++) { pointToDraw1.Add(new Point((int)DNA1[i].x, (int)DNA1[i].y)); } } else { for (int i = edgeMatch.t11; i < edgeMatch.t12; i++) { pointToDraw1.Add(new Point((int)DNA1[i].x, (int)DNA1[i].y)); } } if (edgeMatch.t21 > edgeMatch.t22) { for (int i = edgeMatch.t22; i < edgeMatch.t21; i++) { pointToDraw2.Add(new Point((int)DNA2[i].x, (int)DNA2[i].y)); } } else { for (int i = edgeMatch.t21; i < edgeMatch.t22; i++) { pointToDraw2.Add(new Point((int)DNA2[i].x, (int)DNA2[i].y)); } } CvInvoke.Polylines(pic1Copy, pointToDraw1.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); CvInvoke.Polylines(pic2Copy, pointToDraw2.ToArray(), false, new Bgr(0, 255, 0).MCvScalar, 2); MatImage m1 = new MatImage(pic1Copy); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = m1.Out().Bitmap; MatImage m2 = new MatImage(pic2Copy); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); pictureBox2.Image = m2.Out().Bitmap; } }
// get all of the valid contour maps, valid means circumfence > 200 px // this was not in their code, I added this feature, but I used their logic public static List <ColorfulContourMap> getAllContourMap(Mat input, int index, int mode = 0) { // use for all members List <ColorfulContourMap> result = new List <ColorfulContourMap>(); MatImage m1 = new MatImage(input); m1.Convert(); Mat gray = m1.Out(); // use for black background if (mode == 0) { MatImage m2 = new MatImage(gray); m2.SmoothGaussian(3); m2.ThresholdBinaryInv(245, 255); gray = m2.Out(); } // use for white background else { MatImage m2 = new MatImage(gray); m2.SmoothGaussian(3); m2.ThresholdBinaryInv(100, 255); gray = m2.Out(); } // one time use List <Point> pointList = new List <Point>(); List <Point> polyPointList = new List <Point>(); List <ColorfulPoint> cps = new List <ColorfulPoint>(); List <ColorfulPoint> pcps = new List <ColorfulPoint>(); // fetch all the contours using Emgu CV // fetch all the polys using Emgu CV // extract the points and colors Mat temp = gray.Clone(); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(gray, contours, new Mat(), RetrType.List, ChainApproxMethod.ChainApproxNone); double area = Math.Abs(CvInvoke.ContourArea(contours[0])); VectorOfPoint maxArea = contours[0]; // maxArea is used as the current contour //contour = contour.HNext; // use this to loop for (int i = 0; i < contours.Size; i++) { double nextArea = Math.Abs(CvInvoke.ContourArea(contours[i], false)); // Find the area of contour area = nextArea; if (area >= Constants.MIN_AREA) { maxArea = contours[i]; VectorOfPoint poly = new VectorOfPoint(); CvInvoke.ApproxPolyDP(maxArea, poly, 1.0, true); pointList = maxArea.ToArray().ToList(); polyPointList = poly.ToArray().ToList(); foreach (Point p in pointList) { ColorfulPoint cp = new ColorfulPoint { X = p.X, Y = p.Y, color = extractPointColor(p, input) }; cps.Add(cp); } foreach (Point p in polyPointList) { ColorfulPoint cp = new ColorfulPoint { X = p.X, Y = p.Y, color = extractPointColor(p, input) }; pcps.Add(cp); } result.Add(new ColorfulContourMap(cps, pcps, index)); // clear temporal lists pointList = new List <Point>(); polyPointList = new List <Point>(); cps = new List <ColorfulPoint>(); pcps = new List <ColorfulPoint>(); } } return(result); }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { int firstAppear = 0; bool blackOrWhite = false; // false=black foreach (ColorfulContourMap cmap in Form1.blackContourMaps) { if (cmap.imageIndex == num) { blackOrWhite = false; goto black; } firstAppear++; } firstAppear=0; foreach (ColorfulContourMap cmap in Form1.whiteContourMaps) { if (cmap.imageIndex == num-Form1.blackSourceImages.Count) { blackOrWhite = true; break; } firstAppear++; } black: if (!blackOrWhite) // black { Mat img1 = Form1.blackSourceImages[num].Clone(); Mat img2 = Form1.blackSourceImages[num].Clone(); img2.SetTo(new MCvScalar(255,255,255)); //Form1.blackContourMaps[firstAppear + listBox1.SelectedIndex].DrawTo(img1); //Form1.blackContourMaps[firstAppear + listBox1.SelectedIndex].DrawColorTo(img2); { MatImage m1 = new MatImage(img1); m1.ResizeTo(pictureBox1.Width,pictureBox1.Height); img1 = m1.Out(); } pictureBox1.Image = img1.Bitmap; { MatImage m2 = new MatImage(img2); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); img2 = m2.Out(); } pictureBox2.Image = img2.Bitmap; } else // white { Mat img1 = Form1.whiteSourceImages[num - Form1.blackSourceImages.Count].Clone(); Mat img2 = Form1.whiteSourceImages[num - Form1.blackSourceImages.Count].Clone(); img2.SetTo(new MCvScalar(0)); //Form1.whiteContourMaps[firstAppear + listBox1.SelectedIndex].DrawTo(img1); //Form1.whiteContourMaps[firstAppear + listBox1.SelectedIndex].DrawColorTo(img2); { MatImage m1 = new MatImage(img1); m1.ResizeTo(pictureBox1.Width, pictureBox1.Height); img1 = m1.Out(); } pictureBox1.Image = img1.Bitmap; { MatImage m2 = new MatImage(img2); m2.ResizeTo(pictureBox2.Width, pictureBox2.Height); img2 = m2.Out(); } pictureBox2.Image = img2.Bitmap; } }
private void button7_Click(object sender, EventArgs e) { MatImage m1 = new MatImage(mimg); m1.Convert(); mimgInGray = m1.Out(); MatImage m2 = new MatImage(mimgInGray); m2.SmoothGaussian(3); m2.ThresholdBinaryInv(245, 255); // this causes an exception, and I don't know how to fix that yet. //The good news is that this really doesn't matter for small images //m2.MorphologyEx(2); mimgInGray = m2.Out(); MatImage m3 = new MatImage(mimgInGray); VectorOfVectorOfPoint contours = m3.FindContours(); List<ColorfulContourMap> cmaps = ColorfulContourMap.getAllContourMap(mimg, 0); //CvInvoke.DrawContours(mimg, contours,-1, new Bgr(255,0,0).MCvScalar,2); Mat mimg2 =new Mat(new Size(mimg.Width,mimg.Height),DepthType.Cv8U,3); foreach(ColorfulContourMap cmap in cmaps) { cmap.DrawColorTo(mimg2); } pictureBox1.Image = mimg.Bitmap; pictureBox2.Image = mimg2.Bitmap; }
//Transform images according to transform matrix // From Line 130-239 public static ReturnColorImg transformColor(Mat img1, Mat mask1, Mat img2, Mat mask2, Mat dst, Mat dst_mask, Point centroid1, Point centroid2, double angle, Point tweak1, Point tweak2, bool mode = true) { Mat E = img2.Clone(); Mat E_mask = mask2.Clone();//Don't ruin original images double intersections = 0; double x = centroid2.X; double y = centroid2.Y; double _x, _y, _y2; double y2; LineSegment2D centerLine = new LineSegment2D(new Point((int)x, (int)y), new Point(img2.Width - (int)x, img2.Height - (int)y)); //Rectangle r=new Rectangle((int)x,(int)y,2*(img2.Width-(int)x),2*(img2.Height-(int)y)); Mat ri = new Mat(2 * (img2.Width - (int)x), 2 * (img2.Height - (int)y), DepthType.Cv8U, 3); ri.SetTo(new MCvScalar(255, 255, 255)); Point oldc = new Point((int)x, (int)y); bool success = false; // if the tweaking is not successful, return false // inverse y axis // y2 = -y; // rotation of centeroid x -= img2.Width / 2; y -= img2.Height / 2; //shift origin to (w/2,h/2) _x = x * Math.Cos(angle / (180 / Math.PI)) - y * Math.Sin(angle / (180 / Math.PI)); //rotate by theta _y = x * Math.Sin(angle / (180 / Math.PI)) + y * Math.Cos(angle / (180 / Math.PI)); _x += img2.Width / 2; _y += img2.Height / 2;//back to origin //_x = x+img2.Width/2; //_y = y+img2.Height/2; // inverse y axis //_y = -_y2; centroid2.X = (int)_x; centroid2.Y = (int)_y; Point shift = new Point(); shift.X = centroid1.X - centroid2.X; shift.Y = centroid1.Y - centroid2.Y; MatImage m1 = new MatImage(E); m1.Rotate(angle, new Bgr(255, 255, 255)); E = m1.Out(); MatImage m2 = new MatImage(E_mask); m1.Rotate(angle, new Bgr(255, 255, 255)); E_mask = m2.Out(); //Find optimal size of canvas to hold both images and appropriate transformations Point t1, t2;//transformation 1 and 2 t1 = new Point(); t2 = new Point(); int optimal_h = 0, optimal_w = 0;//of canvas(IplImage* dst) switch (quadrant(shift)) { case 1: t1.X = 0; t1.Y = 0; t2 = shift; optimal_h = Math.Max(img1.Height, img2.Height + shift.Y); optimal_w = Math.Max(img1.Width, img2.Width + shift.X); break; case 2: t1.X = -shift.X; t1.Y = 0; t2.X = 0; t2.Y = shift.Y; optimal_h = Math.Max(img1.Height, img2.Height + shift.Y); optimal_w = Math.Max(img2.Width, img1.Width - shift.X); break; case 3: t1.X = -shift.X; t1.Y = -shift.Y; t2.X = 0; t2.Y = 0; optimal_h = Math.Max(img1.Height - shift.Y, img2.Height); optimal_w = Math.Max(img1.Width - shift.X, img2.Width); break; case 4: t1.X = 0; t1.Y = -shift.Y; t2.X = shift.X; t2.Y = 0; optimal_h = Math.Max(img1.Height - shift.Y, img2.Height); optimal_w = Math.Max(img2.Width + shift.X, img1.Width); break; } // add tweak factor t1.X += tweak1.X; t1.Y += tweak1.Y; t2.X += tweak2.X; t2.Y += tweak2.Y; //optimal_h = 1000; //optimal_w = 1000; dst = new Mat(optimal_h, optimal_w, DepthType.Cv8U, 3); dst_mask = new Mat(optimal_h, optimal_w, DepthType.Cv8U, 3); if (mode) { dst.SetTo(new MCvScalar(255, 255, 255)); // white background=255, black background=0 } else { dst.SetTo(new MCvScalar(0, 0, 0)); // white background=255, black background=0 } dst_mask.SetTo(new MCvScalar(0, 0, 0)); /*if (BKG_WHITE) * cvSet(dst, cvScalar(255));//make it white * else * cvSet(dst, cvScalar(0));//make it black*/ for (int i = 0; i < img1.Height; ++i) { for (int j = 0; j < img1.Width; ++j) { // if black background if (mode) { if (mask1.GetData(i, j)[0] != 255) { int i_new = i + t1.Y; int j_new = j + t1.X; try { dst.SetValue(i_new, j_new, img1.GetData(i, j)); int[] vals = { 255, 255, 255 }; dst_mask.SetValue(i_new, j_new, vals); } catch (IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } // if white background else { if (mask1.GetData(i, j) [0] != 0) { int i_new = i + t1.Y; int j_new = j + t1.X; try { dst.SetValue(i_new, j_new, img1.GetData(i, j)); int[] vals = { 0, 0, 0 }; dst_mask.SetValue(i_new, j_new, vals); } catch (IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } } } //Apply transformation to image2 for (int i = 0; i < img2.Height; ++i) { for (int j = 0; j < img2.Width; ++j) { // if black background if (mode) { if (E_mask.GetData(i, j) [0] != 255) { int i_new = i + t2.Y; int j_new = j + t2.X; try { if (dst_mask.GetData(i_new, j_new)[0] != 0) { intersections++; } else { dst.SetValue(i_new, j_new, E.GetData(i, j)); int[] vals = { 255, 255, 255 }; dst_mask.SetValue(i_new, j_new, vals); } } catch (IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } // else if white background else { if (E_mask.GetData(i, j)[0] != 0) { int i_new = i + t2.Y; int j_new = j + t2.X; try { if (dst_mask.GetData(i_new, j_new)[0] != 0) { intersections++; } else { dst.SetValue(i_new, j_new, E.GetData(i, j)); int[] vals = { 0, 0, 0 }; dst_mask.SetValue(i_new, j_new, vals); } } catch (IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } } } /*for (int i = 0; i < Adst.Length; i++) * { * for(int j=0; j < Adst[0].Length; j++) * { * dst.SetValue(i, j, Adst[i][j]); * } * } * for (int i = 0; i < Adst_mask.Length; i++) * { * for (int j = 0; j < Adst_mask[0].Length; j++) * { * dst_mask.SetValue(i, j, Adst_mask[i][j]); * } * }*/ // dst.SetTo(Adst); //dst_mask.SetTo(Adst_mask); success = true; /*cvReleaseImage(&E); * cvReleaseImage(&E_mask);*/// should not need these two lines because of garbage collection // threshold detection is meaningless for 2-piece case, always success ret: if (intersections > Constants.THRESHOLD) { /*cvReleaseImage(&dst);//In case of failure in joining * cvReleaseImage(&dst_mask);//release memory*/ ReturnColorImg img = new ReturnColorImg(); img.img = dst; img.img_mask = dst_mask; img.source1 = img1; img.source2 = E_mask; img.center1 = centroid1; img.center2old = oldc; img.center2new = centroid2; img.centerLinee = centerLine; img.returnbool = false; // for determining if the image is matched or not img.translate1 = t1; img.translate2 = t2; img.overlap = intersections; img.success = success; // for tweak only return(img); } else { ReturnColorImg img = new ReturnColorImg(); img.img = dst; img.img_mask = dst_mask; img.source1 = img1; img.source2 = E_mask; img.center1 = centroid1; img.center2old = oldc; img.center2new = centroid2; img.centerLinee = centerLine; img.returnbool = true; // for determining if the image is matched or not img.translate1 = t1; img.translate2 = t2; img.overlap = intersections; img.success = success; // for tweak only return(img); } }
private Mat generateThumbnail(Mat input) { MatImage m1 = new MatImage(input); m1.ResizeTo(150, 150); return m1.Out(); }
private void button4_Click(object sender, EventArgs e) { long start = DateTime.Now.ToFileTime() * 10 / 1000000; for (int i = 0; i < 1000; i++) { MatImage m1 = new MatImage(mimg); m1.Translate(-1 + 2*i % 2, -1 + 2*i % 2); // don't let the picture move out of the box // engineering code Mat mim = m1.Out(); pictureBox2.Image = mim.Bitmap; } Console.WriteLine(DateTime.Now.ToFileTime() * 10 / 1000000 - start); }
// get all of the valid contour maps, valid means circumfence > 200 px // this was not in their code, I added this feature, but I used their logic public static List<ColorfulContourMap> getAllContourMap(Mat input, int index, int mode = 0) { // use for all members List<ColorfulContourMap> result = new List<ColorfulContourMap>(); MatImage m1 = new MatImage(input); m1.Convert(); Mat gray = m1.Out(); // use for black background if (mode == 0) { MatImage m2 = new MatImage(gray); m2.SmoothGaussian(3); m2.ThresholdBinaryInv(245, 255); gray = m2.Out(); } // use for white background else { MatImage m2 = new MatImage(gray); m2.SmoothGaussian(3); m2.ThresholdBinaryInv(100, 255); gray = m2.Out(); } // one time use List<Point> pointList = new List<Point>(); List<Point> polyPointList = new List<Point>(); List<ColorfulPoint> cps = new List<ColorfulPoint>(); List<ColorfulPoint> pcps = new List<ColorfulPoint>(); // fetch all the contours using Emgu CV // fetch all the polys using Emgu CV // extract the points and colors Mat temp = gray.Clone(); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(gray, contours, new Mat(), RetrType.List, ChainApproxMethod.ChainApproxNone); double area = Math.Abs(CvInvoke.ContourArea(contours[0])); VectorOfPoint maxArea = contours[0]; // maxArea is used as the current contour //contour = contour.HNext; // use this to loop for (int i = 0; i < contours.Size; i++) { double nextArea = Math.Abs(CvInvoke.ContourArea(contours[i], false)); // Find the area of contour area = nextArea; if (area >= Constants.MIN_AREA) { maxArea = contours[i]; VectorOfPoint poly = new VectorOfPoint(); CvInvoke.ApproxPolyDP(maxArea, poly, 1.0, true); pointList = maxArea.ToArray().ToList(); polyPointList = poly.ToArray().ToList(); foreach (Point p in pointList) { ColorfulPoint cp = new ColorfulPoint { X = p.X, Y = p.Y, color = extractPointColor(p, input) }; cps.Add(cp); } foreach (Point p in polyPointList) { ColorfulPoint cp = new ColorfulPoint { X = p.X, Y = p.Y, color = extractPointColor(p, input) }; pcps.Add(cp); } result.Add(new ColorfulContourMap(cps, pcps, index)); // clear temporal lists pointList = new List<Point>(); polyPointList = new List<Point>(); cps = new List<ColorfulPoint>(); pcps = new List<ColorfulPoint>(); } } return result; }
//Transform images according to transform matrix // From Line 130-239 public static ReturnColorImg transformColor(Mat img1, Mat mask1, Mat img2, Mat mask2, Mat dst, Mat dst_mask, Point centroid1, Point centroid2, double angle, Point tweak1, Point tweak2, bool mode = true) { Mat E = img2.Clone(); Mat E_mask = mask2.Clone();//Don't ruin original images double intersections = 0; double x = centroid2.X; double y = centroid2.Y; double _x, _y, _y2; double y2; LineSegment2D centerLine = new LineSegment2D(new Point((int)x, (int)y), new Point(img2.Width - (int)x, img2.Height - (int)y)); //Rectangle r=new Rectangle((int)x,(int)y,2*(img2.Width-(int)x),2*(img2.Height-(int)y)); Mat ri = new Mat(2 * (img2.Width - (int)x), 2 * (img2.Height - (int)y),DepthType.Cv8U, 3); ri.SetTo(new MCvScalar(255, 255, 255)); Point oldc = new Point((int)x, (int)y); bool success = false; // if the tweaking is not successful, return false // inverse y axis // y2 = -y; // rotation of centeroid x -= img2.Width / 2; y -= img2.Height / 2;//shift origin to (w/2,h/2) _x = x * Math.Cos(angle / (180 / Math.PI)) - y * Math.Sin(angle / (180 / Math.PI));//rotate by theta _y = x * Math.Sin(angle / (180 / Math.PI)) + y * Math.Cos(angle / (180 / Math.PI)); _x += img2.Width / 2; _y += img2.Height / 2;//back to origin //_x = x+img2.Width/2; //_y = y+img2.Height/2; // inverse y axis //_y = -_y2; centroid2.X = (int)_x; centroid2.Y = (int)_y; Point shift = new Point(); shift.X = centroid1.X - centroid2.X; shift.Y = centroid1.Y - centroid2.Y; MatImage m1 = new MatImage(E); m1.Rotate(angle, new Bgr(255, 255, 255)); E = m1.Out(); MatImage m2 = new MatImage(E_mask); m1.Rotate(angle, new Bgr(255, 255, 255)); E_mask = m2.Out(); //Find optimal size of canvas to hold both images and appropriate transformations Point t1, t2;//transformation 1 and 2 t1 = new Point(); t2 = new Point(); int optimal_h = 0, optimal_w = 0;//of canvas(IplImage* dst) switch (quadrant(shift)) { case 1: t1.X = 0; t1.Y = 0; t2 = shift; optimal_h = Math.Max(img1.Height, img2.Height + shift.Y); optimal_w = Math.Max(img1.Width, img2.Width + shift.X); break; case 2: t1.X = -shift.X; t1.Y = 0; t2.X = 0; t2.Y = shift.Y; optimal_h = Math.Max(img1.Height, img2.Height + shift.Y); optimal_w = Math.Max(img2.Width, img1.Width - shift.X); break; case 3: t1.X = -shift.X; t1.Y = -shift.Y; t2.X = 0; t2.Y = 0; optimal_h = Math.Max(img1.Height - shift.Y, img2.Height); optimal_w = Math.Max(img1.Width - shift.X, img2.Width); break; case 4: t1.X = 0; t1.Y = -shift.Y; t2.X = shift.X; t2.Y = 0; optimal_h = Math.Max(img1.Height - shift.Y, img2.Height); optimal_w = Math.Max(img2.Width + shift.X, img1.Width); break; } // add tweak factor t1.X += tweak1.X; t1.Y += tweak1.Y; t2.X += tweak2.X; t2.Y += tweak2.Y; //optimal_h = 1000; //optimal_w = 1000; dst = new Mat(optimal_h, optimal_w,DepthType.Cv8U,3); dst_mask = new Mat(optimal_h, optimal_w,DepthType.Cv8U,3); if (mode) { dst.SetTo(new MCvScalar(255, 255, 255)); // white background=255, black background=0 } else { dst.SetTo(new MCvScalar(0, 0, 0)); // white background=255, black background=0 } dst_mask.SetTo(new MCvScalar(0, 0, 0)); /*if (BKG_WHITE) cvSet(dst, cvScalar(255));//make it white else cvSet(dst, cvScalar(0));//make it black*/ for (int i = 0; i < img1.Height; ++i) { for (int j = 0; j < img1.Width; ++j) { // if black background if (mode) { if (mask1.GetData(i, j)[0] != 255) { int i_new = i + t1.Y; int j_new = j + t1.X; try { dst.SetValue(i_new, j_new, img1.GetData(i, j)); int[] vals = { 255, 255, 255 }; dst_mask.SetValue(i_new, j_new, vals); } catch(IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } // if white background else { if (mask1.GetData(i, j) [0] != 0) { int i_new = i + t1.Y; int j_new = j + t1.X; try { dst.SetValue(i_new, j_new, img1.GetData(i, j)); int[] vals = { 0, 0, 0 }; dst_mask.SetValue(i_new, j_new, vals); } catch(IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } } } //Apply transformation to image2 for (int i = 0; i < img2.Height; ++i) { for (int j = 0; j < img2.Width; ++j) { // if black background if (mode) { if (E_mask.GetData(i, j) [0] != 255) { int i_new = i + t2.Y; int j_new = j + t2.X; try { if (dst_mask.GetData(i_new,j_new)[0] != 0) { intersections++; } else { dst.SetValue(i_new, j_new, E.GetData(i, j)); int[] vals = { 255, 255, 255 }; dst_mask.SetValue(i_new, j_new, vals); } } catch (IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } // else if white background else { if (E_mask.GetData(i, j)[0] != 0) { int i_new = i + t2.Y; int j_new = j + t2.X; try { if (dst_mask.GetData(i_new, j_new)[0] != 0) { intersections++; } else { dst.SetValue(i_new, j_new, E.GetData(i, j)); int[] vals = { 0, 0, 0 }; dst_mask.SetValue(i_new, j_new, vals); } } catch (IndexOutOfRangeException e) { //MessageBox.Show("You cannot tweak in that direction further"); success = false; goto ret; } } } } } /*for (int i = 0; i < Adst.Length; i++) { for(int j=0; j < Adst[0].Length; j++) { dst.SetValue(i, j, Adst[i][j]); } } for (int i = 0; i < Adst_mask.Length; i++) { for (int j = 0; j < Adst_mask[0].Length; j++) { dst_mask.SetValue(i, j, Adst_mask[i][j]); } }*/ // dst.SetTo(Adst); //dst_mask.SetTo(Adst_mask); success = true; /*cvReleaseImage(&E); cvReleaseImage(&E_mask);*/ // should not need these two lines because of garbage collection // threshold detection is meaningless for 2-piece case, always success ret: if (intersections > Constants.THRESHOLD) { /*cvReleaseImage(&dst);//In case of failure in joining cvReleaseImage(&dst_mask);//release memory*/ ReturnColorImg img = new ReturnColorImg(); img.img = dst; img.img_mask = dst_mask; img.source1 = img1; img.source2 = E_mask; img.center1 = centroid1; img.center2old = oldc; img.center2new = centroid2; img.centerLinee = centerLine; img.returnbool = false; // for determining if the image is matched or not img.translate1 = t1; img.translate2 = t2; img.overlap = intersections; img.success = success; // for tweak only return img; } else { ReturnColorImg img = new ReturnColorImg(); img.img = dst; img.img_mask = dst_mask; img.source1 = img1; img.source2 = E_mask; img.center1 = centroid1; img.center2old = oldc; img.center2new = centroid2; img.centerLinee = centerLine; img.returnbool = true; // for determining if the image is matched or not img.translate1 = t1; img.translate2 = t2; img.overlap = intersections; img.success = success; // for tweak only return img; } }