OpenCv案例(十): 基于OpenCvSharp识别二维码

11 篇文章 5 订阅
10 篇文章 6 订阅

1:二维码在工业和工作生活中应用广泛,下面基于OpenCvSharp识别图像中二维码;

2:函数:OpenCvSharp中,QRCodeDetector 有两个相关API分别实现二维码检测与二维码解析。

          public string DetectAndDecode(InputArray img, out Point2f[] points, OutputArray straightQrcode = null);

其中:

        //
        //  摘要:
        //     Both detects and decodes QR code
        //
        //  参数:
        //   img:
        //     grayscale or color (BGR) image containing QR code.
        //
        //   points:
        //     opiotnal output array of vertices of the found QR code quadrangle. Will be empty
        //     if not found.
        //
        //   straightQrcode:
        //     The optional output image containing rectified and binarized QR code
       

  • img 为输入图像,灰度或者彩色图像;
  • points 是二维码ROI最小外接矩形顶点坐标;
  • straightQrcode 输出的是二维码区域ROI图像信息 返回的二维码utf-8字符串;
  • 返回值是已识别的二维码内容
        public string DetectQRCode(Mat src)
        {
            //src = Cv2.ImRead(@"C:\Users\1617\Desktop\Test\qr.png");
            //图像灰度
            Mat gray = new Mat();
            Cv2.CvtColor(src, gray, ColorConversionCodes.BGRA2GRAY);
            Cv2.ImShow("gray", gray);
            //二值化
            Mat threshold = new Mat();
            //Cv2.AdaptiveThreshold(gray, threshold, 255.0, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 13, 2);
            Cv2.Threshold(gray, threshold, 0, 255, ThresholdTypes.Binary|ThresholdTypes.Otsu);
            Cv2.ImShow("threshold", threshold);
            //绘制轮廓

            //截取二维码有效区域
            //识别二维码
            QRCodeDetector qRCodeDetector = new QRCodeDetector();
            Point2f[] point2Fs;
            //Cv2.ImShow("gray", gray);
            Mat mat = new Mat();
            string code = qRCodeDetector.DetectAndDecode(src, out point2Fs, mat);
            //Point[][] contours;
            //HierarchyIndex[] hierarchies;
            //Cv2.FindContours(src, out contours, out hierarchies, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
            //RNG rng = new RNG(12345);
            //for (int i = 0; i < point2Fs.Length; i++)
            //{
            //  Cv2.DrawContours(src, point2Fs, i, new Scalar(rng.Uniform(0, 255), rng.Uniform(0, 255), rng.Uniform(0, 255)), 6, LineTypes.Link4);

            //}
            Cv2.Rectangle(src, new Point((int)point2Fs[3].X, (int)point2Fs[1].Y), new Point((int)point2Fs[2].X, (int)point2Fs[2].Y), new Scalar(0, 255, 255), 2);
            Cv2.ImShow("src", src);
            return code;
        }

3:实测以下两张图片,一张手机拍摄的二维码图像,一张截图

 4:处理结果图像如下:

 内容:1234567

 内容:http://weixin.qq.com/r/GihgeEPEix70rQcb930I

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCvSharp 是一个基于 OpenCV 的开源计算机视觉库,它提供了用于图像处理和计算机视觉任务的丰富功能和接口。在使用 OpenCvSharp 识别二维码时,可以遵循以下步骤: 1. 导入需要的命名空间:使用 OpenCvSharp 进行二维码识别,需要导入 OpenCvSharp 的相关命名空间,例如 `using OpenCvSharp;`。 2. 读取图像:使用 `imread` 函数加载待识别的图像,将其转换为 OpenCvSharp 中的 Mat 格式。例如 `Mat image = Cv2.ImRead("image.jpg");`,其中 `"image.jpg"` 是待读取的图像路径。 3. 创建二维码检测器:使用 `QRCodeDetector` 类创建二维码检测器对象。例如 `QRCodeDetector detector = new QRCodeDetector();`。 4. 检测二维码:使用 `detectAndDecode` 方法检测并解码二维码。其中 `detectAndDecode` 方法会返回一个包含检测到的二维码信息的数组。例如 `string[] qrCodes = detector.DetectAndDecode(image);`。 5. 处理识别结果:使用得到的识别结果数组 `qrCodes` 进行进一步处理和分析。根据具体需求,可能需要对识别结果进行判断或者提取二维码中的信息。 通过上述步骤,我们可以使用 OpenCvSharp 实现二维码识别。需要注意的是,为了成功识别二维码,图像中的二维码应该具有足够的清晰度、对比度和正确的角度。此外,OpenCvSharp 还提供了其他图像处理和计算机视觉的功能,可以根据需要进行进一步的开发和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值