c++ - 如何使用 OpenCV 和 C++ 通过限制矩形大小来过滤轮廓?

标签 c++ opencv

我尝试使用 OpenCV 和 C++ 从图像中检测车牌。我可以找到车牌的轮廓。但我只想丢掉车牌。我有一个想法通过限制矩形大小来过滤轮廓。

这是我的代码:

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv\ml.h>
#include <opencv\cxcore.h>

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace cv;
using namespace std;
cv::Mat _img;       
cv::Mat _imgGray;       
int main()
{
    _img = cv::imread("bs9.jpg");           
    if (_img.empty()) {                                 
    std::cout << "error: image not read from file\n\n";     
    return(0);                                              
}
cv::Mat src;
medianBlur(_img, src, 9);

// chuyển ảnh gốc sang ảnh xám
cv::cvtColor(src, _imgGray, CV_BGR2GRAY);
cv::Mat _imgGray2;
medianBlur(_imgGray, _imgGray2, 7);
blur(_imgGray2, _imgGray2, Size(3, 3));
//Canny
cv::Mat edges;
//dalation

//cv::Canny(_imgGray, edges, 100, 250);
cv::Canny(_imgGray2, edges, 100, 200, 3);
//contour
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
//
//vector<Rect> boundRect(contours.size());
//CvMemStorage* stor = cvCreateMemStorage(1000);
findContours(edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// vẽ đường bao các cạnh

Mat drawing = Mat::zeros(edges.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
    Scalar color = Scalar(0,255,0);
    drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());     
}
//filter contour
????
/// show image
cv::imshow("Goc", _img);        // show ảnh gốc
//
//cv::namedWindow("Anh xam", CV_WINDOW_AUTOSIZE);
cv::imshow("Xam", _imgGray);        // show ảnh xám
cv::imshow("edges", edges);     // show ảnh Canny
cv::imshow("contours", drawing);
cv::waitKey(0);                 
return(0);
}

original

gray

cany_egdes

contours

最佳答案

您可以使用opencv中的boundingRect(或某些版本中的boundingBox)函数来提取轮廓的边界框。

   int w_threshold = 100;
    int h_threshold = 100;
    vector<int> selected;
    for (int i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(0, 255, 0);
        Rect R = boundingRect(contours[i]);
        // filter contours according to their bounding box
        if (R.width > w_threshold && R.height > h_threshold)
        {
            selected.push_back(i);
            drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
        }
    }
    //filter contour
        /// show image
        cv::imshow("Goc", _img);        // show ảnh gốc
        for (size_t i = 0; i < selected.size(); i++)
        {
            rectangle(_img, boundingRect(contours[selected[i]]), Scalar(0, 0, 255), 5);
        }
        cv::imshow("license candidates", _img);        // show ảnh xám

这是我的输出: enter image description here

您还可以使用cvBlobLibs库。它具有简单的功能来像您想做的那样操作 Blob 。

关于c++ - 如何使用 OpenCV 和 C++ 通过限制矩形大小来过滤轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623878/

相关文章:

c++ - C/C++ 交互式解释器

c++ - 给定一个数组,是否有算法可以从中分配内存?

python-2.7 - 如何在全屏 opencv 窗口中以真实大小显示图像?

c - 如何将图像转换为 Redscale?

python-3.x - 如何将 PyCairo 表面转换为 OpenCV numpy 并在 Python 3 中返回

android - 如何在没有相机的情况下将 OpenCV 用于存储的图像?

javascript - ATL ActiveX 控件如何向 javascript 公开一个 bool 值

c++ - 在 C++ 中类继承的情况下强制后期方法解析

C++ ofstream 不在程序的后续运行中创建和写入文件

python - OpenCv - 从边缘创建蒙版 - 裁剪并保存图像