OpenCV Error: Assertion failed (src.size() == dst.size() &&src.channels<>==dst.c

直上云霄 2012-09-02 03:27:02
OpenCV Error: Assertion failed (src.size() == dst.size() &&src.channels<>==dst.channels<>>in unknown function
一直找不到解决方法,跪求解决方案



下面是代码:

#include "cvut.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace cvut;
using namespace std;

#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")

void main() {
ifstream fin(
"C:\\Users\\dzy\\Documents\\Visual Studio 2008\\Projects\\opencv\\Debug\\1.txt"); /* 定标所用图像文件的路径 *///修改路径
ofstream fout("C:\\Users\\dzy\\Documents\\Visual Studio 2008\\Projects\\opencv\\Debug\\2.txt"); /* 保存定标结果的文件 *///修改路径

/************************************************************************
读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
*************************************************************************/
cout<<"开始提取角点………………";
int image_count=0; /* 图像数量 */
CvSize image_size; /* 图像的尺寸 */
CvSize board_size = cvSize(5,7); /* 定标板上每行、列的角点数 *///修改角点数
CvPoint2D32f* image_points_buf = new CvPoint2D32f[board_size.width*board_size.height]; /* 缓存每幅图像上检测到的角点 */
Seq<CvPoint2D32f> image_points_seq; /* 保存检测到的所有角点 */

string filename;
while (getline(fin,filename))
{
cout<<"\n 将鼠标焦点移到标定图像所在窗口 并输入回车进行下一幅图像的角点提取 \n";

image_count++;
int count;
Image<uchar> view(filename);
if (image_count == 1) {
image_size.width = view.size().width;
image_size.height = view.size().height;
}
/* 提取角点 */
if (0 == cvFindChessboardCorners( view.cvimage, board_size,
image_points_buf, &count, CV_CALIB_CB_ADAPTIVE_THRESH ))
{
cout<<"can not find chessboard corners!\n";
exit(1);
} else {
Image<uchar> view_gray(view.size(),8,1);
rgb2gray(view,view_gray);
/* 亚像素精确化 */
cvFindCornerSubPix( view_gray.cvimage, image_points_buf, count, cvSize(11,11),
cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
image_points_seq.push_back(image_points_buf,count);
/* 在图像上显示角点位置 */
cvDrawChessboardCorners( view.cvimage, board_size, image_points_buf, count, 1);
view.show("calib");
cvWaitKey();
view.close();
}


}
delete []image_points_buf;
cout<<"角点提取完成!\n";

/************************************************************************
摄像机定标
*************************************************************************/
cout<<"开始定标………………";
CvSize square_size = cvSize(5,7); /* 实际测量得到的定标板上每个棋盘格的大小 */
Matrix<double> object_points(1,board_size.width*board_size.height*image_count,3); /* 保存定标板上角点的三维坐标 */
Matrix<double> image_points(1,image_points_seq.cvseq->total,2); /* 保存提取的所有角点 */
Matrix<int> point_counts(1,image_count,1); /* 每幅图像中角点的数量 */
Matrix<double> intrinsic_matrix(3,3,1); /* 摄像机内参数矩阵 */
Matrix<double> distortion_coeffs(1,4,1); /* 摄像机的4个畸变系数:k1,k2,p1,p2 */
Matrix<double> rotation_vectors(1,image_count,3); /* 每幅图像的旋转向量 */
Matrix<double> translation_vectors(1,image_count,3); /* 每幅图像的平移向量 */


/* 初始化定标板上角点的三维坐标 */
int i,j,t;
for (t=0;t<image_count;t++) {
for (i=0;i<board_size.height;i++) {
for (j=0;j<board_size.width;j++) {
/* 假设定标板放在世界坐标系中z=0的平面上 */
object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,0) = i*square_size.width;
object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,1) = j*square_size.height;
object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,2) = 0;
}
}
}

/* 将角点的存储结构转换成矩阵形式 */
for (i=0;i<image_points_seq.cvseq->total;i++) {
image_points(0,i,0) = image_points_seq[i].x;
image_points(0,i,1) = image_points_seq[i].y;
}

/* 初始化每幅图像中的角点数量,这里我们假设每幅图像中都可以看到完整的定标板 */
for (i=0;i<image_count;i++)
point_counts(0,i) = board_size.width*board_size.height;

/* 开始定标 */
cvCalibrateCamera2(object_points.cvmat,
image_points.cvmat,
point_counts.cvmat,
image_size,
intrinsic_matrix.cvmat,
distortion_coeffs.cvmat,
rotation_vectors.cvmat,
translation_vectors.cvmat,
0);
cout<<"定标完成!\n";

/************************************************************************
对定标结果进行评价
*************************************************************************/
cout<<"开始评价定标结果………………\n";
double total_err = 0.0; /* 所有图像的平均误差的总和 */
double err = 0.0; /* 每幅图像的平均误差 */
Matrix<double> image_points2(1,point_counts(0,0,0),2); /* 保存重新计算得到的投影点 */

cout<<"\t每幅图像的定标误差:\n";
fout<<"每幅图像的定标误差:\n";
for (i=0;i<image_count;i++) {
/* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
cvProjectPoints2(object_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvmat,
rotation_vectors.get_col(i).cvmat,
translation_vectors.get_col(i).cvmat,
intrinsic_matrix.cvmat,
distortion_coeffs.cvmat,
image_points2.cvmat,
0,0,0,0);
/* 计算新的投影点和旧的投影点之间的误差*/
err = cvNorm(image_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvmat,
image_points2.cvmat,
CV_L1);
total_err += err/=point_counts(0,0,0);
cout<<"\t\t第"<<i+1<<"幅图像的平均误差:"<<err<<"像素"<<'\n';
fout<<"\t第"<<i+1<<"幅图像的平均误差:"<<err<<"像素"<<'\n';
}
cout<<"\t总体平均误差:"<<total_err/image_count<<"像素"<<'\n';
fout<<"总体平均误差:"<<total_err/image_count<<"像素"<<'\n'<<'\n';
cout<<"评价完成!\n";

/************************************************************************
保存定标结果
*************************************************************************/
cout<<"开始保存定标结果………………";
Matrix<double> rotation_vector(3,1); /* 保存每幅图像的旋转向量 */
Matrix<double> rotation_matrix(3,3); /* 保存每幅图像的旋转矩阵 */

fout<<"相机内参数矩阵:\n";
fout<<intrinsic_matrix<<'\n';
fout<<"畸变系数:\n";
fout<<distortion_coeffs<<'\n';
for (i=0;i<image_count;i++) {
fout<<"第"<<i+1<<"幅图像的旋转向量:\n";
fout<<rotation_vectors.get_col(i);
/* 对旋转向量进行存储格式转换 */
for (j=0;j<3;j++) {
rotation_vector(j,0,0) = rotation_vectors(0,i,j);
}
/* 将旋转向量转换为相对应的旋转矩阵 */
cvRodrigues2(rotation_vector.cvmat,rotation_matrix.cvmat);
fout<<"第"<<i+1<<"幅图像的旋转矩阵:\n";
fout<<rotation_matrix;
fout<<"第"<<i+1<<"幅图像的平移向量:\n";
fout<<translation_vectors.get_col(i)<<'\n';
}
cout<<"完成保存\n";
}
...全文
1440 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LorryXY_* 2013-11-28
  • 打赏
  • 举报
