#include <iostream>
#include <opencv2/opencv.hpp>
// 高斯滤波
void gaussianBlur(const cv::Mat& input, cv::Mat& output, int kernelSize, double sigma) {
cv::GaussianBlur(input, output, cv::Size(kernelSize, kernelSize), sigma);
}
// 计算梯度幅值和方向
void calculateGradient(const cv::Mat& input, cv::Mat& gradientMagnitude, cv::Mat& gradientDirection) {
cv::Mat gradientX, gradientY;
cv::Sobel(input, gradientX, CV_64F, 1, 0, 3);
cv::Sobel(input, gradientY, CV_64F, 0, 1, 3);
cv::cartToPolar(gradientX, gradientY, gradientMagnitude, gradientDirection, true);
}
// 非极大值抑制
void nonMaxSuppression(const cv::Mat& gradientMagnitude, const cv::Mat& gradientDirection, cv::Mat& output) {
cv::Mat paddedMagnitude;
cv::copyMakeBorder(gradientMagnitude, paddedMagnitude, 1, 1, 1, 1, cv::BORDER_CONSTANT, 0);
output = cv::Mat::zeros(gradientMagnitude.size(), CV_8U);
std::vector<cv::Point> edgePoints; // 存储边缘点的坐标
for (int i = 1; i < paddedMagnitude.rows - 1; ++i) {
for (int j = 1; j < paddedMagnitude.cols - 1; ++j) {
float currentMagnitude = paddedMagnitude.at<float>(i, j);
float angle = gradientDirection.at<float>(i - 1, j - 1);
float value1, value2;
if ((angle >= 0 && angle < 45) || (angle >= 180 && angle < 225)) {
value1 = paddedMagnitude.at<float>(i, j - 1);
value2 = paddedMagnitude.at<float>(i, j + 1);
}
else if ((angle >= 45 && angle < 90) || (angle >= 225 && angle < 270)) {
value1 = paddedMagnitude.at<float>(i - 1, j - 1);
value2 = paddedMagnitude.at<float>(i + 1, j + 1);
}
else if ((angle >= 90 && angle < 135) || (angle >= 270 && angle < 315)) {
value1 = paddedMagnitude.at<float>(i - 1, j);
value2 = paddedMagnitude.at<float>(i + 1, j);
}
else {
value1 = paddedMagnitude.at<float>(i - 1, j + 1);
value2 = paddedMagnitude.at<float>(i + 1, j - 1);
}
if (currentMagnitude >= value1 && currentMagnitude >= value2) {
output.at<uchar>(i - 1, j - 1) = 255;
// 记录边缘点的坐标
edgePoints.push_back(cv::Point(j - 1, i - 1));
}
}
}
// 打印边缘点的坐标
for (const auto& point : edgePoints) {
std::cout << "Edge Point: (" << point.x << ", " << point.y << ")" << std::endl;
}
}
// 双阈值处理
void doubleThreshold(const cv::Mat& input, cv::Mat& output, double lowThreshold, double highThreshold) {
output = cv::Mat::zeros(input.size(), CV_8U);
for (int i = 0; i < input.rows; ++i) {
for (int j = 0; j < input.cols; ++j) {
if (input.at<uchar>(i, j) >= highThreshold) {
output.at<uchar>(i, j) = 255;
}
else if (input.at<uchar>(i, j) >= lowThreshold) {
bool hasHighNeighbor = false;
for (int m = -1; m <= 1; ++m) {
for (int n = -1; n <= 1; ++n) {
if (i + m >= 0 && i + m < input.rows && j + n >= 0 && j + n < input.cols) {
if (input.at<uchar>(i + m, j + n) >= highThreshold) {
hasHighNeighbor = true;
break;
}
}
}
}
if (hasHighNeighbor) {
output.at<uchar>(i, j) = 255;
}
}
}
}
}
int main() {
// 读取彩色图像
cv::Mat image = cv::imread("input.jpg", cv::IMREAD_COLOR);
// 转换为灰度图像
cv::Mat grayImage;
cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
// 高斯滤波
cv::Mat blurredImage;
gaussianBlur(grayImage, blurredImage, 5, 1.4);
// 计算梯度幅值和方向
cv::Mat gradientMagnitude, gradientDirection;
calculateGradient(blurredImage, gradientMagnitude, gradientDirection);
// 非极大值抑制
cv::Mat edges;
nonMaxSuppression(gradientMagnitude, gradientDirection, edges);
// 双阈值处理
cv::Mat finalEdges;
doubleThreshold(edges, finalEdges, 30.0, 80.0);
// 显示边缘点
cv::Mat edgePoints = cv::Mat::zeros(image.size(), CV_8UC3);
for (int i = 0; i < finalEdges.rows; ++i) {
for (int j = 0; j < finalEdges.cols; ++j) {
if (finalEdges.at<uchar>(i, j) == 255) {
edgePoints.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 0, 255); // 将边缘点设置为红色
// 打印边缘点的坐标
std::cout << "Edge Point: (" << j << ", " << i << ")" << std::endl;
}
}
}
// 可视化结果
cv::imshow("Original Image", image);
cv::imshow("Canny Edges", finalEdges);
cv::imshow("Edge Points", edgePoints);
cv::waitKey(0);
return 0;
}