如何在c ++中将16位图像转换为8位

时间:2016-12-25 10:48:01

标签: c++ image opencv graphics

我想在C ++中使用OpenCV库更改图片中的RGB值。我遍历图像中的像素,读取R,G,B并将其更改为:

#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main(int argc, char *argv[]){

  Mat depth16 = imread("./test.tif",CV_LOAD_IMAGE_UNCHANGED);

  if(depth16.empty()){
    cout<<"image not loaded";
  }
  else{
    cout<<depth16.type()<<endl;
  }

  float scale_factor=1.0/256.0;
  //depth16.convertTo(depth16, CV_8U, scale_factor);

  float step=65536/256;

 uchar r, g, b;
      for (int i = 0; i < depth16.rows; ++i){
        Vec3b* pixel = depth16.ptr<cv::Vec3b>(i);
        for (int j = 0; j < depth16.cols; ++j){
          r=pixel[j][2];
          g=pixel[j][1];
          b=pixel[j][0];

          depth16.at<Vec3b>(Point(j, i))[2] = step/r+r/2;
          depth16.at<Vec3b>(Point(j, i))[1] = step/g+g/2;
          depth16.at<Vec3b>(Point(j, i))[0] = step/b+b/2;
        }
      }

  namedWindow( "test", CV_WINDOW_AUTOSIZE );
  imshow("test",depth16);
  imwrite( "./Gray_Image.tif", depth16 );

  cout<<depth16.type()<<endl;
  waitKey(0);    



  return 0;  
} 

但只有左半边的图片会改变颜色,我不知道自己做错了什么。

0 个答案:

没有答案