//============================================================= // // 確率的Hough変換処理 // //============================================================= private void HoughPbl(PictureBox pbox, IplImage image) { IplImage gray; IplImage canny; IplImage hPbl; gray = Cv.CreateImage(image.Size, BitDepth.U8, 1); canny = Cv.CreateImage(image.Size, BitDepth.U8, 1); hPbl = Cv.CreateImage(image.Size, BitDepth.U8, 3); Cv.CvtColor(image, gray, ColorConversion.RgbToGray); Cv.Canny(gray, canny, 50, 200); Cv.CvtColor(canny, hPbl, ColorConversion.GrayToRgb); CvMemStorage storage = new CvMemStorage(); CvSeq lines = Cv.HoughLines2(canny, storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, 50, 10, 10); for (int i = 0; i < lines.Total; i++) { CvLineSegmentPoint elem = lines.GetSeqElem <CvLineSegmentPoint>(i).Value; Cv.Line(hPbl, elem.P1, elem.P2, CvColor.Red, 1, LineType.AntiAlias, 0); } lines.Dispose(); storage.Dispose(); ViewBitmap(pbox, hPbl); Cv.ReleaseImage(gray); Cv.ReleaseImage(canny); Cv.ReleaseImage(hPbl); pictureBox2.Invalidate(); }
//============================================================= // // 古典的Hough変換処理 // //============================================================= private void HoughStd(PictureBox pbox, IplImage image) { IplImage gray; IplImage canny; IplImage hstd; gray = Cv.CreateImage(image.Size, BitDepth.U8, 1); canny = Cv.CreateImage(image.Size, BitDepth.U8, 1); hstd = Cv.CreateImage(image.Size, BitDepth.U8, 3); Cv.CvtColor(image, gray, ColorConversion.RgbToGray); Cv.Canny(gray, canny, 50, 200); Cv.CvtColor(canny, hstd, ColorConversion.GrayToRgb); CvMemStorage storage = new CvMemStorage(); CvSeq lines = Cv.HoughLines2(canny, storage, HoughLinesMethod.Standard, 1, Math.PI / 180, 120); for (int i = 0; i < lines.Total; i++) { CvLineSegmentPolar elem = lines.GetSeqElem <CvLineSegmentPolar>(i).Value; float rho = elem.Rho; float theta = elem.Theta; double a = Math.Cos(theta); double b = Math.Sin(theta); double x0 = a * rho; double y0 = b * rho; CvPoint pt1 = new CvPoint(Cv.Round(x0 + 10000 * (-b)), Cv.Round(y0 + 10000 * (a))); CvPoint pt2 = new CvPoint(Cv.Round(x0 - 10000 * (-b)), Cv.Round(y0 - 10000 * (a))); Cv.Line(hstd, pt1, pt2, CvColor.Red, 1, LineType.AntiAlias, 0); } lines.Dispose(); storage.Dispose(); ViewBitmap(pbox, hstd); Cv.ReleaseImage(gray); Cv.ReleaseImage(canny); Cv.ReleaseImage(hstd); pictureBox2.Invalidate(); }
static IplImage GetCountours(IplImage src) { CvMemStorage mem = Cv.CreateMemStorage(0); CvSeq <CvPoint> firstContour = null; IplImage dst = GetThresholdImage(src); dst = GetThresholdImage(dst); int count = dst.FindContours(mem, out firstContour); //src.DrawContours(firstContour, CvColor.Green, CvColor.Blue, 2); //src.DrawRect(firstContour.BoundingRect(), CvColor.Red); src = src.GetSubImage(firstContour.BoundingRect()); mem.Dispose(); firstContour.Dispose(); dst.Dispose(); return(src); }
public static CvAvgComp[] ToArrayAndDispose(this CvSeq <CvAvgComp> seq) { CvAvgComp[] arr = seq.ToArray(); seq.Dispose(); return(arr); }
//private CvSeq houghLines; //public IplImage srcImgGray = new IplImage(); //public IplImage srcImgStd = new IplImage(); //public CvMemStorage storage = new CvMemStorage(); public List <CvPoint> HoughLines_Point(IplImage src, int edge1, int edge2, int line1, int line2, int line3) { try { // 확률적 허프 변환을 지정해 선분의 검출을 실시한다 List <CvPoint> LinePoints = new List <CvPoint>(); IplImage srcImgStd = new IplImage(src.ROI.Size, BitDepth.U8, 3); Cv.Copy(src, srcImgStd); IplImage srcImgGray = new IplImage(src.ROI.Size, BitDepth.U8, 1); Cv.CvtColor(srcImgStd, srcImgGray, ColorConversion.BgrToGray); Cv.Canny(srcImgGray, srcImgGray, edge1, edge2, ApertureSize.Size3); CvMemStorage storage = new CvMemStorage(); CvSeq houghLines = srcImgGray.HoughLines2(storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, line1, line2, line3); //CvWindow.ShowImages(src); //Hough_Lines_Point LinesFinder = new Hough_Lines_Point(); //LinesFinder.srcImgGray = new IplImage(src.Size, BitDepth.U8, 1); //Cv.CvtColor(src, LinesFinder.srcImgGray, ColorConversion.BgrToGray); //Cv.Canny(LinesFinder.srcImgGray, LinesFinder.srcImgGray, edge1, edge2, ApertureSize.Size3); //CvMemStorage storage = new CvMemStorage(); //CvSeq houghLines = LinesFinder.srcImgGray.HoughLines2(storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, line1, line2, line3); //CvWindow.ShowImages(LinesFinder.srcImgGray); //bool tmpFalg = true; // for(int i = 1; i < 256;i++) // { // if (houghLines.Total >= 10) // { // break; // } // houghLines = LinesFinder.srcImgGray.HoughLines2(LinesFinder.storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, 2, line2, line3); // } //LinePoints = new List<CvPoint>(); LinePoints.Clear(); int limit = Math.Min(houghLines.Total, 10); for (int i = 0; i < limit; i++) { CvLineSegmentPolar elem = houghLines.GetSeqElem <CvLineSegmentPolar>(i).Value; CvPoint pt1 = houghLines.GetSeqElem <CvLineSegmentPoint>(i).Value.P1; CvPoint pt2 = houghLines.GetSeqElem <CvLineSegmentPoint>(i).Value.P2; //Trace.WriteLine(pt1.X.ToString("000.00000 ") + pt1.Y.ToString("000.00000 ") + pt2.X.ToString("000.00000 ")+ pt2.Y.ToString("000.00000")); //LinesFinder.srcImgStd.Line(LinesFinder.pt1, LinesFinder.pt2, CvColor.LawnGreen, 3, LineType.AntiAlias, 0); //TestImage = LinesFinder.srcImgStd.Clone(); //src.Line(pt1, pt2, CvColor.LawnGreen, 3, LineType.AntiAlias, 0); //CvWindow.ShowImages(src); //TestImage = src.Clone(); LinePoints.Add(pt1); LinePoints.Add(pt2); } srcImgStd.Dispose(); srcImgGray.Dispose(); houghLines.Dispose(); storage.Dispose(); return(LinePoints); } catch (Exception e) { MessageBox.Show(MethodBase.GetCurrentMethod().Name + " " + e.Message); throw; } }