简体   繁体   中英

Set rows and cols for opencv image

I made a function to create a thresholding image. The input is in img and the output in out . This is my code:

void *tresholdImage (CvMat *img, CvMat *out) {
    for (int i = 0; i < img->rows; i++)
        {
        for (int j = 0; j < img->cols; j++)
            {
            double x;
            if (cvGetReal2D(img, i, j) < 128) x = 0; else x = 255;
            cvSetReal2D(out, i, j, x);
            }
        }
    }

When calling this function, the image in out should have the same size as the image in img . I want to automatically set out 's size the same as img 's. I add this before the first for :

out = cvCreateMat(img->rows, img->cols, img->type);

but it won't work; the compiler said Bad Flag in CvGetMat . But if I put it in the main function, it works. Anyone can help me ?

If you want to create the result image during this call, you need something like this:

CvMat * thresholdImage (CvMat *img) {
  CvMat *out = cvCreateMat(img->rows, img->cols, img->type);
  for (int i = 0; i < img->rows; i++)
    for (int j = 0; j < img->cols; j++) {
      double x;
      if (cvGetReal2D(img, i, j) < 128) x = 0; else x = 255;
      cvSetReal2D(out, i, j, x);
    }
  return out;
}

Then you'll use it as:

CvMat *out = thresholdImage(img);

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