我希望使用 C++/OpenCV 将图像的像素值归一化到 [0..1] 范围内。但是,当我使用 image *= 1./255 或 normalize 函数进行归一化时,像素值会向下舍入为零。我尝试将图像设置为键入 CV_32FC3

下面是我的代码:

  Mat image;
  image = imread(imageLoc, CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH);

  Mat tempImage;

  // (didn't work) tempImage *= 1./255;

  image.convertTo(tempImage, CV_32F, 3);
  normalize(image, tempImage, 0, 1, CV_MINMAX);

  int r = 100;
  int c = 150;

  uchar* ptr = (uchar*)(tempImage.data + r * tempImage.step);
  Vec3f tempVals;
  tempVals.val[0] =  ptr[3*c+1];
  tempVals.val[1] =  ptr[3*c+2];
  tempVals.val[2] =  ptr[3*c+3];
  cout<<" temp image - "<< tempVals << endl;

  uchar* ptr2 = (uchar*)(image.data + r * image.step);
  Vec3f imVals;
  imVals.val[0] =  ptr2[3*c+1];
  imVals.val[1] =  ptr2[3*c+2];
  imVals.val[2] =  ptr2[3*c+3];
  cout<<" image - "<< imVals << endl;

这会在控制台中产生以下输出:
temp image - [0, 0, 0]
image - [90, 78, 60]

最佳答案

你可以让 convertTo() 为你做规范化:

image.convertTo(tempImage, CV_32FC3, 1.f/255);

您将 3 传递给 convertTo() ,大概是作为 channel 计数,但这不是 the correct signature

关于c++ - 标准化介于 0 和 1 之间的像素值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17345967/

10-11 16:15