public void imageAvailableMain(string filename) { //Bitmap temp = new Bitmap(filename); //mainView.postImageMain(temp); ImageAnalysis tempAnalysis = mainAnalyser.analyse(filename); //Move to thread task? mainView.postImageMain(tempAnalysis.Result); currentGrain = new GrainAnalysis(); currentGrain.setMain(tempAnalysis); string output = ""; switch (currentState) { case State.SampleARecord: //runSampleA.add(tempAnalysis); break; case State.SampleBRecord: //runSampleB.add(tempAnalysis); break; case State.DataRecord: //currentRun.add(tempAnalysis); break; case State.Idle: break; } output += "Main| C=(" + (int)tempAnalysis.Center.X + "px," + (int)tempAnalysis.Center.Y + "px), "; output += "A=" + (int)tempAnalysis.Area + ", W=" + (int)tempAnalysis.Width + ", L=" + (int)tempAnalysis.Length + ", R=1:" + tempAnalysis.Ratio; mainView.displayData(output); }
public void imageAvailableSide(string filename) { //Bitmap temp = new Bitmap(filename); //mainView.postImageSide(temp); ImageAnalysis tempAnalysis2 = sideAnalyser.analyse(filename); mainView.postImageSide(tempAnalysis2.Result); string output = "Side| C=(" + (int)tempAnalysis2.Center.X + "px," + (int)tempAnalysis2.Center.Y + "px), "; output += "A=" + (int)tempAnalysis2.Area + ", W=" + (int)tempAnalysis2.Width + ", L=" + (int)tempAnalysis2.Length + ", R=1:" + tempAnalysis2.Ratio; mainView.displayData(output); if (currentGrain != null) { currentGrain.setSide(tempAnalysis2); currentGrain.analyse(); switch (currentState) { case State.SampleARecord: //runSampleA.add(tempAnalysis); runSampleA.add(currentGrain); break; case State.SampleBRecord: //runSampleB.add(tempAnalysis); runSampleB.add(currentGrain); break; case State.DataRecord: //currentRun.add(tempAnalysis); currentRun.add(currentGrain); int length = currentRun.totalLength(); mainView.displayLength(length); break; case State.Idle: break; } output = "Grain| L=" + currentGrain.Length + ", W=" + currentGrain.Width + ", D=" + currentGrain.Depth + ", V=" + currentGrain.Volume; mainView.displayData(output); if (sort) { if (rejectAboveThreshold) { if (currentGrain.Volume > threshold) { //reject mainView.displayData("Reject!"); System.Media.SystemSounds.Asterisk.Play(); } } else { if (currentGrain.Volume < threshold) { //reject mainView.displayData("Reject!"); System.Media.SystemSounds.Asterisk.Play(); } } } currentGrain = null; } }
public ImageAnalysis analyse(string fileName, int lLow, int lHigh, int aLow, int aHigh, int bLow, int bHigh) { ImageAnalysis analysis = new ImageAnalysis(); Mat tempMat = new Mat(fileName); // pixel mask, erode x2, dilate x2 //CvInvoke.GaussianBlur(mat, mat, new Size(5, 5), 1.5, 1.5); //CvInvoke.GaussianBlur(mat, mat, new Size(5, 5), 1.5, 1.5); //GetColorPixelMask(tempMat, tempMat, maskHueUpper, maskHueLower, maskSatUpper, maskSatLower, maskLumUpper, maskLumLower); GetLabColorPixelMask(tempMat, tempMat, lLow, lHigh, aLow, aHigh, bLow, bHigh); //tempMat.Save(fileName + "temp.jpg"); CvInvoke.Erode(tempMat, tempMat, null, new Point(-1, -1), 2, BorderType.Constant, CvInvoke.MorphologyDefaultBorderValue); Mat temp = new Mat(); CvInvoke.Dilate(tempMat, temp, null, new Point(-1, -1), 2, BorderType.Constant, CvInvoke.MorphologyDefaultBorderValue); tempMat = temp; //find largest contour Mat result = new Mat(540, 720, 0, 1); int largest_contour_index = 0; double largest_area = 0; VectorOfPoint largestContour; VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierachy = new Mat(); CvInvoke.FindContours(tempMat, contours, hierachy, RetrType.Tree, ChainApproxMethod.ChainApproxNone); if (contours.Size > 0) { for (int i = 0; i < contours.Size; i++) { MCvScalar color = new MCvScalar(0, 0, 255); double a = CvInvoke.ContourArea(contours[i], false); // Find the area of contour if (a > largest_area) { largest_area = a; largest_contour_index = i; //Store the index of largest contour } //CvInvoke.DrawContours(result, contours, largest_contour_index, new MCvScalar(255, 0, 0)); } //draw largest contour CvInvoke.DrawContours(result, contours, largest_contour_index, new MCvScalar(255, 255, 255), 1, LineType.EightConnected, hierachy); largestContour = new VectorOfPoint(contours[largest_contour_index].ToArray()); Image <Bgr, Byte> tempImg = result.ToImage <Bgr, Byte>(); //Find center point MCvMoments m = CvInvoke.Moments(largestContour, true); MCvPoint2D64f center = m.GravityCenter; //textBox1.AppendText("Center point: " + Math.Round(center.X, 3) + "px, " + Math.Round(center.Y, 3) + "px\n"); tempImg.Draw(new Cross2DF(new PointF((float)center.X, (float)center.Y), 3, 3), new Bgr(0, 0, 255), 2); //Find Area double area = CvInvoke.ContourArea(largestContour); //textBox1.AppendText("Area: " + area + "px, " + convertSqPxToSqMm(area) + "sq mm\n"); //Find Bounding Rectangle RotatedRect rect = CvInvoke.MinAreaRect(largestContour); float width0 = rect.Size.Width; float height0 = rect.Size.Height; float length = (height0 >= width0 ? height0 : width0); float width = (height0 < width0 ? height0 : width0); tempImg.Draw(rect, new Bgr(255, 0, 0), 2); //textBox1.AppendText("Width: " + width + "px Length: " + length + "px\n"); //textBox1.AppendText("Width: " + convertPxToMm(width) + "mm Length: " + convertPxToMm(length) + "mm\n"); double ratio = Math.Round((length / width), 3); //textBox1.AppendText("Ratio (width:length): 1:" + ratio + "\n"); //save and display tempImg.Save(fileName + "_after.bmp"); tempMat = tempImg.Mat; analysis.Contours = contours; analysis.LargestContourIndex = largest_contour_index; analysis.LargestContour = largestContour; analysis.Center = center; analysis.Area = area; analysis.BoundingBox = rect; analysis.Length = length; analysis.Width = width; analysis.Ratio = ratio; analysis.Result = tempImg.ToBitmap(); } return(analysis); }