opencv3/C++图像像素操作详解

RGB图像转灰度图

RGB图像转换为灰度图时通常使用:

同理使用min(r,min(g,b))可以看到由于选择了较小的灰度值图像会明显变暗:

#include<opencv2/opencv.hpp>
#include<math.h>
using namespace cv;

int main()
{
 Mat src, dst;
 src = imread("E:/image/image/daibola.jpg");
 CV_Assert(src.depth() == CV_8U);
 if(!src.data)
 {
  printf("can not load image n");
  return -1;
 }

 src.copyTo(dst);
 for(int row = 1; row<(src.rows - 1); row++)
 {
  const uchar* previous = src.ptr<uchar>(row - 1);
  const uchar* current = src.ptr<uchar>(row);
  const uchar* next = src.ptr<uchar>(row + 1);
  uchar* output = dst.ptr<uchar>(row);
  for(int col = src.channels(); col < (src.cols - 1)*src.channels(); col++)
  {
   *output = saturate_cast<uchar>(9 * current[col] - 2*previous[col] - 2*next[col] - 2*current[col - src.channels()] - 2*current[col + src.channels()]);
   output++;
  }
 }

 namedWindow("image", CV_WINDOW_AUTOSIZE);
 imshow("image",dst);
 waitKey();
 return 0;
}

以上这篇opencv3/C++图像像素操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持来客网。