简体   繁体   English

如何在OpenCV中查找和绘制图像的外线?

[英]How to find and draw outer line of image in OpenCV?

在此处输入图片说明

As given in images when I process image (BEFORE) then I need its outer borders and draw red color line (Like AFTER image) in OpenCV android.正如我处理图像时的图像(BEFORE)然后我需要它的外边框并在OpenCV android中绘制红色线条(如AFTER图像)。

This is a classical application of cv::findContours .这是cv::findContours的经典应用。

A working example is provided in the documentation . 文档中提供了一个工作示例。

For your specific case you can do like this:对于您的具体情况,您可以这样做:

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

int main(int argc, char** argv)
{
    using namespace cv;
    using namespace std;

    Mat3b image;
    image = imread(argv[1], 1);  
    imshow ("Before", image);

    Mat1b gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    threshold(gray, gray, 250, 255, THRESH_BINARY);
    medianBlur(gray,gray, 3);
    gray = 255-gray;

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(gray, contours, hierarchy, RETR_EXTERNAL , CHAIN_APPROX_NONE);

    Scalar color = Scalar( 0, 0, 255 );
    for (size_t i = 0; i < contours.size(); i++) {
        drawContours(image, contours, i, color, 2, 8, hierarchy, 0, Point());
    }     
    imshow("After", image);
    return 0;
}

Which will give you this output:这会给你这个输出: 示例输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM