[c++]根据二维码矩形特征定位识别位置 您所在的位置:网站首页 放大的二维码 [c++]根据二维码矩形特征定位识别位置

[c++]根据二维码矩形特征定位识别位置

2023-03-10 10:29| 来源: 网络整理| 查看: 265

//// Created by leoxae on 19-8-28.//

#include "checkQrcode.h"

float checkQrcode::getDistance(Point pointO, Point pointA) { float distance; distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2); distance = sqrtf(distance);

return distance;}

void checkQrcode::check_center(vector > c, vector &index) { float dmin1 = 10000; float dmin2 = 10000; for (int i = 0; i < c.size(); ++i) { RotatedRect rect_i = minAreaRect(c[i]); for (int j = i + 1; j < c.size(); ++j) { RotatedRect rect_j = minAreaRect(c[j]); float d = getDistance(rect_i.center, rect_j.center); if (d < dmin2 && d > 10) { if (d < dmin1 && d > 10) { dmin2 = dmin1; dmin1 = d; index[2] = index[0]; index[3] = index[1]; index[0] = i; index[1] = j;

} else { dmin2 = d; index[2] = i; index[3] = j; } } } }}

Rect checkQrcode::processData(const Mat gray, Rect resultRect) { Rect RoiRect;// Mat gray(h, w, CV_8UC4, data);// imwrite("/storage/emulated/0/scan/src.jpg", gray);

// Mat filter;// bilateralFilter(gray, filter, 15, 150, 15, 4);// imwrite("/storage/emulated/0/scan/filter.jpg", filter);

int w = gray.cols; int h = gray.rows;

// 进行canny化,变成黑白线条构成的图片 Mat binary; Canny(gray, binary, 100, 255, 3);// imwrite("/storage/emulated/0/scan/src_canny.jpg", binary); // detect rectangle now vector> contours; vector hierarchy; vector found; vector> found_contours; findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);// Mat result = Mat::zeros(gray.size(), CV_8UC4); for (int t = 0; t < contours.size(); ++t) { double area = contourArea(contours[t]); if (area < 150) continue;

RotatedRect rect = minAreaRect(contours[t]); // 根据矩形特征进行几何分析 float rect_w = rect.size.width; float rect_h = rect.size.height; float rate = min(rect_w, rect_h) / max(rect_w, rect_h); if (rate > 0.65 && rect_w < (gray.cols >> 2) && rect_h < (gray.rows >> 2)) { int k = t; int c = 0; while (hierarchy[k][2] != -1) { k = hierarchy[k][2]; c = c + 1; } if (c >= 1) { found.push_back(t); found_contours.push_back(contours[t]);// drawContours(result, contours, static_cast(t), Scalar(255, 0, 0), 2, 8); } } }

// imwrite("/storage/emulated/0/scan/src_patter_1.jpg", result);

if (found.size() >= 3) { vector indexs(4, -1); check_center(found_contours, indexs); vector final; for (int i = 0; i < 4; ++i) { if (indexs[i] == -1) { continue; } RotatedRect part_rect = minAreaRect(found_contours[indexs[i]]); Point2f p[4]; part_rect.points(p); for (auto &j : p) { final.push_back(j); } }

//region of qr Rect ROI = boundingRect(final);// if (ROI.empty()) {// return;// } int space = 0; if (ROI.width < ROI.height) { space = ROI.height - ROI.width; ROI = ROI + Size(space, 0); } else if (ROI.width > ROI.height) { space = ROI.width - ROI.height; ROI = ROI + Size(0, space); }

Point left_top = ROI.tl(); Point right_down = ROI.br(); if (left_top.x >= 20 || left_top.y >= 20 || right_down.x 0 && ROI.br().x < w && ROI.br().y < h) {// rectangle(result, ROI.tl(), ROI.br(), Scalar(0, 0, 255));// imwrite("/storage/emulated/0/scan/src_patter_2.jpg", result); RoiRect = ROI; } } return RoiRect;}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有