0
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.

Antonio
  • 19,451
  • 13
  • 99
  • 197

4 Answers4

1

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.

Anubhav Rohatgi
  • 458
  • 1
  • 6
  • 16
1

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

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
0

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
-1

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.

  • `x*col` is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable `cols`, not `col`). I do not know where to find `CvMat` documentation so I wouldn't know where the stride value is saved (in my answer there's an example for `cv::Mat`), maybe in the field `step`. – Antonio Feb 26 '15 at 10:41
  • The conversion between `CvMat` and `cv::Mat` is [so straightforward](http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=cvmat#mat) (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it [here](http://docs.opencv.org/modules/core/doc/old_basic_structures.html#CvMat). Indeed the stride is in the `step` field. Note that `stride != cols` can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment. – Antonio Feb 26 '15 at 10:51