private CvLineSegmentPoint[] detectLinesFromCanny(CvMat roi) { CvMat edgesMat = MatOps.CopySize(roi, MatrixType.U8C1); roi.Canny(edgesMat, 10, 200, ApertureSize.Size3); // Size5 also works good; 7 not; rest crash! // these values work fine with "box7.png" double rho = 1; // 1 double theta = 1 * Cv.PI / 180; // 1*Cv.PI/180 int threshold = 75; // 75 (quality) double minLength = 1; // 1 double maxGap = 10000; // 1000000, but not Infinity, for some dumb reason CvLineSegmentPoint[] lines = edgesMat.HoughLinesProbabilistic(rho, theta, threshold, minLength, maxGap); CvMat linesMat = MatOps.CopySize(edgesMat, MatrixType.U8C3, 0); for (int i = 0; i < lines.Length; ++i) { linesMat.Line(lines[i].P1, lines[i].P2, Const.ScalarRandom(), 3, LineType.AntiAlias); } //MatOps.NewWindowShow( edgesMat, "edgesMat Canny-Hough" ); MatOps.NewWindowShow(linesMat, "linesMat"); Console.WriteLine("===================="); Console.WriteLine("detectLinesFromCanny"); Console.WriteLine("lines=" + lines.Length); Console.WriteLine("===================="); return(lines); }
private CvLineSegmentPoint[] detectLinesFromFeatures(CvMat hue, CvMat roi) { // IDEA 3 : // Extract features (actual box corners?!) from ROI with corner detection CvPoint2D32f[] corners; // extracted features int cornerCount; // not exactly "count", but rather "maximum number of corners to return" double qualityLevel = 0.05; // this changes to 0.1 if NOT using ROI as mask! double minimumDistance = 25; // maybe this has to be a percentage of the input-size, rather than an absolute value?!?!? bool useHarris = false; int blockSize = 3; // NOTE : roi is not as good to check for features as the hue itself!!! #if false cornerCount = 100; Cv.GoodFeaturesToTrack( roi, MatOps.CopySize(roi, MatrixType.F32C1, Const.ScalarBlack), MatOps.CopySize(roi, MatrixType.F32C1, Const.ScalarBlack), out corners, ref cornerCount, qualityLevel, minimumDistance, null, blockSize, useHarris); CvMat roiClone = roi.Clone(); roiClone.SaveImage("roiClone.png"); for (int i = 0; i < cornerCount; ++i) { // remove "isolated" features : gave back some good results, but it still wasn't as good as actual HUE feature discovery CvPoint2D32f feature = corners[i]; if (checkFeatureArea(roiClone, feature)) { roiClone.Circle(feature, 10, 127); } } MatOps.NewWindowShow(roiClone, "ROI!"); Console.WriteLine("corners=" + cornerCount); #endif // TODO : determine if it's a good idea to use ROI as a mask. // NOTE : Abandoning this idea for now. Good features are truly found, but they give worse lines than [IDEA 4]! cornerCount = 100; Cv.GoodFeaturesToTrack( hue, MatOps.CopySize(roi, MatrixType.F32C1, Const.ScalarBlack), MatOps.CopySize(roi, MatrixType.F32C1, Const.ScalarBlack), out corners, ref cornerCount, qualityLevel, minimumDistance, roi, blockSize, useHarris); //CvMat hueClone = hue.Clone(); CvMat hueClone = MatOps.CopySize(hue, MatrixType.U8C1, 0); for (int i = 0; i < cornerCount; ++i) { hueClone.Circle(corners[i], 10, 127, -1); } CvLineSegmentPoint[] lines2 = hueClone.HoughLinesProbabilistic(1, 1 * Cv.PI / 180, 75, 1, 10000); for (int i = 0; i < lines2.Length; ++i) { hueClone.Line(lines2[i].P1, lines2[i].P2, Const.ScalarRandom(), 3, LineType.AntiAlias); } MatOps.NewWindowShow(hueClone, "Lines from Features"); Console.WriteLine("======================="); Console.WriteLine("detectLinesFromFeatures"); Console.WriteLine("corners=" + cornerCount); Console.WriteLine("lines=" + lines2.Length); Console.WriteLine("======================="); return(lines2); }