private Tuple <Bitmap, int> CreatePreviewImage(string imgPath, double val) { var q1 = new Image <Bgr, byte>(imgPath); var w3 = ExtensionMethods.FindContours (q1.Copy().Convert <Gray, byte>().GaussBlur().AdaptiveThreshold().Dilate(8).Erode()); var avg = ExtensionMethods.CalculateAvreage(w3.Item1, val); var e4 = new VectorOfVectorOfPoint(); for (var i = 0; i < w3.Item1.Size; i++) { if (CvInvoke.ContourArea(w3.Item1[i]) > avg) { e4.Push(w3.Item1[i]); } } var boundRect = new List <Rectangle>(); for (var i = 0; i < e4.Size; i++) { boundRect.Add(CvInvoke.BoundingRectangle(e4[i])); } var puzzelCounter = 0; var avgX = new int[boundRect.Count]; var avgY = new int[boundRect.Count]; foreach (var r in boundRect) { avgX[puzzelCounter] = r.X; avgY[puzzelCounter] = r.Y; puzzelCounter++; q1 = q1.Rectangle (r, new MCvScalar(250, 0, 250)) .PutText ( puzzelCounter.ToString() , new Point(r.X + r.Width / 2, r.Y + r.Height / 2) , new MCvScalar(255, 0, 255) , FontFace.HersheySimplex , 10 , 20); } var assumedConfiguration = ExtensionMethods.AssumePuzzleConfiguration(avgX, avgY); X_axis.Value = assumedConfiguration[1]; Y_axis.Value = assumedConfiguration[0]; return(new Tuple <Bitmap, int>(q1.ToBitmap(), puzzelCounter)); }