Koi-Blue / Koi-Blue.github.io

1 stars 0 forks source link

OpenCV第三阶段 #10

Open Koi-Blue opened 1 week ago

Koi-Blue commented 1 week ago

第三阶段:中级图像处理

1. 图像滤波

目标:

内容:

实践:

1.1 平滑滤波

均值滤波( Average Filtering
#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // 读取图像
    cv::Mat image = cv::imread("example.jpg");
    if (image.empty()) {
        std::cerr << "Could not open or find the image!" << std::endl;
        return -1;
    }

    // 均值滤波
    cv::Mat avgFiltered;
    cv::blur(image, avgFiltered, cv::Size(5, 5));  // 使用5x5的滤波器

    // 显示结果
    cv::imshow("Original Image", image);
    cv::imshow("Average Filtered Image", avgFiltered);
    cv::waitKey(0);
    return 0;
}
解释:
cv::Mat gaussianFiltered;
cv::GaussianBlur(image, gaussianFiltered, cv::Size(5, 5), 1.5);  // 使用5x5的高斯核
解释:
Sobel 边缘检测
cv::Mat sobelX, sobelY, sobelCombined;
cv::Sobel(image, sobelX, CV_64F, 1, 0, 3);  // 水平方向Sobel
cv::Sobel(image, sobelY, CV_64F, 0, 1, 3);  // 垂直方向Sobel

// 合并X方向和Y方向的边缘
cv::magnitude(sobelX, sobelY, sobelCombined);

cv::imshow("Sobel Edge Detection", sobelCombined);
cv::waitKey(0);
解释:
cv::Mat cannyEdges;
cv::Canny(image, cannyEdges, 100, 200);  // 阈值分别为100和200
cv::imshow("Canny Edge Detection", cannyEdges);
cv::waitKey(0);
解释:
腐蚀( Erosion

cv::Mat eroded;
cv::Mat kernel = cv::Mat::ones(3, 3, CV_8U);  // 3x3大小的结构元素
cv::erode(image, eroded, kernel);
cv::imshow("Eroded Image", eroded);
cv::waitKey(0);
解释:
cv::Mat dilated;
cv::dilate(image, dilated, kernel);
cv::imshow("Dilated Image", dilated);
cv::waitKey(0);
解释:

内容:

cv::Mat gray, cornerStrength, cornerImage;
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
cv::cornerHarris(gray, cornerStrength, 3, 3, 0.04);

// 归一化结果
cv::normalize(cornerStrength, cornerStrength, 0, 255, cv::NORM_MINMAX, CV_32FC1);

// 标记角点
cornerImage = image.clone();
for (int i = 0; i < cornerStrength.rows; i++) {
    for (int j = 0; j < cornerStrength.cols; j++) {
        if (cornerStrength.at<float>(i, j) > 100) {
            circle(cornerImage, cv::Point(j, i), 5, cv::Scalar(0, 0, 255), 2);
        }
    }
}

cv::imshow("Harris Corner Detection", cornerImage);
cv::waitKey(0);
解释:
#include <opencv2/xfeatures2d.hpp>

cv::Ptr<cv::xfeatures2d::SIFT> sift = cv::xfeatures2d::SIFT::create();
std::vector<cv::KeyPoint> keypoints;
cv::Mat descriptors;
sift->detectAndCompute(image, cv::Mat(), keypoints, descriptors);

cv::Mat output;
cv::drawKeypoints(image, keypoints, output);
cv::imshow("SIFT Features", output);
cv::waitKey(0);
解释:
cv::Ptr<cv::xfeatures2d::SIFT> sift = cv::xfeatures2d::SIFT::create();
cv::Mat descriptors1, descriptors2;
std::vector<cv::KeyPoint> keypoints1, keypoints2;

// 提取特征点和描述子
sift->detectAndCompute(image1, cv::Mat(), keypoints1, descriptors1);
sift->detectAndCompute(image2, cv::Mat(), keypoints2, descriptors2);

// 暴力匹配
cv::BFMatcher matcher(cv::NORM_L2, true);
std::vector<cv::DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);

// 绘制匹配结果
cv::Mat imgMatches;
cv::drawMatches(image1, keypoints1, image2, keypoints2, matches, imgMatches);
cv::imshow("Feature Matching", imgMatches);
cv::waitKey(0);
解释:
Tatsuyxa commented 1 week ago

The donkeys of the production team did not dare to rest like this,boss were pretty diligent

Koi-Blue commented 6 days ago

The donkeys of the production team did not dare to rest like this,boss were pretty diligent

Fish never lie down to sleep