简体   繁体   English

如何使用CvMat检索像素值

[英]How can retrieve pixel value using CvMat

img->data.ptr[i,j]=img1.data.ptr[(m_c*w_in)+n_c];

I tried this but it is showing me only one value. 我试过了,但这只显示了我一个价值。 Any help can be appreciated. 任何帮助,我们将不胜感激。

first of all why are you using the old interface. 首先,您为什么要使用旧界面。 If you have new opencv then convert the CvMat to cv::Mat and then do the operations. 如果您有新的opencv,则将CvMat转换为cv :: Mat,然后执行操作。 Once you are done then you can convert the Mat back to CvMat. 完成后,您可以将Mat转换回CvMat。

First of all, switch to cv::Mat Then, you have several ways to access pixel x,y: 首先,切换到cv::Mat然后,您可以通过多种方式访问​​像素x,y:

cv::Mat img;
int x,y;

//[...] Initialize here x and y

cv::Point p(x,y);
int stride = img.step1();

//All of these are valid ways to access pixel x,y
img.at<uint8_t>(y,x); //Or, for example, cv::Vec3b in place of uint8_t in case of color images
img.at<uint8_t>(p);
//The following are valid only for grayscale 8-bit images, otherwise they have to be modified a bit
img.ptr(y)[x];
img.ptr()[y * stride + x]; 

In fact, once you switch to cv::Mat you can find other extensive answers here OpenCV get pixel channel value from Mat image and here Accessing certain pixel RGB value in openCV 实际上,一旦切换到cv :: Mat,您就可以找到其他广泛的答案,在这里OpenCV从Mat图像获取像素通道值 ,在这里访问openCV中的某些像素RGB值

This is an old question, just for anybody who do not have the luxury to use the newer cv:mat format, and must use cvmat to access pixel. 这是一个古老的问题,对于那些不愿意使用较新的cv:mat格式并且必须使用cvmat来访问像素的人来说。 Tested using OpenCV 1.1. 使用OpenCV 1.1测试。

static unsigned long get_color(IplImage *img, CvPoint* pt, double *luma) {
    uchar blue, green, red;
    unsigned long color = 0;
    CvMat hdr; 
    CvMat *mat = cvGetMat(img, &hdr);
    int col = mat->step / mat->cols;
    uchar *pix = mat->data.ptr + (pt->y * mat->step + pt->x * col);

    if (col == 1) {
        // Grayscale
        color = *pix;
        blue = color * 11 / 100;
        green = color * 59 / 100;
        red = color * 30 / 100;
    } else if (col == 3) {
        // 3 channel RGB
        blue = *pix;
        green = *(pix + 1);
        red = *(pix + 2); 
        color = red << 16 | green << 8 | blue;
    } else {
        printf("Unsupported number of channel %d\n", col);
        return 0;
    }

    if (luma) 
        *luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

    printf("\n\nb=%x g=%x, r=%x color=%x\n", blue, green, red, color);
    printf("cols=%d, step=%d, col=%d, x=%d, y=%d loc=%d\n", 
           mat->cols, mat->step, col, pt->x, pt->y,
           (pt->y * mat->step + pt->x * col));
    return color;
}

Output: 输出:

1. Output from a grayscaled 600x600 Red.jpeg file
// Pixel (0,0)
b=8 g=2c, r=16 color=4c
cols=600, step=600, col=1, x=0, y=0 loc=0

// Pixel (1,0)
b=8 g=2c, r=16 color=4c
cols=600, step=600, col=1, x=1, y=0 loc=1

// Pixel (1,1)
b=8 g=2c, r=16 color=4c
cols=600, step=600, col=1, x=1, y=1 loc=601


2. Output from a 3 channel rgb 600x600 Red.jpeg file
// Pixel (0,0)
b=0 g=0, r=fe color=fe0000
cols=600, step=1800, col=3, x=0, y=0 loc=0

// Pixel (1,0)
b=0 g=0, r=fe color=fe0000
cols=600, step=1800, col=3, x=1, y=0 loc=3

// Pixel (1,1)
cols=600, step=1800, col=3, x=1, y=1 loc=1803
b=0 g=0, r=fe color=fe0000

for accessing data using CvMat you have to use "img->data.ptr[x*col+y]" it can used to store data of uchar. 要使用CvMat访问数据,必须使用“ img-> data.ptr [x * col + y]”,它可以用于存储uchar的数据。 CvMat also support double,float,string and integer type. CvMat还支持double,float,string和integer类型。 So you can store data according to your convince. 因此,您可以根据自己的确信来存储数据。

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

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