有一如锤子状的图像,该怎么用opencv将上下两块分开,同时能够得到各个角点坐标
//背景:某文件夹series下有若干张图片,对每一张图片进行处理,将处理后的图片保存至新的文件夹save下
#include<opencv2/opencv.hpp>
#include<iostream>
#include<time.h>
#include<fstream>
using namespace std;
using namespace cv;
int main() {
ifstream file("../data/series/test.txt");
int img_index = 0;
while (!file.eof()) {
char txt_cont[200];
file.getline(txt_cont, 200);
char img_file[200], save_file[200];
sprintf(img_file, "../data/series/%s", txt_cont);
sprintf(save_file, "../save/%d.jpg", img_index);
Mat src = imread(img_file);
img_index++;
imwrite(save_file, src);
}
}
凹陷点检测或者如下检测出角点后沿着红色角点处切开:
int main() {
Mat srcGary = imread("G:/projects/XRT/projects/preprocess/XRT_Segmentation/test.png",0);
// 阈值化操作
Mat threMat;
int thresh = 210;
threshold(srcGary, threMat, thresh, 255, THRESH_BINARY_INV);
imwrite("threMat.jpg", threMat);
// 轮廓检测
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(threMat, contours, hierarchy, 0, CHAIN_APPROX_SIMPLE, Point(0, 0));
// 绘制轮廓
Mat contours_img(srcGary.size(), CV_8U, Scalar(0));
drawContours(contours_img, contours, -1, Scalar(255), -1);
imwrite("contours_img.jpg", contours_img);
//绘制凸包及凸包缺陷
Mat convex_hull_img_show;
cvtColor(contours_img, convex_hull_img_show, COLOR_GRAY2BGR);
vector<vector<Point> > lastpts;
for (size_t i = 0; i < contours.size(); i++)
{
vector<Point> cutpts;
vector<Point> concavepts;
judgeIfConx(contours_img,contours[i],4,1,3.0,concavepts,cutpts);
for (size_t id = 0; id < cutpts.size(); id++)
{
circle(convex_hull_img_show, cutpts[id], 4, Scalar(255, 0, 0), 2);
}
for (size_t id = 0; id < concavepts.size(); id++)
{
circle(convex_hull_img_show, concavepts[id], 4, Scalar(0, 0,255), 2);
}
if(concavepts.size()>1)
{
lastpts.push_back(concavepts);
}
}
imwrite("convex_hull_img0.jpg", convex_hull_img_show);
if(lastpts.size()>0)
{
if(lastpts[0].size()==2)
{
line(convex_hull_img_show, lastpts[0][0], lastpts[0][1], CV_RGB(0, 0, 0), 3);
}
}
imwrite("convex_hull_img1.jpg", convex_hull_img_show);
return 0;
}