我使用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;
}
我希望看到该区域的代表性特征图像,以便评估它们是否有用的特征。我该如何实施?
答案 0 :(得分:0)
在这里,我计算了此图像上补丁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;
}