简体   繁体   English

OpenCV:.ptr <uchar> 与.ptr <Vec3b> 在灰度图像中

[英]OpenCV: .ptr<uchar> vs .ptr<Vec3b> in grayscale images

My main aim is to access the pointers properly in a single channel grayscale image and passing them to Cuda kernel functions (such as for convolution, filtering etc). 我的主要目的是在单通道灰度图像中正确访问指针,并将其传递给Cuda内核函数(例如用于卷积,过滤等)。 But I can't figure out why I can't see memory adresses by using .ptr<uchar> for grayscale images . 但是我无法弄清楚为什么通过对灰度图像使用.ptr<uchar>看不到内存地址。 I have prepared a small example code for you to check. 我准备了一个小的示例代码供您检查。

In the code below, I'm using 2 methods to convert a color image (liquidmoon.jpeg) to single channel 8bit gray-scale image. 在下面的代码中,我使用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";

} 

Using: 使用:

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

And the program output is: 程序输出为:

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

What I expect was to obtain memory addresses with .ptr<uchar>(0) (at least for the first method, since it has 1 channel), but interestingly .ptr<Vec3b>(0) gives results for the both conditions. 我期望的是使用.ptr<uchar>(0)获得内存地址(至少对于第一种方法,因为它具有1个通道),但是有趣的是.ptr<Vec3b>(0)给出了这两种情况的结果。 Are those images not grayscale yet? 这些图像不是灰度图像吗? What may be the problem? 可能是什么问题?

It's just that gimg.ptr<uchar> returns an uchar* which cout interprets as a pointer to a C-string and attempts to display as such. 只是gimg.ptr<uchar>返回一个uchar*cout会将其解释为指向C字符串的指针,并尝试以此形式显示。 Cast to a void* pointer first. 首先强制转换为void*指针。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM