Riwa Ml
Riwa Ml

Reputation: 19

use cvSet2D in gray scale image in C language?

I want to set pixel value in given bmp image using CvSet2D but I want to access only the first value. Could you explain what the 4 arguments of CvSet2D are for?

For example:

#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char *argv[])
{
    IplImage* img = cvLoadImage("initial.bmp",1);

    int f = 123;
    cvSet2D(img, 1, 2, f); // here is the error
    //cvSet2D(img, 1, 2, cvScalar(f)); // error also this way

    return 0;
}

I get an error that says:

incompatible type for argument 4 of 'cvSet2D'   

I just need to set pixel value of a gray scale image, how con I do this?

Upvotes: 1

Views: 383

Answers (1)

Miki
Miki

Reputation: 41765

You can refer to this question.

C: void cvSet2D(CvArr* arr, int idx0, int idx1, CvScalar value)

The arguments are:

  • arr - Input array (i.e. the image)
  • idx0 – The first zero-based component of the element index (i.e. row)
  • idx1 – The second zero-based component of the element index (i.e column)
  • value – The assigned value

To know what cvScalar is, refer to this and

The cvScalar is simply a convenient container for 1, 2, 3 or 4 floating point values.

You should do something like:

uchar f = 123;
CvScalar scalar = cvGet2D(img, 1, 2);
scalar.val[0] = f;
cvSet2D(img, 1, 2, scalar);

Upvotes: 1

Related Questions