提取单个像素数据的最快方法?

发布于 2024-12-28 19:48:07 字数 133 浏览 0 评论 0原文

我必须使用 OpenCV 获取有关灰度图像上许多像素的标量值的信息。它将遍历数十万个像素,因此我需要尽可能最快的方法。我在网上找到的所有其他来源都非常神秘且难以理解。是否有一行简单的代码应该只传递一个简单的整数值,表示图像的第一个通道(亮度)的标量值?

I have to get information about the scalar value of a lot of pixels on a gray-scale image using OpenCV. It will be traversing hundreds of thousands of pixels so I need the fastest possible method. Every other source I've found online has been very cryptic and hard to understand. Is there a simple line of code that should just hand a simple integer value representing the scalar value of the first channel (brightness) of the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余罪 2025-01-04 19:48:07
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 位边界

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++;
    }
}

You need to get the data pointer on each new row because opencv will pad the data to 32bit boundary at the start of each row

淡墨 2025-01-04 19:48:07

关于Martin的帖子,您实际上可以使用OpenCV的Mat对象中的isContinously()方法来检查内存是否连续分配。以下是确保外循环仅循环一次(如果可能)的常见习惯用法:

#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;
}

With regards to Martin's post, you can actually check if the memory is allocated continuously using the isContinuous() method in OpenCV's Mat object. The following is a common idiom for ensuring the outer loop only loops once if possible:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文