c++ - 无法分配任何大于 255 OpenCV 的值
问题描述
我试图创建一个直方图,但我不能分配任何大于 255 的值,如果它超过示例 256,它将为 0,依此类推。我该如何解决?
histMatrix = Mat(nChannelSource, 256, CV_16SC1);
uchar* pRowHistMatrix = histMatrix.data;
for (int y = 0; y < nChannelSource; y++, pRowHistMatrix += histMatrix.step[0]) {
uchar* pColHistMatrix = pRowHistMatrix;
for (int x = 0; x < 256; x++, pColHistMatrix += histMatrix.step[1]) {
((signed short*)pColHistMatrix)[0] = 256;
}
}
pRowHistMatrix = histMatrix.data;
for (int y = 0; y < nChannelSource; y++, pRowHistMatrix += histMatrix.step[0]) {
uchar* pColHistMatrix = pRowHistMatrix;
for (int x = 0; x < 256; x++, pColHistMatrix += histMatrix.step[1]) {
std::cout << (int)pColHistMatrix[0] << " ";
}
}
解决方案
我为与我有相同错误的人提供解决方案
错误是 uchar* 只处理一个变量的 8 位,当我们将 n 字节移向我们必须为 sizeof(type) 分配时,这里是简短的
histMatrix = Mat(nChannelSource, 256, CV_16SC1);
signed short* pRowHistMatrix = (signed short*)histMatrix.data;
for (int y = 0; y < nChannelSource; y++, pRowHistMatrix += histMatrix.step[0] / sizeof(signed short)) {
signed short* pColHistMatrix = pRowHistMatrix;
for (int x = 0; x < 256; x++, pColHistMatrix += histMatrix.step[1] / sizeof(signed short)) {
pColHistMatrix[0] = 256;
}
}
推荐阅读
- javascript - 网站在本地呈现,但不在 Github 服务器上。RESTfulAPI 的问题
- python - 如何使用 Python 获取 UN Comtrade 数据?
- python - python如何比较元组和整数
- swift - 尝试使用 while 循环在 Swift 中制作星号三角形
- excel - Worksheet_Change 事件 - 重复检查,忽略空白
- vue.js - 向数组中添加项目会导致 nextTick "RangeError" 中的错误
- reactjs - 它可以做到这一点
以及如何(reactjs)? - python - 在列表和列表列表中查找 int
- firebase - Flutter:当 TextView 获得焦点时,屏幕上的键盘未打开
- node.js - 使用 npm 脚本作为“bin”