首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何可视化提取特征

如何可视化提取特征
EN

Stack Overflow用户
提问于 2015-12-23 09:54:35
回答 1查看 65关注 0票数 0

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

代码语言:javascript
复制
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;
}

我希望看到该区域具有代表性的特征图像,以便评估这些特征是否有用。我如何实施呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-23 13:14:33

在这里,我计算了这个映像上32x32补丁的特性:

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

如果您最多有4个功能,如本例所示,则可以将所有图像组合成一个4通道映像:

供参考的完整代码:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34432896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档