1、源码
#include <opencv2/opencv.hpp>
#include <QDebug>
#include <opencv2/imgproc.hpp>
using namespace cv;
int main()
{
Mat image = imread("C:/Users/Public/input.png", IMREAD_GRAYSCALE);
// get_outline(image);
// Mat image = imread("temp.png", IMREAD_GRAYSCALE);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat img_dilate, img_erode;
dilate(image, img_dilate, kernel, Point(-1, -1), 3);
erode(image, img_erode, kernel, Point(-1, -1), 3);
Mat laplacian;
Laplacian(image, laplacian, CV_8U, 3);
threshold(laplacian, laplacian, 127, 255, THRESH_BINARY | THRESH_OTSU);
std::vector<std::vector<Point>> contours;
std::vector<Vec4i> hierarchy;
findContours(laplacian, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
qDebug()<<hierarchy.size();
// Mat draw_img = Mat::zeros(image.size(), CV_8UC3);
Mat image1 = imread("C:/Users/Public/input.png", IMREAD_COLOR);
drawContours(image1, contours, -1, Scalar(0, 0, 255), 1);
imshow("Photo Boundaries", image1);
waitKey(0);
return 0;
}
2.遇到的问题
在使用边缘检测时第一次使用的是canny检测导致最外层轮廓不完整,使得得到的最外层轮廓不是理想的结果,正确的应该是使用Laplacian进行边缘检测,得到的图形最外层轮廓是完整的。