gpt4 book ai didi

c++ - OpenCV - 将 vector 的 vector 转换为 Mat

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:06 25 4
gpt4 key购买 nike

我想转换一个 vector<vector<double> >Mat因为我需要对此值应用自定义平滑过滤器。

下图显示了正确的值

This image shows the CORRECT values

我试过了。

std::vector<std::vector<double> > angles;
calculateAngles(angles);
Mat matAngles(angles.size(), angles.at(0).size(), CV_64FC1, angles.data());

但第一列中的值转换错误,值为 2.12566e-314。

生成的图像

result image

我还尝试将值直接放在 Mat 中.

void calculateAngles(cv::Mat& im, cv::Mat& angles, int blockSize, int(*f)(int x, int y), int(*g)(int x, int y)){

static int ySobel[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
static int xSobel[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};

angles.create(cv::Size(im.cols/blockSize+1, im.rows/blockSize+1), CV_64FC1);

int nominator;
int denominator;
int GX, GY;
double angle;
for(int i = 1; i < im.cols; i += blockSize){
for(int j = 1; j < im.rows; j += blockSize){
nominator = 0;
denominator = 0;
for(int k = i; k < std::min(i + blockSize, im.cols-1); k++){
for(int l = j; l < std::min(j + blockSize, im.rows-1); l++){
GX = applyKernelAt(im, xSobel, k, l);
GY = applyKernelAt(im, ySobel, k, l);

nominator += f(GX, GY);
denominator += g(GX, GY);
}
}

angle = (PI + std::atan2(nominator, denominator)) / 2;
angles.at<double>((i-1)/blockSize, (j-1)/blockSize) = angle;
}
}
}

但是值在某个点之后重复。

enter image description here

这是打印值的方式

void drawLines(cv::Mat& src, cv::Mat& dst, cv::Mat& angles, int blockSize){
cv::Size size = src.size();
dst.create(src.size(), src.type());

// for a white background
dst += 255;

cv::cvtColor(dst, dst, CV_GRAY2BGR);

int x1,y1,x2,y2;

for(int i = 1; i < size.width; i += blockSize){
for(int j = 1; j < size.height; j += blockSize){
double tang = std::tan(angles.at<double>((i-1)/blockSize,(j-1)/blockSize));

if( tang >= -1 && tang <= 1 ){
x1 = i;
y1 = (-blockSize/2) * tang + j + blockSize/2;
x2 = i + blockSize;
y2 = (blockSize/2) * tang + j + blockSize/2;
}else{
x1 = i + blockSize/2 + blockSize/(2*tang);
y1 = j + blockSize/2;
x2 = i + blockSize/2 - blockSize/(2*tang);
y2 = j -blockSize/2;
}

cv::line(dst, cv::Point(x1,y1), cv::Point(x2,y2), cv::Scalar(0,0,255));
}
}
}

最佳答案

这是一种从 vector 的 vector 填充 cv::Mat 的方法。

std::vector<std::vector<double> > angles;
cv::Mat matAngles(angles.size(), angles.at(0).size(), CV_64FC1);
for(int i=0; i<matAngles.rows; ++i)
for(int j=0; j<matAngles.cols; ++j)
matAngles.at<double>(i, j) = angles.at(i).at(j);

关于c++ - OpenCV - 将 vector 的 vector 转换为 Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com