回复
话说 的确是用了vc6.0+opencv1.0就是0 error 0 warning 换成vs2008+opencv2.4.4就是一大堆的warning和n个error 修改完error 还有LZ说的错误 揪心哈!
从来不作 2013-07-10
  • 打赏
  • 举报
回复
真的是版本问题吗?我用2.2的表示有点压力啊
wuxiao0313 2013-01-05
  • 打赏
  • 举报
回复
不知道为什么1.0以后版本都不行 1.1prea和2.3.1都试验过
直上云霄 2012-09-03
  • 打赏
  • 举报
回复
问题已解决,找了很长时间才发现需要vc++6.0和opencv1.0才能顺利运行
遇到同样问题的朋友可以参考下
直上云霄 2012-09-02
  • 打赏
  • 举报
回复
发帖比较少,不太会弄,多谢楼上的修改
whucv 2012-09-02
  • 打赏
  • 举报
回复
你的 src 和dst ,不匹配
1.check这两个图或者矩阵是不是初始化了
2.如果初始化了,初始化的大小是不是一样,即两者有没有相同的长宽
3.两者的depth深度是不是一样,即是不是一个是三通道的,一个是单通道的。
或者一个是单通道32位浮点类型,一个是单通道8位整形类型
whucv 2012-09-02
  • 打赏
  • 举报
回复 1
看着就很不舒服
#include "cvut.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace cvut;
using namespace std;

#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")

