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

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

当我处理图像时(之前)在图像中给出,然后我需要它的外边框并在 OpenCV 中绘制红色线(像之后的图像)android。

这是cv::findContours的经典应用。

documentation 中提供了一个工作示例。

对于您的具体情况,您可以这样做:

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

这会给你这个输出: