如何可视化提取的特征
How To Visualize Extracted Features
我使用opencv249和Visual Studio2013提取了一张图片中四个区域的特征,代码如下:
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;
}
我希望看到该区域的代表性特征图像,以便评估特征是否有用。我该如何实施?
我在此图像上计算了 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;
}
我使用opencv249和Visual Studio2013提取了一张图片中四个区域的特征,代码如下:
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;
}
我希望看到该区域的代表性特征图像,以便评估特征是否有用。我该如何实施?
我在此图像上计算了 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;
}