为什么断言在这里失败
Why does assertion fail here
当我创建CvMat *
时,为什么断言在这里失败?在cv::Mat中使用指针加载的图像不会发生这种情况。
struct RGB { unsigned char b, g, r; };
cv::Point p;
RGB *data;
CvMat* mat = cvCreateMat(300,300,CV_32FC1);
for( row = 0; row < mat->rows; ++row)
{
for ( col = 0; col < mat->cols; ++col)
{
p.x=row,p.y=col;
ERROR ----->>> assert((mat->step/mat->cols) == sizeof(RGB));
data = (RGB*)&mat->data;
data += p.y * mat->cols + p.x;
}
}
对于这段代码,断言不会失败:
IplImage * img=cvLoadImage("blah.jpg");
int row=0,col=0;
cv::Mat in(img);
cv::Mat *mat=∈
cv::Point p;
struct RGB { unsigned char b, g, r; };
RGB *data;
for( row = 0; row < mat->rows; ++row)
{
for ( col = 0; col < mat->cols; ++col)
{
p.x=row,p.y=col;
assert((mat->step/mat->cols) == sizeof(RGB));
data = (RGB*)&mat->data;
data += p.y * mat->cols + p.x;
printf("Row=%dxCol=%d b=%u g=%u r=%un",row,col,data->b,data->g,data->r);
wait_for_frame(1);
}
}
因为sizeof(RGB) != sizeof(float)
,也就是你在这里填入的矩阵:
CvMat* mat = cvCreateMat(300,300,CV_32FC1);
CV_32FC1
表示1个分量,32位浮点数。你可能需要CV_8UC3
。
如果使用,可以跳过整个IplImage
的痛苦cv::Mat img = cv::loadImage("blah.jpg");
此外,最好使用行ptr来遍历所有像素。
它会跳,所以你不用担心!
来自refman:
如果你需要处理2D数组的一整行,效率最高方法是先获得指向行的指针,然后使用C操作符[]
请注意,如果你正在加载数据中有"跳转"的大图像,你的代码将无法工作。在你的情况下
cv::Mat img = cv::loadImage("blah.jpg");
const cv::Mat& M = img;
for(int i = 0; i < rows; i++)
{
const Vec3b* Mi = M.ptr<Vec3b>(i);
for(int j = 0; j < cols; j++)
{
const Vec3b& Mij = Mi[j];
std::cout<<"Row="<<i<<"Col="<<j<<"t";
std::cout<<"b="<<Mij[0]<<" g="<<Mij[1]<<" r="<<Mij[2]<<std::endl;
}
}
是最快的正确方法。否则,您可以使用M.at<Vec3b>(i,j)
。
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 为什么thread_local变量在这里从未初始化?
- 为什么我必须在这里使用dynamic_cast
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 为什么 C++20 中的 [[可能]] 属性在这里引发警告?
- 我在这里正确传递参数了吗?
- 为什么gmp会在这里与"invalid next size"重新定位一起崩溃?
- 移动语义在这里如何工作?
- 如何在这里循环运行?
- 为什么枚举变量在这里是右值?
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- 试图美化这个Arduino代码[初学者在这里]
- 为什么模板类型推断在这里失败
- 为什么断言在这里失败
- 为什么模板参数演绎/替换在这里失败?
- shared_ptr隐式强制转换在这里失败,在那里成功