使用step方法检索图像的bgr值的代码有什么问题
What is wrong with this code to retrieve bgr values of an image using the step method
这是我从信誉良好的来源获得的代码,但它不起作用:
Mat img = imread("/home/w/d1",CV_LOAD_IMAGE_COLOR);
unsigned char *input = (unsigned char*)(img.data);
int i,j,r,g,b;
for(int i = 0;i < img.rows ;i++){
for(int j = 0;j < img.cols ;j++){
b = input[img.step * j + i ] ;
g = input[img.step * j + i + 1];
r = input[img.step * j + i + 2];
cout << b << g <<r;
}
}
当我运行它时,输出与我做 cout <<img 时不同;我认为这可能与新的C++界面有关。如果是这样并且我应该使用 step1 方法,有人可以告诉我如何使用 step1 方法更新我的代码以访问 BGR 值。我找不到有关如何使用步骤1的在线文档。提前感谢任何帮助。
你的代码看起来基本上是正确的,但据我所知,你的排序是错误的。您必须访问
value = data[img.step*ROW + COL]
但您切换了行和列。
编辑:此外,您需要将 COL 乘以通道数:
value = data[img.step*ROW + #channels*COL + currentChannel]
尝试:
Mat img = imread("/home/w/d1",CV_LOAD_IMAGE_COLOR);
unsigned char *input = (unsigned char*)(img.data);
int i,j,r,g,b;
for(int i = 0;i < img.rows ;i++){
for(int j = 0;j < img.cols ;j++){
b = input[img.step * i + 3*j ] ; // 3 == img.channels()
g = input[img.step * i + 3*j + 1];
r = input[img.step * i + 3*j + 2];
cout << b << g <<r;
}
}
也许你想避免使用"原始数据"方法:
for(int i=0; i<img.rows; i++) {
for(int j=0; j<img.cols; j++) {
Vec3b pix = img.at<Vec3b>(i,j);
cout << int(pix[0]) << int(pix[1]) << int(pix[2]) << endl;
}
}
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 0-1背包代码中的错误.我的代码中有什么错误
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 这行代码在C++类中意味着什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 以下 C++ 代码用于 -> "#define idiv(a, b) (((a) + (b) / 2) / (b))" 是什么?
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 此代码中的操作流程是什么?C/C++.
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 此代码验证公式是什么意思?
- 需要以下代码的帮助,下面的代码有什么问题
- C++标准提案代码:什么是 N 和 P?
- C - 哈希算法代码什么都没有返回
- 将"12345678"转换为"1-234-5678",我的代码什么也没产生
- 在编译过程中,琐碎的(没有效果的)代码什么时候会被删除
- 有人能告诉我为什么这个代码什么都不打印吗