【问题标题】:How To Visualize Extracted Features如何可视化提取的特征
【发布时间】:2016-03-29 17:22:57
【问题描述】:

我使用 opencv249 和 Visual Studio 2013 提取了图像中区域的四个特征。代码如下:

vector<double> calculateFeatures(Mat src, Mat mask, Rect Rect){
    vector<double> sonuc;
    double Feature1, Feature2, Feature3, Feature4;
    Mat bolum(src, Rect);

    Scalar mean_number = mean(src, mask);
    double num = mean_number.val[0];


    double minVal;
    double maxVal = 0;
    Point minLoc;
    Point maxLoc = 0;
    minMaxLoc(bolum, &minVal, &maxVal, &minLoc, &maxLoc);
    double fark = num - minVal;
    Feature1 = fark;


    double thresh = num*0.95;
    int sayi = countNonZero(bolum < thresh);
    int alan = countNonZero(mask);
    double pixelSayisi = sayi / alan;
    Feature2 = pixelSayisi;



    Mat dst, smoothed;
    GaussianBlur(bolum, smoothed, Size(25, 25), 4, 4);
    Laplacian(smoothed, dst, CV_8UC1, 25, 1, 0, BORDER_DEFAULT);
    cv::Scalar s = cv::sum(dst);
    double toplam = s.val[0];
    Feature3 = toplam;

    cout << "s: " << s << endl;



    Mat dst2;
    cornerHarris(bolum, dst2, 2, 25, 0.04, BORDER_DEFAULT);
    Scalar top = sum(dst2);
    double top2 = top.val[0];
    Feature4 = top2 / alan;

    cout << "Feature4: " << Feature4 << endl;

    sonuc.push_back(Feature1);
    sonuc.push_back(Feature2);
    sonuc.push_back(Feature3);
    sonuc.push_back(Feature4);
    return sonuc;
}

我想查看该区域的代表性特征图像,以评估特征是否有用。我该如何实施?

【问题讨论】:

    标签: c++ opencv feature-extraction


    【解决方案1】:

    此处我计算了此图像上的修补程序32x32上的功能:

    您可以为功能的每个维度创建图像,并且像素的值由该位置的特征值给出。

    如果您最多拥有4个功能,就像在这种情况下一样,您可以将所有图像组合成4频道图像:

    参考的完整代码:

    #include <opencv2/opencv.hpp>
    #include <vector>
    using namespace cv;
    using namespace std;
    
    vector<double> calculateFeatures(Mat src, Mat mask, Rect Rect){
        vector<double> sonuc;
        double Feature1, Feature2, Feature3, Feature4;
        Mat bolum(src, Rect);
    
        Scalar mean_number = mean(src, mask);
        double num = mean_number.val[0];
    
    
        double minVal;
        double maxVal = 0;
        Point minLoc;
        Point maxLoc = 0;
        minMaxLoc(bolum, &minVal, &maxVal, &minLoc, &maxLoc);
        double fark = num - minVal;
        Feature1 = fark;
    
    
        double thresh = num*0.95;
        int sayi = countNonZero(bolum < thresh);
        int alan = countNonZero(mask);
        double pixelSayisi = sayi / alan;
        Feature2 = pixelSayisi;
    
    
    
        Mat dst, smoothed;
        GaussianBlur(bolum, smoothed, Size(25, 25), 4, 4);
        Laplacian(smoothed, dst, CV_8UC1, 25, 1, 0, BORDER_DEFAULT);
        cv::Scalar s = cv::sum(dst);
        double toplam = s.val[0];
        Feature3 = toplam;
    
        cout << "s: " << s << endl;
    
    
    
        Mat dst2;
        cornerHarris(bolum, dst2, 2, 25, 0.04, BORDER_DEFAULT);
        Scalar top = sum(dst2);
        double top2 = top.val[0];
        Feature4 = top2 / alan;
    
        cout << "Feature4: " << Feature4 << endl;
    
        sonuc.push_back(Feature1);
        sonuc.push_back(Feature2);
        sonuc.push_back(Feature3);
        sonuc.push_back(Feature4);
        return sonuc;
    }
    
    int main() 
    {
        Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
    
        int featuresSize = 4;
    
    
        int patch_size = 32;
        int right = patch_size - (img.cols % patch_size);
        int bottom = patch_size - (img.rows % patch_size);
        copyMakeBorder(img, img, 0, bottom, 0, right, BORDER_REFLECT101);
    
        vector<Mat1d> visual;
        for (int i = 0; i < featuresSize; ++i)
        {
            Mat1d v(img.rows, img.cols);
            v.setTo(0);
            visual.push_back(v.clone());
        }
    
        for (int r = 0; r < img.rows; r += patch_size)
        {
            for (int c = 0; c < img.cols; c += patch_size)
            {
                Rect roi(c, r, patch_size, patch_size);
                Mat1b mask(img.rows, img.cols, uchar(0));
                mask(roi) = uchar(255);
    
                vector<double> features = calculateFeatures(img, mask, roi);
                for (int i = 0; i < featuresSize; ++i)
                {
                    visual[i](roi) = features[i];
                }
            }
        }
    
        for (int i = 0; i < featuresSize; ++i)
        {
            normalize(visual[i], visual[i], 0.0, 1.0, NORM_MINMAX);
    
            Mat1b b;
            visual[i].convertTo(b, CV_8U, 255.0);
    
            imshow("Feature " + to_string(i), b);
        }
    
        // Makes sense only if nFeatures <= 4
        Mat all;
        merge(visual, all);
    
        Mat allb;
        all.convertTo(allb, CV_8U, 255.0);
    
        imshow("All", all);
    
        waitKey(0);
    
    
    
        return 0;
    }
    

    【讨论】:

    • 谢谢miki。为什么只有在特征数量小于或等于4? span>时才有意义
    • 您不能具有超过4个通道(bgra) span>的图像
    猜你喜欢
    • 2021-04-11
    • 2015-10-20
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2022-07-06
    • 2021-02-01
    • 2011-07-25
    相关资源
    最近更新 更多