OpenCV关闭一个形状并填充它 [英] OpenCV closing a shape and filling it

查看:147
本文介绍了OpenCV关闭一个形状并填充它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出一个蓝色的手,但得到不正确的输出。我在下面包含了输入图片,错误的输出图片和代码。



我认为下面的代码不会填充整个图片,因为图片尚未关闭



 # 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> >轮廓;
vector< Vec4i>层次;

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 approxPolyDP(contoururs [i],approxShape,arcLength(Mat(contoururs [i]),true)* 0.04 ,true);
drawContours(drawing,contoururs,i,Scalar(255,0,0),CV_FILLED); // fill BLUE
}

showContourWindow();
}

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

void displayCannyWindow(){
cv :: namedWindow(Canny);
cv :: imshow(Canny,canny_output);
}
void showContourWindow(){
cv :: namedWindow(Fill);
cv :: imshow(Fill,drawing);
}


解决方案

要解决您的问题,应该只检测外轮廓。通过这样做,你将只得到一个轮廓,你可以感觉到它的颜色。

  findContours(canny_output,contour,hierarchy,Imgproc.RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE,Point(0,0)); 


I want to output a blue-filled hand but get the incorrect output. I've included the input picture, incorrect output picture and code below.

i think the code below does not fill the whole image because the image isn't closed yet at the right boundary.

how do i close the shape and fill it with blue properly?

#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);
}

解决方案

To solve your problem, you should detect only outer contours. By doing that you will get only one contour, and you can feel it with the color.

findContours( canny_output, contours, hierarchy, Imgproc.RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

这篇关于OpenCV关闭一个形状并填充它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