Java调用OpenCV进行Hough变换直线检测

22 篇文章 0 订阅
8 篇文章 0 订阅

   private BitmapHoughTransFormLine(Bitmap bmp)

   {

    Mat rgbMat = new Mat();  //存储原图像的矩阵

    Mat grayMat = new Mat(); //存储灰度图像的矩阵

        Mat lineMat = new Mat(); //存储检测出的直线坐标的矩阵,每个element4个通道,第12个通道为直线的1个端点,第34个通道为直线的另1个端点

       

        Utils.bitmapToMat(bmp, rgbMat);

        Imgproc.cvtColor(rgbMat,grayMat, Imgproc.COLOR_RGB2GRAY); //灰度化

        Imgproc.Canny(grayMat, grayMat,50, 200);                  //Canny边缘检测

       

        /* Hough变换标记直线

         * public static void HoughLinesP(Matimage, Mat lines, double rho, double theta, int threshold)

         *    image - 8-bit, single-channel binary source image. The image may bemodified by the function.

         *    lines - Output vector of lines. Each line is represented by a 4-elementvector (x_1, y_1, x_2, y_2),

         *                where (x_1,y_1) and (x_2, y_2) arethe ending points of each detected line segment.

         *    rho - Distance resolution of the accumulator in pixels.

        *     theta - Angle resolution of theaccumulator in radians.

         *    threshold - Accumulator threshold parameter. Only those lines arereturned that get enough votes (>threshold).

         */

        Imgproc.HoughLinesP(grayMat,lineMat, 1, Math.PI/180,50, 50.0, 10.0);

       

        int[] a = new int[(int)lineMat.total()*lineMat.channels()]; //数组a存储检测出的直线端点坐标

        lineMat.get(0,0,a);

   

        for (int i = 0; i < a.length; i += 4)

        {

           Core.line(grayMat, new Point(a[i], a[i+1]), new Point(a[i+2], a[i+3]), new Scalar(255, 0, 255),4);

        }

 

        //创建一个图像

        Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), Config.RGB_565);

        //将矩阵grayMat转换为图像 

        Utils.matToBitmap(grayMat,newBmp);

       

        rgbMat = null;

        grayMat = null;

        lineMat = null;

        a = null;

       

        return newBmp;

   }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值