OpenCV schließen Sie eine Form und füllen es

Ich möchte die Ausgabe einer blau gefüllten hand bekomme aber immer die falsche Ausgabe. Ich habe den Eingangs-Bild, falsche Ausgabe von Bild und code.

ich denke, dass der folgende code füllt nicht das ganze Bild, weil das Bild nicht geschlossen ist, noch an der rechts-Grenze.

wie schließe ich die Form und füllen Sie es mit blau richtig?

OpenCV schließen Sie eine Form und füllen es

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

void drawStuff();
void showInputWindow();
void showCannyWindow();
void showContourWindow();

int thresh = 40;
int max_thresh = 120;
Mat img_rgb,img_gray,img_bw,canny_output,drawing;

int main(){
    img_rgb  = imread("qq.jpg");
    blur( img_rgb, img_rgb, Size(3,3) );
    cvtColor(img_rgb,img_gray,CV_RGB2GRAY);
    showInputWindow();

    drawStuff();
    cv::waitKey(0);
}

void drawStuff(){
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Canny( img_gray, canny_output, thresh, thresh*2, 3 );
    cv::dilate(canny_output, canny_output, cv::Mat(), cv::Point(-1,-1));
    showCannyWindow();

    findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    drawing = Mat::zeros( canny_output.size(), CV_8UC3 );

    vector<Point> approxShape;
    for(size_t i = 0; i < contours.size(); i++){
        approxPolyDP(contours[i], approxShape, arcLength(Mat(contours[i]), true)*0.04, true);
        drawContours(drawing, contours, i, Scalar(255, 0, 0), CV_FILLED);   //fill BLUE
    }

    showContourWindow();
}

void showInputWindow(){
    cv::namedWindow("InputImage");
    cv::imshow("InputImage",img_rgb);
}

void showCannyWindow(){
    cv::namedWindow("Canny");
    cv::imshow("Canny",canny_output);
}
void showContourWindow(){
    cv::namedWindow("Fill");
    cv::imshow("Fill",drawing);
}
Ich denke, das problem ist, dass es Konturen erkannt in der Innenseite der hand. Versuchen Sie, ein inverser Ansatz - füllen Sie die Umgebung und kehren Sie dann.
wie haben Sie füllen die Umgebung?
Zeichne nur die Konturen (nicht gefüllt), einige unterschiedliche Farbe, wie grün, dann verwenden Sie cvFloodFill der Setzliste in der oberen linken Ecke (Koordinaten 0, 0) und füllen Sie es mit blau. Dann gehen die Bild-und-ersetzen blauen Pixel mit schwarz und schwarze oder grüne Pixel mit blau.
Ja, es ist wahrscheinlich, dass es Löcher in die Konturen. Versuchen cvDilate gefolgt von cvErode zu schließen kleine Lücken.
cvErode(src, dst, NULL, 1) der gleichen, wie für cvDilate. Die "1" bedeutet, wie groß die Lücken (in Pixel) wird geschlossen.

InformationsquelleAutor Og Namdik | 2012-10-17

Schreibe einen Kommentar