简体   繁体   中英

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. Once you are done then you can convert the Mat back to CvMat.

First of all, switch to cv::Mat Then, you have several ways to access pixel 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

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. Tested using 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 also support double,float,string and integer type. So you can store data according to your convince.

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.

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