CV_32FC1 cvMat に SVD を適用し、「u」コンポーネントの値をいくつか変更しました。ここで、'u、'w'、および 'vt' コンポーネントを乗算して単一の行列 A を取得しようとしていますが、OpenCV は次のエラーで行列を乗算できません。
OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in gemm, file /build/buildd/opencv-2.1.0/src/cxcore/cxmatmul.cpp, line 687
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.1.0/src/cxcore/cxmatmul.cpp:687: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) in function gemm
SVD オブジェクトの行列の型を調べたところ、型 = 20 のように見えますが、これは既定の行列の型のいずれとも一致しません。
#include <iostream>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "constants.h"
const unsigned int MAX = 10000;
using namespace cv;
using namespace std;
int NO_FRAMES;
Mat resize_image(Mat &src, Mat img)
{
Mat dst;
resize(src, dst, Size(img.rows, img.cols));
return dst;
}
bool check_exit()
{
return (waitKey(27) > 0)?true:false;
}
int main(int argc, char ** argv)
{
Mat rgb[MAX];
Mat ycbcr[MAX];
Mat wm_rgb[MAX];
SVD svd[MAX];
namedWindow("watermark",1);
namedWindow("RGB", 2);
namedWindow("YCBCR",3);
namedWindow("u",4);
namedWindow("w",5);
namedWindow("vt",6);
if (argc < 3)
{
cout<<"Video file required! (Supported formats: avi, mp4, mpeg)\n";
return 1;
}
VideoCapture capture(argv[1]);
Mat watermark = imread(argv[2]);
if(!capture.isOpened())
{
cout<<"Unable to open the video file!\n";
return 1;
}
int i=0;
capture >> rgb[i];
while(!rgb[i].empty())
{
imshow("RGB", rgb[i]);
cvtColor(rgb[i], ycbcr[i], CV_RGB2YCrCb);
imshow("YCBCR", ycbcr[i]);
i++;
capture >> rgb[i];
if(check_exit())
exit(0);
}
NO_FRAMES = i;
watermark = resize_image(watermark, ycbcr[0]);
VideoWriter writer("output.avi", CV_FOURCC('d', 'i', 'v', 'x'), 24.0, cvSize(ycbcr[0].cols, ycbcr[0].rows), true);
for(int i = 0; i < NO_FRAMES - 1; i++)
{
Mat dst(ycbcr[i].rows, ycbcr[i].cols, CV_32FC1);
ycbcr[i].convertTo(dst, CV_32S);
SVD temp(dst, 5);
imshow("u", temp.u);
imshow("w", temp.w);
imshow("vt", temp.vt);
svd[i] = temp;
if(check_exit())
exit(0);
}
int j = 0, k = 0;
for (i = 0; i < NO_FRAMES; i++)
{
for (int p = 0; p < svd[i].u.rows; p++)
{
for(int q = 0; q < svd[i].u.cols; q++)
{
if (p == q)
{
if (j >= watermark.rows)
{
goto x;
}
if (k >= watermark.cols)
{
k = 0;
j++;
}
svd[i].u.at<float>(p, q) = watermark.at<float>(j,k);
k++;
}
}
}
}
x: for (i = 0; i < NO_FRAMES; i++)
{
Mat A(rgb[0].rows, rgb[0].cols, CV_32FC1);
//Here
A = svd[i].u * svd[i].w * svd[i].vt;
Mat B(A.rows, A.cols, CV_32FC1);
cvtColor(A, B, CV_YCrCb2RGB);
writer << B;
}
capture.release();
return 0;
}