【问题标题】:OpenCV: .ptr<uchar> vs .ptr<Vec3b> in grayscale imagesOpenCV:灰度图像中的 .ptr<uchar> 与 .ptr<Vec3b>
【发布时间】:2015-03-24 11:21:19
【问题描述】:

我的主要目标是在单通道灰度图像中正确访问指针并将它们传递给 Cuda 内核函数(例如用于卷积、过滤等)。但我无法弄清楚为什么我无法通过使用.ptr&lt;uchar&gt; 来查看灰度图像的内存地址。我准备了一个小示例代码供您查看。

在下面的代码中,我使用 2 种方法将彩色图像 (liquidmoon.jpeg) 转换为单通道 8 位灰度图像。

#include <cuda.h>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(void)
{
    //Method 1
    Mat img1 = imread("liquidmoon.jpeg",CV_LOAD_IMAGE_GRAYSCALE);
    cout <<"Number of channels in the first converted image : " << img1.channels() << "\n";
    cout << "ptr with uchar : "<< img1.ptr<uchar>(0)<<"\n";
    cout << "ptr with Vec3b : "<< img1.ptr<Vec3b>(0)<<"\n";

    //Method 2
    Mat img2 = imread("liquidmoon.jpeg");
    Mat gimg;
    img2.convertTo(gimg,CV_8UC1);
    cout <<"Number of channels in the second converted image : " << gimg.channels() << "\n";
    cout << "ptr with uchar : "<< gimg.ptr<uchar>(0)<<"\n";
    cout << "ptr with Vec3b : "<< gimg.ptr<Vec3b>(0)<<"\n";

} 

使用:

nvcc -o testCode testCode.cu `pkg-config opencv --cflags --libs`

而程序输出为:

Number of channels in the first converted image : 1
ptr with uchar : 
ptr with Vec3b : 0x1093000
Number of channels in the second converted image : 3
ptr with uchar : 
ptr with Vec3b : 0x10eaea0

我期望的是使用.ptr&lt;uchar&gt;(0) 获得内存地址(至少对于第一种方法,因为它有一个通道),但有趣的是.ptr&lt;Vec3b&gt;(0) 给出了两种条件的结果。这些图像还不是灰度的吗?可能是什么问题?

【问题讨论】:

    标签: image opencv cuda grayscale


    【解决方案1】:

    只是gimg.ptr&lt;uchar&gt; 返回一个uchar*cout 将其解释为指向 C 字符串的指针并尝试显示为这样。首先转换为void* 指针。

    cout << "ptr with uchar : "<< static_cast<void const*>(img1.ptr<uchar>(0)) <<"\n";
    

    【讨论】:

    • 但是为什么gimg中的通道数是3?
    • 现在我明白了。谢谢。
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2021-04-06
    相关资源
    最近更新 更多