我正在尝试为视频中的每5帧提取一个直方图矩阵,以进行进一步处理。过程如下:
步骤5的输出是随时间变化的特征矩阵。
在第4步中,我想生成一个新的Mat,用于存储每一行中每一帧的直方图。在矩阵中,行数=帧数,列数= 16 + 4 + 4(箱数)。
我为第4步创建了一个函数(formatHistogramforSVD),但第3步和第4步的数据输出不同。只有向量的第一个元素可以正确传递到Mat矩阵中。
int main ()
{
string path = "";
Mat image;
VideoCapture capture(path);
if (!capture.isOpened())
{
cout << "could not open the VideoCapture !" << endl;
return -1;
}
for (int i = 0; i < 5; i++)
{
capture >> image;
// generate histogram and save into a vector of Mat
vector<Mat> histogramI = calculate_histogram(image);
// Reshape and stack histogram into a rowMatrix
Mat matrix_histograms = formatHistogramforSVD(histogramI);
cout << matrix_histograms << endl;
}
}
vector<Mat> calculate_histogram(Mat image)
{
cvtColor(image, hsvImage, COLOR_BGR2HSV);
vector<Mat> bgrPlanes;
split(hsvImage, bgrPlanes);
Mat hHist, sHist, vHist;
int hMax = 16, svMax = 4;
calcHist(&bgrPlanes[0], 1, 0, Mat(), hHist, 1, &hMax, &hRange);
calcHist(&bgrPlanes[1], 1, 0, Mat(), sHist, 1, &svMax, &svRange);
calcHist(&bgrPlanes[2], 1, 0, Mat(), vHist, 1, &svMax, &svRange);
vector<Mat> histogramI = { hHist, sHist, vHist };
return histogramI;
}
Mat formatHistogramforSVD(const vector<Mat> &data)
{
Mat dst(static_cast<int>(data.size()), data[0].rows*data[0].cols, CV_32F);
for (unsigned int i = 0; i < data.size(); i++)
{
Mat image_row = data[i].clone().reshape(1, 1);
Mat row_i = dst.row(i);
image_row.convertTo(row_i, CV_32F);
}
return dst;
}
我的代码输出
MAT中每行所需的输出 [1966,10,19,43,28,36,94,36,50,56,148,84,130,204,214,522,1829,746,933,315]
如何在步骤4之后为MAT正确生成数据?