void main() {
ifstream fin(
"C:\\Users\\dzy\\Documents\\Visual Studio 2008\\Projects\\opencv\\Debug\\1.txt"); /* 定标所用图像文件的路径 *///修改路径
ofstream fout("C:\\Users\\dzy\\Documents\\Visual Studio 2008\\Projects\\opencv\\Debug\\2.txt"); /* 保存定标结果的文件 *///修改路径

/************************************************************************
读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
*************************************************************************/
cout<<"开始提取角点………………";
int image_count=0; /* 图像数量 */
CvSize image_size; /* 图像的尺寸 */
CvSize board_size = cvSize(5,7); /* 定标板上每行、列的角点数 *///修改角点数
CvPoint2D32f* image_points_buf = new CvPoint2D32f[board_size.width*board_size.height]; /* 缓存每幅图像上检测到的角点 */
Seq<CvPoint2D32f> image_points_seq; /* 保存检测到的所有角点 */

string filename;
while (getline(fin,filename))
{
cout<<"\n 将鼠标焦点移到标定图像所在窗口 并输入回车进行下一幅图像的角点提取 \n";

image_count++;
int count;
Image<uchar> view(filename);
if (image_count == 1) {
image_size.width = view.size().width;
image_size.height = view.size().height;
}
/* 提取角点 */
if (0 == cvFindChessboardCorners( view.cvimage, board_size,
image_points_buf, &count, CV_CALIB_CB_ADAPTIVE_THRESH ))
{
cout<<"can not find chessboard corners!\n";
exit(1);
} else {
Image<uchar> view_gray(view.size(),8,1);
rgb2gray(view,view_gray);
/* 亚像素精确化 */
cvFindCornerSubPix( view_gray.cvimage, image_points_buf, count, cvSize(11,11),
cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
image_points_seq.push_back(image_points_buf,count);
/* 在图像上显示角点位置 */
cvDrawChessboardCorners( view.cvimage, board_size, image_points_buf, count, 1);
view.show("calib");
cvWaitKey();
view.close();
}


}
delete []image_points_buf;
cout<<"角点提取完成!\n";

/************************************************************************
摄像机定标
*************************************************************************/
cout<<"开始定标………………";
CvSize square_size = cvSize(5,7); /* 实际测量得到的定标板上每个棋盘格的大小 */
Matrix<double> object_points(1,board_size.width*board_size.height*image_count,3); /* 保存定标板上角点的三维坐标 */
Matrix<double> image_points(1,image_points_seq.cvseq->total,2); /* 保存提取的所有角点 */
Matrix<int> point_counts(1,image_count,1); /* 每幅图像中角点的数量 */
Matrix<double> intrinsic_matrix(3,3,1); /* 摄像机内参数矩阵 */
Matrix<double> distortion_coeffs(1,4,1); /* 摄像机的4个畸变系数:k1,k2,p1,p2 */
Matrix<double> rotation_vectors(1,image_count,3); /* 每幅图像的旋转向量 */
Matrix<double> translation_vectors(1,image_count,3); /* 每幅图像的平移向量 */


/* 初始化定标板上角点的三维坐标 */
int i,j,t;
for (t=0;t<image_count;t++) {
for (i=0;i<board_size.height;i++) {
for (j=0;j<board_size.width;j++) {
/* 假设定标板放在世界坐标系中z=0的平面上 */
object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,0) = i*square_size.width;
object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,1) = j*square_size.height;
object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,2) = 0;
}
}
}

/* 将角点的存储结构转换成矩阵形式 */
for (i=0;i<image_points_seq.cvseq->total;i++) {
image_points(0,i,0) = image_points_seq[i].x;
image_points(0,i,1) = image_points_seq[i].y;
}

/* 初始化每幅图像中的角点数量,这里我们假设每幅图像中都可以看到完整的定标板 */
for (i=0;i<image_count;i++)
point_counts(0,i) = board_size.width*board_size.height;

/* 开始定标 */
cvCalibrateCamera2(object_points.cvmat,
image_points.cvmat,
point_counts.cvmat,
image_size,
intrinsic_matrix.cvmat,
distortion_coeffs.cvmat,
rotation_vectors.cvmat,
translation_vectors.cvmat,
0);
cout<<"定标完成!\n";

/************************************************************************
对定标结果进行评价
*************************************************************************/
cout<<"开始评价定标结果………………\n";
double total_err = 0.0; /* 所有图像的平均误差的总和 */
double err = 0.0; /* 每幅图像的平均误差 */
Matrix<double> image_points2(1,point_counts(0,0,0),2); /* 保存重新计算得到的投影点 */

