直线检测
cpp
复制代码
void LineDetect(const cv::Mat &binaryImage)
{
cv::Mat xImage,yImage,binaryImage1,binaryImage2;
// 形态学变化,闭操作 先膨胀,再腐蚀 可以填充小洞,填充小的噪点
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 3));
morphologyEx(binaryImage, binaryImage1, cv::MORPH_CLOSE, element);
cv::imwrite("close1.jpg",binaryImage1);
cv::Sobel(binaryImage1, xImage, CV_8U, 1, 0);
element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 15));
morphologyEx(binaryImage1, binaryImage2, cv::MORPH_CLOSE, element);
cv::imwrite("close2.jpg",binaryImage2);
cv::Sobel(binaryImage2, yImage, CV_8U, 0, 1);
vector<cv::Vec4i> lines;
cv::HoughLinesP(xImage,lines, 1, CV_PI / 180, 10,100,50);
for (size_t i = 0; i < lines.size(); i++)
{
cv::Vec4i l = lines[i];
line(xImage, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(255), 3, cv::LINE_AA);
}
// drawLine(xImage, lines, xImage.rows, xImage.cols, cv::Scalar(255),1);
cv::imwrite("xImage.jpg",xImage);
cv::imwrite("yImage.jpg",yImage);
}
轮廓检测
cpp
复制代码
void ContourDetect(const cv::Mat &binaryImage,vector<vector<cv::Point>> &contours)
{
int closeSize = 25;
float areaRatio = 0.005;
int imgArea = binaryImage.cols * binaryImage.rows;
cv::Mat closeImg;
// 形态学变化,闭操作 先膨胀,再腐蚀 可以填充小洞,填充小的噪点
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(closeSize, closeSize));
morphologyEx(binaryImage, closeImg, cv::MORPH_CLOSE, element);
cv::imwrite("closeImg.jpg",closeImg);
cv::Mat edges_image;
// 应用Canny边缘检测算法
Canny(closeImg, edges_image, 100, 200);
cv::imwrite("edges_image.jpg",edges_image);
// 寻找轮廓
vector<vector<cv::Point>> allContours;
cv::findContours(edges_image, allContours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
cout << "allContours size is: " <<allContours.size()<< endl;
// 轮廓过滤
for (auto contour:allContours)
{
cv::Rect rect = cv::boundingRect(contour);
// cout << rect.area() <<" "<< imgArea * areaRatio <<endl;
if (rect.area() > imgArea * areaRatio)
{
contours.push_back(contour);
}
}
// 绘制轮廓
cv::Mat contourImg = cv::Mat::zeros(binaryImage.size(), binaryImage.type());
cout << "contours size is: " <<contours.size()<< endl;
for (int i = 0; i < contours.size(); i++)
{
cv::drawContours(contourImg, contours, i, cv::Scalar(255), 2);
}
cv::imwrite("contours.jpg", contourImg);
}
连通区域检测
cpp
复制代码
void ConnectedComponentRect(const cv::Mat &binaryImage,vector<cv::Rect> &rects)
{
int closeSize = 25;
float areaRatio = 0.001;
int imgArea = binaryImage.cols * binaryImage.rows;
cv::Mat closeImg;
// 形态学变化,闭操作 先膨胀,再腐蚀 可以填充小洞,填充小的噪点
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(closeSize, closeSize));
morphologyEx(binaryImage, closeImg, cv::MORPH_CLOSE, element);
cv::imwrite("closeImg.jpg",closeImg);
cv::Mat labels, stats, centroids;
int num_objects = cv::connectedComponentsWithStats(closeImg, labels, stats, centroids);
for (int i = 1; i < num_objects; i++) {
// 获取连通组件的统计信息
int* stat = stats.ptr<int>(i);
int left = stat[cv::CC_STAT_LEFT];
int top = stat[cv::CC_STAT_TOP];
int width = stat[cv::CC_STAT_WIDTH];
int height = stat[cv::CC_STAT_HEIGHT];
int area = stat[cv::CC_STAT_AREA];
if (area > imgArea * areaRatio)
{
rects.push_back(cv::Rect(left, top, width, height));
}
}
cout << num_objects << " rect size is:"<< rects.size() <<endl;
}