OpenCV+Java 调用:Imgproc.findContours 报错 FindContours supports only CV_8UC1XXX

OpenCV+Java 调用:Imgproc.findContours方法时报错:
Java FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL oth

代码:

//读取图片
        Mat dst = src.clone();
        Mat dstx = src.clone();
        Mat dsty = src.clone();
        //对图像进行高斯模糊去噪,梯度计算对噪声很敏感;
        Imgproc.GaussianBlur(src, dst, new Size(5, 5), 1.0);
        //调用Sobel函数计算图像在x,y方向梯度
        Imgproc.Sobel(dst, dstx, -1, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
        Imgproc.Sobel(dst, dsty, -1, 0, 1, 3, 1, 0, Core.BORDER_DEFAULT);
        Core.addWeighted(dstx, 0.5, dsty, 0.5, 0, dst);
//       调用convertScaleAbs函数将x,y梯度图像像素值限制在0-255;src – 原图 dst – 目标图 alpha – 乘数因子/beta – 偏移量
        Core.convertScaleAbs(dstx,dstx, 1, 0);
        Core.convertScaleAbs(dsty,dsty, 1, 0);
//        调用addWeight函数将x,y梯度图像融合
        Mat mEdge=new Mat();
        Core.addWeighted(dstx, 1, dsty, 1, 0, mEdge);
//        调用threshold函数对融合图像进行二值化;
        Mat mThresh=new Mat();
        Imgproc.threshold(mEdge, mThresh, 0, 255,  Imgproc.THRESH_BINARY );
//        调用findContours方法查找脏污区域轮廓。
        List<MatOfPoint> contours = new ArrayList<>();
        Mat hierarchy = new Mat();
        //Test.showImg(mThresh,"erzhi",3);
        Imgproc.findContours(mThresh, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(-1, -1));

解决方法:加载读取图片以后,加一句代码
Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
在图片的第一步将Mat转换为灰图即可
代码如下`

//读取图片
        Mat dst = src.clone();
        Mat dstx = src.clone();
        Mat dsty = src.clone();
        //1.彩色转灰色
        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
        //对图像进行高斯模糊去噪,梯度计算对噪声很敏感;
        Imgproc.GaussianBlur(src, dst, new Size(5, 5), 1.0);

到此,完美解决!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值