Computer vision image feature extraction

Image feature extraction is an important task in computer vision, which helps in identifying, classifying, detecting and tracking objects. The following are some commonly used image feature extraction algorithms and their introduction:

  1. Color Histogram :

    • Introduction : A color histogram represents the distribution of various colors in an image. The color information of an image is captured by dividing the pixels in the image into color channels (such as RGB) and calculating the distribution of color values ​​for each channel.
    • Application : Color histograms are commonly used in tasks such as image retrieval, image classification, and image matching.
  2. Local Binary Pattern (LBP) :

    • Introduction : LBP is a method for texture feature extraction. It builds features by comparing the gray value of each pixel with its neighbor pixels. LBP features have a good ability to describe texture changes and structures.
    • Application : LBP is commonly used in tasks such as face recognition, texture classification, and target detection.
  3. Histogram of Oriented Gradients (HOG) :

    • Introduction : HOG is a feature extraction method for object detection, especially suitable for pedestrian detection. It represents an image by calculating the gradient direction for each pixel in the image and constructing a histogram of the gradient directions.
    • Application : HOG is widely used in fields such as pedestrian detection, target recognition and behavior analysis.
  4. Scale-Invariant Feature Transform (SIFT) :

    • Introduction : SIFT is a method for detecting and describing local features in images. It is invariant to changes in image scale, rotation, and brightness, so it performs well under a variety of conditions.
    • Applications : SIFT is widely used in tasks such as feature matching, object recognition, and image registration.
  5. Speeded-Up Robust Features (SURF) :

    • Introduction : SURF is a fast algorithm for image feature extraction. It combines the features of SIFT and uses integral images to accelerate feature detection and descriptor calculations.
    • Application : SURF is suitable for real-time applications such as real-time target tracking and image stitching.
  6. Convolutional Neural Network Features :

    • Introduction : Deep learning-based methods, such as convolutional neural networks (CNN), have achieved remarkable success in image feature extraction tasks. By extracting feature vectors on pre-trained CNN models, powerful image representations can be obtained.
    • Application : CNN features are widely used in various computer vision tasks such as image classification, target detection, and image generation.

course:

Color Histogram example :

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat image = cv::imread("image.jpg");
    cv::Mat hist;

    // 将图像转换为HSV颜色空间
    cv::cvtColor(image, image, cv::COLOR_BGR2HSV);

    // 计算直方图
    int histSize = 256; // 直方图的大小
    float range[] = {
    
    0, 256}; // 像素值范围
    const float* histRange = {
    
    range};
    cv::calcHist(&image, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange);

    // 打印直方图
    for (int i = 0; i < histSize; i++) {
    
    
        std::cout << "Bin " << i << ": " << hist.at<float>(i) << std::endl;
    }

    return 0;
}

Local Binary Pattern (LBP) example :

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat image = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat lbpImage;

    // 计算LBP图像
    cv::Ptr<cv::ximgproc::LBP> lbp = cv::ximgproc::createLBPFast();
    lbp->compute(image, lbpImage);

    return 0;
}

Histogram of Oriented Gradients (HOG) example :

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat image = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE);
    cv::HOGDescriptor hog;

    // 设置HOG参数
    hog.winSize = cv::Size(64, 128); // 检测窗口大小
    hog.blockSize = cv::Size(16, 16); // 块大小
    hog.blockStride = cv::Size(8, 8); // 块的步幅
    hog.cellSize = cv::Size(8, 8); // 细胞大小

    // 计算HOG特征向量
    std::vector<float> descriptors;
    hog.compute(image, descriptors);

    return 0;
}

Speeded-Up Robust Features (SURF) example :

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat image = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE);
    cv::Ptr<cv::xfeatures2d::SURF> surf = cv::xfeatures2d::SURF::create();

    // 检测关键点和计算描述子
    std::vector<cv::KeyPoint> keypoints;
    cv::Mat descriptors;
    surf->detectAndCompute(image, cv::noArray(), keypoints, descriptors);

    return 0;
}

Scale-Invariant Feature Transform (SIFT) example :

#include <opencv2/opencv.hpp>

int main() {
    
    
    cv::Mat image = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE);
    cv::Ptr<cv::xfeatures2d::SIFT> sift = cv::xfeatures2d::SIFT::create();

    // 检测关键点和计算描述子
    std::vector<cv::KeyPoint> keypoints;
    cv::Mat descriptors;
    sift->detectAndCompute(image, cv::noArray(), keypoints, descriptors);

    return 0;
}

Convolutional Neural Network Features Example :

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main() {
    
    
    cv::Mat image = cv::imread("image.jpg");
    cv::dnn::Net net = cv::dnn::readNetFromCaffe("deploy.prototxt", "model.caffemodel");

    // 预处理图像(归一化、尺寸调整等)
    cv::Mat blob = cv::dnn::blobFromImage(image, 1.0, cv::Size(224, 224), cv::Scalar(104, 117, 123));

    // 设置输入图像
    net.setInput(blob);

    // 前向传播并获取特征向量
    cv::Mat features = net.forward();

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_42244167/article/details/132410569