栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

OpenCV C ++ / Obj

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV C ++ / Obj

这是反复出现的主题,由于我找不到相关的实现,因此决定接受挑战。

我对OpenCV中存在的squares演示进行了一些修改,下面生成的C ++代码能够检测图像中的纸:

void find_squares(Mat& image, vector<vector<Point> >& squares){    // blur will enhance edge detection    Mat blurred(image);    medianBlur(image, blurred, 9);    Mat gray0(blurred.size(), CV_8U), gray;    vector<vector<Point> > contours;    // find squares in every color plane of the image    for (int c = 0; c < 3; c++)    {        int ch[] = {c, 0};        mixChannels(&blurred, 1, &gray0, 1, ch, 1);        // try several threshold levels        const int threshold_level = 2;        for (int l = 0; l < threshold_level; l++)        { // Use Canny instead of zero threshold level! // Canny helps to catch squares with gradient shading if (l == 0) {     Canny(gray0, gray, 10, 20, 3); //      // Dilate helps to remove potential holes between edge segments     dilate(gray, gray, Mat(), Point(-1,-1)); } else {         gray = gray0 >= (l+1) * 255 / threshold_level; } // Find contours and store them in a list findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); // Test contours vector<Point> approx; for (size_t i = 0; i < contours.size(); i++) {         // approximate contour with accuracy proportional         // to the contour perimeter         approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);         // Note: absolute value of an area is used because         // area may be positive or negative - in accordance with the         // contour orientation         if (approx.size() == 4 &&      fabs(contourArea(Mat(approx))) > 1000 &&      isContourConvex(Mat(approx)))         {      double maxCosine = 0;      for (int j = 2; j < 5; j++)      {   double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));   maxCosine = MAX(maxCosine, cosine);      }      if (maxCosine < 0.3)   squares.push_back(approx);         } }        }    }}

执行此过程后,纸页将成为以下位置中最大的正方形vector >



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号