【发布时间】:2015-03-24 11:21:19
【问题描述】:
我的主要目标是在单通道灰度图像中正确访问指针并将它们传递给 Cuda 内核函数(例如用于卷积、过滤等)。但我无法弄清楚为什么我无法通过使用.ptr<uchar> 来查看灰度图像的内存地址。我准备了一个小示例代码供您查看。
在下面的代码中,我使用 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<uchar>(0) 获得内存地址(至少对于第一种方法,因为它有一个通道),但有趣的是.ptr<Vec3b>(0) 给出了两种条件的结果。这些图像还不是灰度的吗?可能是什么问题?
【问题讨论】:
标签: image opencv cuda grayscale