提取单个像素数据的最快方法
Fastest way to extract individual pixel data?
我必须使用OpenCV获得关于灰度图像上许多像素的标量值的信息。它将遍历数十万像素,所以我需要最快的方法。我在网上找到的所有其他来源都很神秘,很难理解。有没有一行简单的代码应该只提供一个简单的整数值,表示图像的第一个通道(亮度)的标量值?
for (int row=0;row<image.height;row++) {
unsigned char *data = image.ptr(row);
for (int col=0;col<image.width;col++) {
// then use *data for the pixel value, assuming you know the order, RGB etc
// Note 'rgb' is actually stored B,G,R
blue= *data++;
green = *data++;
red = *data++;
}
}
您需要在每一个新行上获得数据指针,因为opencv将在每一行的开头将数据填充到32位边界
关于Martin的帖子,您实际上可以使用OpenCV的Mat对象中的isContinuous()方法来检查内存是否连续分配。以下是一个常见的习惯用法,用于确保外部循环在可能的情况下只循环一次:
#include <opencv2/core/core.hpp>
using namespace cv;
int main(void)
{
Mat img = imread("test.jpg");
int rows = img.rows;
int cols = img.cols;
if (img.isContinuous())
{
cols = rows * cols; // Loop over all pixels as 1D array.
rows = 1;
}
for (int i = 0; i < rows; i++)
{
Vec3b *ptr = img.ptr<Vec3b>(i);
for (int j = 0; j < cols; j++)
{
Vec3b pixel = ptr[j];
}
}
return 0;
}
相关文章:
- 通过JNI传递数据数组的最快方法是什么
- 如何使用curlpp通过POST方法上传文件和json数据
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 将线程中的数据存储到全局容器的最佳方法?
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- 在C++中查找像素 RGB 数据的最快方法是什么?
- 构造智能点数据类型以及普通数据类型的通用方法
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- 在 c++ 中解析数据包数据的最佳方法是什么?
- 处理编译器关于可能丢失数据的警告的最优雅方法是什么
- 创建异构顶点数据数组的可移植方法
- 使用 memcpy 将矢量数据复制到 wstring 的正确方法
- 在 std::vector<无符号字符中存储任意数据的方法>
- 在C++事务之间存储大量字符数据的有效方法
- 学习数据结构和算法的简单方法
- C++中的多维数据集:从 std::vector 的 2D 数据到 std::vector 的 2D 网格的最干净方法?
- C++ 使用派生类方法更改基类数据成员
- 类方法 - 数据结构中 For 循环的运行时错误