是否有线索来确定是否需要通过cvtcolor进行色彩空间转换

时间:2017-07-06 11:50:15

标签: c++ opencv rgb imread opencv3.2

我使用imread从磁盘上传图像(我认为BGR是cv :: imread的默认输出)。然后,我使用cvtcolor将图像从BGR(默认的OpenCV)转换为RGBA(默认的CUDA)。

我认为我应该确保图像已正确转换,因此我显示(并保存)转换后的图像。然而,图像颜色失真&当我使用imshow& imwrite。出于调试目的,我试图将转换恢复为图像debugImage&令人惊讶的是它显示正确。

  cv::Mat image;
  image = cv::imread("input_image.jpg", CV_LOAD_IMAGE_COLOR);
  if (image.empty()) {
        std::cerr << "Couldn't open file: " << filename << std::endl;
        exit(1);
  }
  int type1 = image.type(); //16 --> CV_8UC3
  cv::namedWindow("image window", cv::WINDOW_AUTOSIZE); 
  cv::imshow("image window", image);
  cv::waitKey(0);
  cv::destroyWindow("image window");

  cv::Mat imageRGBA1(image.rows, image.cols, CV_8UC4);
  cv::cvtColor(image, imageRGBA1, CV_BGR2RGBA); // BGR2RGBA DIDNOT WORKED!!
  int type2 = imageRGBA1.type(); // 24 --> CV_8UC4
  cv::namedWindow("imageRGBA1 window", cv::WINDOW_AUTOSIZE);
  cv::imshow("imageRGBA1 window", imageRGBA1); // BLUISH IMAGE
  cv::waitKey(0);
  cv::destroyWindow("imageRGBA1 window");

  cv::Mat debugImage(image.rows, image.cols, CV_8UC4);
  cv::cvtColor(imageRGBA1, debugImage, CV_RGBA2BGR);
  int debugtype = debugImage.type(); // 24 --> CV_8UC4
  cv::namedWindow("debugImage window", cv::WINDOW_AUTOSIZE); 
  cv::imshow("debugImage window", debugImage); // CORRECT IMAGE
  cv::waitKey(0);
  cv::destroyWindow("debugImage window");

当我假设从imread获得的图像是RGB图像时,转换有效:

cv::Mat image;
  image = cv::imread("input_image.jpg", CV_LOAD_IMAGE_COLOR);
  if (image.empty()) {
        std::cerr << "Couldn't open file: " << filename << std::endl;
        exit(1);
  }
  int type1 = image.type(); //16 --> CV_8UC3
  cv::namedWindow("image window", cv::WINDOW_AUTOSIZE); 
  cv::imshow("image window", image);
  cv::waitKey(0);
  cv::destroyWindow("image window");

  cv::Mat imageRGBA1(image.rows, image.cols, CV_8UC4);
  cv::cvtColor(image, imageRGBA1, CV_RGB2RGBA); // CV_RGB2RGBA WORKED!!
  int type2 = imageRGBA1.type(); // 24 --> CV_8UC4
  cv::namedWindow("imageRGBA1 window", cv::WINDOW_AUTOSIZE);
  cv::imshow("imageRGBA1 window", imageRGBA1); // CORRECT IMAGE
  cv::waitKey(0);
  cv::destroyWindow("imageRGBA1 window");

我错过了什么?

0 个答案:

没有答案