forked from ray/RobotKernal-UESTC
123 lines
4.1 KiB
C++
123 lines
4.1 KiB
C++
#include <iostream>
|
|
#include <opencv2/opencv.hpp>
|
|
|
|
int main(int argc, char **argv){
|
|
|
|
/* 生成一张像素点为0.05的空白图
|
|
cv::Mat image = cv::Mat(400, 400, CV_8UC1, cv::Scalar(0));
|
|
|
|
cv::Rect whiteRect(150, 150, 100, 100);
|
|
cv::rectangle(image, whiteRect, cv::Scalar(255), cv::FILLED);
|
|
cv::imshow(" Image", image);
|
|
cv::imwrite("output.png", image);
|
|
|
|
cv::waitKey(0);
|
|
return 0;
|
|
*/
|
|
// char *input_path = argv[0];
|
|
// char *output_path = argv[1];
|
|
// 读取PNG图片
|
|
// cv::Mat image = cv::imread("../map/07.png");
|
|
// cv::Mat image = cv::imread("/home/xxx/test_map/map/test.png");
|
|
// cv::Mat image = cv::imread(input_path,1);
|
|
|
|
// 保存输出图像的路径
|
|
// std::string output_directory = argv[2];
|
|
std::string input_image_name = argv[1];
|
|
std::string output_image_name = argv[2];
|
|
|
|
// 拼接输出图像的完整路径
|
|
std::string input_path = "/home/xxx/test_map/map/" + input_image_name + ".png";
|
|
std::string output_path = "/home/xxx/test_map/map/" + output_image_name + ".png";
|
|
cv::Mat image = cv::imread(input_path);
|
|
|
|
// 检查图像是否成功加载
|
|
if (image.empty()) {
|
|
std::cerr << "Error: Unable to load input image." << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
|
|
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
|
|
cv::Mat closedImage;
|
|
|
|
cv::morphologyEx(image, closedImage, cv::MORPH_OPEN, kernel);
|
|
|
|
cv::bitwise_not(closedImage, closedImage);
|
|
|
|
cv::Mat erodedImage;
|
|
cv::dilate(closedImage, erodedImage, kernel);
|
|
|
|
// 将图像转换为灰度图
|
|
cv::Mat grayImage;
|
|
cv::cvtColor(erodedImage, grayImage, cv::COLOR_BGR2GRAY);
|
|
|
|
// 对灰度图进行阈值分割,得到二值图像
|
|
cv::Mat binaryImage;
|
|
cv::threshold(grayImage, binaryImage, 128, 255, cv::THRESH_BINARY);
|
|
|
|
cv::Mat edges;
|
|
cv::Canny(binaryImage, edges, 50, 150);
|
|
|
|
// 使用Hough线变换检测直线
|
|
std::vector<cv::Vec2f> lines;
|
|
cv::HoughLines(edges, lines, 1, CV_PI / 180, 100);
|
|
|
|
// 绘制检测到的直线
|
|
cv::Mat result_clone = edges.clone();
|
|
for (size_t i = 0; i < lines.size(); ++i) {
|
|
float rho = lines[i][0];
|
|
float theta = lines[i][1];
|
|
double a = cos(theta);
|
|
double b = sin(theta);
|
|
double x0 = a * rho;
|
|
double y0 = b * rho;
|
|
cv::Point pt1(cvRound(x0 + 1000 * (-b)), cvRound(y0 + 1000 * (a)));
|
|
cv::Point pt2(cvRound(x0 - 1000 * (-b)), cvRound(y0 - 1000 * (a)));
|
|
cv::line(result_clone, pt1, pt2, cv::Scalar(255), 1, cv::LINE_AA);
|
|
}
|
|
|
|
// 查找图像中的轮廓
|
|
std::vector<std::vector<cv::Point>> contours;
|
|
std::vector<cv::Vec4i> hierarchy;
|
|
cv::findContours(edges, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_NONE);
|
|
// std::cout<<contours.size()<<std::endl;
|
|
// for (size_t i = 0; i < contours.size(); ++i) {
|
|
// std::cout << "Contour " << i << " points:" << std::endl;
|
|
// for (size_t j = 0; j < contours[i].size(); ++j) {
|
|
// std::cout << "(" << contours[i][j].x << ", " << contours[i][j].y << ") ";
|
|
// }
|
|
// std::cout << std::endl;
|
|
// }
|
|
|
|
// 创建白色背景图像
|
|
cv::Mat whiteBackground = cv::Mat::zeros(image.size(), CV_8UC1) + 255;
|
|
|
|
// 绘制父轮廓(没有子轮廓的轮廓)
|
|
if(contours.size()>4){
|
|
for (int i = 0; i < contours.size(); ++i) {
|
|
if (hierarchy[i][2] != -1 && hierarchy[i+1][2] != -1) {
|
|
cv::drawContours(whiteBackground, contours, i, cv::Scalar(0), cv::FILLED);
|
|
cv::drawContours(whiteBackground, contours, i+1, cv::Scalar(255), cv::FILLED);
|
|
}
|
|
}
|
|
}else{
|
|
for (int i = 0; i < contours.size(); ++i) {
|
|
if (hierarchy[i][2] != -1 ) {
|
|
cv::drawContours(whiteBackground, contours, i, cv::Scalar(0), cv::FILLED);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 显示结果
|
|
cv::bitwise_not(whiteBackground, whiteBackground);
|
|
|
|
cv::imshow("final output", whiteBackground);
|
|
cv::imwrite(output_path, whiteBackground);
|
|
|
|
cv::waitKey(0);
|
|
return 0;
|
|
|
|
|
|
} |