OpenCV C ++Obj

OpenCV C ++Obj,第1张

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 >



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4891643.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存