如何解决OpenCV / C ++-分段错误:mat乘法后为11
我是OpenCV的新手,正在尝试将代码从Matlab复制到C ++
并有细分错误:11进行矩阵乘法时。
我发现这可能是由C ++代码中的trans*U
引起的。
trans
的大小为16032768x3(行x col),U
的大小为3x3,因此,我很确定可以将其相乘。
这是我的照片的链接: Photos
希望有人可以帮助我解决我的问题,谢谢!
这是我的C ++代码:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
int main(int argc,char const *argv[])
{
//import image
Mat_<double> img1,img2,img3;
img1 = imread("S1.jpg",IMREAD_GRAYSCALE);
img2 = imread("S2.jpg",IMREAD_GRAYSCALE);
img3 = imread("S3.jpg",IMREAD_GRAYSCALE);
//Push all the image it to one matrix for SVDecomp
Mat_<double> svd_use;
svd_use.push_back(img1.reshape(0,1));
svd_use.push_back(img2.reshape(0,1));
svd_use.push_back(img3.reshape(0,1));
Mat_<double> source,trans,B,W,U,VT;
trans = svd_use.t();
source = svd_use*trans;
SVDecomp(source,VT);
//To make sure the value is the same as matlab
W = (Mat_<double>(3,3) << W[0][0],W[1][0],W[2][0]);
U = (Mat_<double>(3,3) << -U[0][0],-U[0][1],U[0][2],-U[1][0],-U[1][1],U[1][2],-U[2][0],-U[2][1],U[2][2]);
B = trans*U;//<---this part causes the Segmentation fault: 11
return 0;
}
这是我的Matlab代码
%Import images
source_img1 = rgb2gray(imread('S1.JPG'));
source_img2 = rgb2gray(imread('S2.JPG'));
source_img3 = rgb2gray(imread('S3.JPG'));
%Vectorize images
img_vector1 = source_img1(:);
img_vector2 = source_img2(:);
img_vector3 = source_img3(:);
%Calculate SVD
t = double([img_vector1'; img_vector2'; img_vector3']);
[U,S,V] = svd(t*t');
B = t'*V*S^(-1/2);
此外,我对矩阵的功效提出了疑问:在Matlab中,我可以直接计算S^(1/2)
,
有什么办法可以在OpenCV中做同样的事情?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。