cout<<"\t每幅图像的定标误差:\n";
fout<<"每幅图像的定标误差:\n";
for (i=0;i<image_count;i++) {
/* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
cvProjectPoints2(object_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvmat,
rotation_vectors.get_col(i).cvmat,
translation_vectors.get_col(i).cvmat,
intrinsic_matrix.cvmat,
distortion_coeffs.cvmat,
image_points2.cvmat,
0,0,0,0);
/* 计算新的投影点和旧的投影点之间的误差*/
err = cvNorm(image_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvmat,
image_points2.cvmat,
CV_L1);
total_err += err/=point_counts(0,0,0);
cout<<"\t\t第"<<i+1<<"幅图像的平均误差:"<<err<<"像素"<<'\n';
fout<<"\t第"<<i+1<<"幅图像的平均误差:"<<err<<"像素"<<'\n';
}
cout<<"\t总体平均误差:"<<total_err/image_count<<"像素"<<'\n';
fout<<"总体平均误差:"<<total_err/image_count<<"像素"<<'\n'<<'\n';
cout<<"评价完成!\n";

/************************************************************************
保存定标结果
*************************************************************************/
cout<<"开始保存定标结果………………";
Matrix<double> rotation_vector(3,1); /* 保存每幅图像的旋转向量 */
Matrix<double> rotation_matrix(3,3); /* 保存每幅图像的旋转矩阵 */

fout<<"相机内参数矩阵:\n";
fout<<intrinsic_matrix<<'\n';
fout<<"畸变系数:\n";
fout<<distortion_coeffs<<'\n';
for (i=0;i<image_count;i++) {
fout<<"第"<<i+1<<"幅图像的旋转向量:\n";
fout<<rotation_vectors.get_col(i);
/* 对旋转向量进行存储格式转换 */
for (j=0;j<3;j++) {
rotation_vector(j,0,0) = rotation_vectors(0,i,j);
}
/* 将旋转向量转换为相对应的旋转矩阵 */
cvRodrigues2(rotation_vector.cvmat,rotation_matrix.cvmat);
fout<<"第"<<i+1<<"幅图像的旋转矩阵:\n";
fout<<rotation_matrix;
fout<<"第"<<i+1<<"幅图像的平移向量:\n";
fout<<translation_vectors.get_col(i)<<'\n';
}
cout<<"完成保存\n";
}
感谢您了解学习【英特尔OpenVINO™工具套件】系列课程,为了能给您提供更好的课程体验,现诚邀您花费2分钟的时间填写关于本课程的调查问卷。我们将在填写问卷的同学中抽取5名幸运的小伙伴儿,每人赠送一张价值99元的CSDN·VIP月卡,感谢您的参与!问卷地址:https://t.csdnimg.cn/07Qv 英特尔® OpenVINO™工具套件中级课程面向有一定基础的学员。若您是一名计算视觉技术的初学者,我们将建议您先学习英特尔® OpenVINO™工具套件的初级课程,再进行中级课程的学习。本课程将主要介绍计算机视觉应用的相关知识,特别是英特尔® OpenVINO™工具套件的整体架构以及使用方法。整个课程的视频课程部分包含了OpenVINO™模型优化器和推理引擎的使用,视频解码的OpenCV,MediaSDK和Gstreamer的使用,AI应用中的推理优化,以及构建一套完整的视频推理AI应用的Demo演示。并且课程提供了动手实验环节,届时您将使用一个虚拟云终端进行操作实验。通过本课程的学习,将帮助您快速上手英特尔® OpenVINO™ 工具套件的使用方法,并且能够熟悉如何去快速构建一款AI应用。为保证您顺利收听课程参与测试获取证书,还请您使用电脑端进行课程学习!为了便于您更好的学习本次课程,推荐您在本地下载英特尔® OpenVINO™工具套件,下载地址:https://t.csdnimg.cn/yOf5Intel®Devcloud注册地址:https://devcloud.intel.com/收听中级课程并完成动手实验,可获得专属定制证书,还可以参与定制周边的抽奖活动! 初级课程学习:https://edu.csdn.net/course/detail/27685 请注意:点击报名即表示您确认您已年满18周岁,并且同意CSDN基于商务需求收集并使用您的个人信息,用于注册OpenVINO™工具套件及其课程。CSDN和英特尔会为您定制最新的科学技术和行业信息,将通过邮件或者短信的形式推送给您,您也可以随时取消订阅不再从CSDN或Intel接收此类信息。 查看更多详细信息请点击CSDN“用户服务协议”,英特尔“隐私声明”和“使用条款”。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