簡體   English   中英

使用findContours時如何避免檢測圖像幀

[英]How to avoid detecting image frame when using findContours

使用findContours (OpenCV)時如何避免檢測圖像的幀? 直到我發現OpenCV findContours始終為每個對象找到兩個輪廓並實現該答案之前,我一直沒有始終如一地檢測內部對象(對象線被分成幾部分),但是現在我每次都檢測到圖像幀。

該圖像是從底部看到的四旋翼無人機的圖像; 我正在使用一系列圖片來“訓練”物體檢測。 為此,我需要確保我能夠始終如一地獲得UAV對象。 我想我可以反轉顏色,但這似乎是一個骯臟的技巧。

圖像首先是findContours之前的輸入圖像,以及最終得到的輪廓。 我有七個測試圖像,所有七個都有一個框架和一個無人機。 的時刻非常相似(如預期)。

侵蝕后的二進制圖像

二進制圖像,檢測到輪廓

查找輪廓/對象並計算hu力矩的代碼(C ++ 11,相當混亂):

#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <fstream>
#include <string>

using namespace cv;
using namespace std;

#define EROSION_SIZE 1
#define ERODE_CANNY_PREP_ITERATIONS 5

int main() {
    Mat image, canny_output, element, padded;
    RNG rng(12345);
    int numbers[] = {195, 223, 260, 295, 331, 368, 396};
    string pre = "/home/alrekr/Pictures/UAS/hu-images/frame_";
    string middle = "_threshold";
    string post = ".png";
    string filename = "";
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    ofstream fout("/home/alrekr/Pictures/UAS/hu-data/hu.dat");
    element = getStructuringElement(MORPH_RECT,
            Size(2*EROSION_SIZE + 1, 2*EROSION_SIZE+1),
            Point(EROSION_SIZE, EROSION_SIZE));
    namedWindow("Window", CV_WINDOW_AUTOSIZE);
    for (int i : numbers) {
        filename = pre + to_string(i) + middle + post;
        image = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
        erode(image, image, element, Point(-1,-1), ERODE_CANNY_PREP_ITERATIONS);
        imwrite("/home/alrekr/Pictures/UAS/hu-data/prep_for_canny_" + to_string(i) + ".png", image);
    findContours(image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
        vector<Moments> mu(contours.size());
        if(contours.size() < 1) {
            cout << "No contours found" << endl;
        } else {
            cout << "Contours found: " << contours.size() << endl;
        }
        vector<Point2f> mc(contours.size());
        for(int j = 0; j < (int)contours.size(); j++) {
            mc[j] = Point2f(mu[j].m10/mu[j].m00 , mu[j].m01/mu[j].m00);
        }
        Mat drawing = Mat::zeros(image.size(), CV_8UC3);
        for(int j = 0; j < (int)contours.size(); j++) {
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255));
            drawContours(drawing, contours, j, color, 2, 8, hierarchy, 0, Point());
            imshow("Window", drawing);
            waitKey(0);
        }
        imwrite("/home/alrekr/Pictures/UAS/hu-data/cannied_" + to_string(i) + ".png", drawing);
        fout << "Frame " << i << "\n";
        for(int j = 0; j < (int)contours.size(); j++) {
            mu[j] = moments(contours[j]);
            double hu[7];
            HuMoments(mu[j], hu);
            fout << "Object " << to_string(j) << "\n";
            fout << hu[0] << "\n";
            fout << hu[1] << "\n";
            fout << hu[2] << "\n";
            fout << hu[3] << "\n";
            fout << hu[4] << "\n";
            fout << hu[5] << "\n";
            fout << hu[6] << "\n";
        }
    }
    fout.close();
    return 0;
}

函數cv::findContours描述了由一個組成的區域的輪廓。 但是,您感興趣的區域是黑色的。

因此,解決方案很簡單。 在檢測輪廓之前反轉輸入圖像:

image = 255 - image;

下面是一個代碼示例,該代碼示例是從您上面的示例得出的:

#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <string>

#define EROSION_SIZE 1
#define ERODE_CANNY_PREP_ITERATIONS 5

int main( int argc, char ** argv )
{
    // Display the version of the linked OpenCV library.
    std::cout << "Using OpenCV " << CV_VERSION_MAJOR << "." << CV_VERSION_MINOR << ".";
    std::cout << CV_VERSION_REVISION << CV_VERSION_STATUS << std::endl;

    // Load the input file.
    std::string filename = std::string( argv[ 1 ] );
    cv::Mat image = imread( filename, cv::IMREAD_GRAYSCALE );

    // Invert the image so the area of the UAV is filled with 1's. This is necessary since
    // cv::findContours describes the boundary of areas consisting of 1's.
    image = 255 - image;

    // Detect contours.
    std::vector< std::vector< cv::Point> > contours;
    std::vector< cv::Vec4i > hierarchy;

    cv::findContours( image, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE );
    std::cout << "Contours found: " << contours.size() << std::endl;

    // Display and save the results.
    cv::RNG rng( 12345 );
    cv::Mat contourImage = cv::Mat::zeros( image.size(), CV_8UC3);

    for( size_t j = 0; j <  contours.size(); j++ )
    {
        cv::Scalar color( rng.uniform( 0, 255 ), rng.uniform( 0,255 ), rng.uniform( 0, 255 ) );
        cv::drawContours( contourImage, contours, j, color, 2, 8, hierarchy, 0, cv::Point() );
    }

//  cv::imwrite( "contours.png", contourImage );

    cv::imshow( "contours", contourImage );
    cv::waitKey( 0 );

    return 0;
}

控制台輸出如下:

$ ./a.out gvlGK.png 
Using OpenCV 3.0.0-beta
Contours found: 1

得到的輪廓圖像是這樣的:

等高線

另一個解決方案是:

找到輪廓的邊界矩形

x,y,w,h = cv2.boundingRect(c)

例如,將圖像的大小與邊界矩形的大小進行比較

cnt_size=w*h
if(abs(cnt_size-img_size<=ERROR_THRESHOLD):
    ##discard this contour 

如果您有白色背景,請先使用THRESH_BINARY_INV類型反轉背景,然后再使用輪廓。

    image = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
    threshold(image,image,100,255,THRESH_BINARY_INV);
    findContours( image, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE );

這只會返回您需要的輪廓。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM