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). But I can't figure out why I can't see memory adresses by using .ptr<uchar>
for grayscale images . 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.
#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. 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. Cast to a void*
pointer first.
cout << "ptr with uchar : "<< static_cast<void const*>(img1.ptr<uchar>(0)) <<"\n";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.