簡體   English   中英

OpenCV:.ptr <uchar> 與.ptr <Vec3b> 在灰度圖像中

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

我的主要目的是在單通道灰度圖像中正確訪問指針,並將其傳遞給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)獲得內存地址(至少對於第一種方法,因為它具有1個通道),但是有趣的是.ptr<Vec3b>(0)給出了這兩種情況的結果。 這些圖像不是灰度圖像嗎? 可能是什么問題?

只是gimg.ptr<uchar>返回一個uchar*cout會將其解釋為指向C字符串的指針,並嘗試以此形式顯示。 首先強制轉換為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