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

yipeiwu_com6年前Python基础

RGB图像转灰度图

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

进行转换,以下尝试通过其他对图像像素操作的方式将RGB图像转换为灰度图像。

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

int main()
{
 //像素操作
 Mat src,dst;
 src = imread("E:/image/image/daibola.jpg");
 if(src.empty())
 {
  printf("can not load image \n");
  return -1;
 }

 namedWindow("input");
 imshow("input",src);

 dst.create(src.size(), src.type());

 for(int row = 0; row < src.rows; row++)
 {
  for(int col = 0; col < src.cols; col++)
  {
   int b = src.at<Vec3b>(row, col)[0];
   int g = src.at<Vec3b>(row, col)[1];
   int r = src.at<Vec3b>(row, col)[2];
   dst.at<Vec3b>(row, col)[0] = max(r,max(g,b));
   dst.at<Vec3b>(row, col)[1] = max(r,max(g,b));
   dst.at<Vec3b>(row, col)[2] = max(r,max(g,b));

  }
 }

 namedWindow("output");
 imshow("output",dst);
 waitKey();

}

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

图像线性增强

通过对图像像素操作(线性变换),实现图像的线性增强。

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

int main()
{
 Mat src1, dst;
 src1 = imread("E:/image/image/im1.jpg");
 if(src1.empty())
 {
  printf("can not load im1 \n");
  return -1;
 }
 double alpha = 1.2, beta = 50;
 dst = Mat::zeros(src1.size(), src1.type());
 for(int row = 0; row < src1.rows; row++)
 {
  for(int col = 0; col < src1.cols; col++)
  {
   if(src1.channels() == 3)
   {
    int b = src1.at<Vec3b>(row, col)[0]; 
    int g = src1.at<Vec3b>(row, col)[1]; 
    int r = src1.at<Vec3b>(row, col)[2]; 

    dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta); 
    dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta); 
    dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta); 
   }
   else if (src1.channels() == 1)
   {
    float v = src1.at<uchar>(row, col); 
    dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
   }
  }
 }

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

掩膜操作调整图像对比度

使用一个3×3掩模增强图像对比度:

#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;
}

像素重映射

利用cv::remap实现像素重映射;

cv::remap参数说明:

Remap(
InputArray src,// 输入图像
OutputArray dst,// 输出图像
InputArray map1,// 映射表1(CV_32FC1/CV_32FC2)
InputArray map2,// 映射表2(CV_32FC1/CV_32FC2)
int interpolation,// 选择的插值
int borderMode,// 边界类型(BORDER_CONSTANT)
const Scalar borderValue// 颜色 
)

插值方法:

CV_INTER_NN =0, 
CV_INTER_LINEAR =1, 
CV_INTER_CUBIC =2, 
CV_INTER_AREA =3, 
CV_INTER_LANCZOS4 =4

通过像素重映射实现图像垂直翻转:

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

int main()
{
 Mat src,dst;
 src = imread("E:/image/image/daibola.jpg");
 if(src.empty())
 {
  printf("can not load image \n");
  return -1;
 }
 namedWindow("input", CV_WINDOW_AUTOSIZE);
 imshow("input", src);
 Mat mapx,mapy;
 mapx.create(src.size(), CV_32FC1);
 mapy.create(src.size(), CV_32FC1);
 for(int row = 0; row < src.rows; row++)
 {
  for(int col = 0; col < src.cols; col++)
  {
   mapx.at<float>(row, col) = col;
   mapy.at<float>(row, col) = src.rows - row - 1;
  }
 }
 remap(src, dst, mapx, mapy, CV_INTER_NN, BORDER_CONSTANT, Scalar(0,255,255));

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

以上这篇opencv3/C++图像像素操作详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python读取yaml文件多层菜单

需要用到的Python知识点 Python的对象属性方法; 用到字典{key:value}值的提取; 列表的增加; if循环结合break的使用; yaml文件读取...

使用Python获取网段IP个数以及地址清单的方法

使用Python获取网段IP个数以及地址清单的方法

使用Python获取网段的IP个数以及地址清单需要用到IPy的库,而相应的方法主要就是IP。 写小脚本如下: from IPy import IP ip = IP('192.1...

Python编程之string相关操作实例详解

Python编程之string相关操作实例详解

本文实例讲述了Python编程之string相关操作。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 字符串是Python中最常见的类型。可以通过引号见包含字...

Python装饰器用法实例总结

本文实例讲述了Python装饰器用法。分享给大家供大家参考,具体如下: 一、装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下...

python 图片去噪的方法示例

图像可能在生成、传输或者采集过程中夹带了噪声,去噪声是图像处理中常用的手法。通常去噪声用滤波的方法,比如中值滤波、均值滤波。但是那样的算法不适合用在处理字符这样目标狭长的图像中,因为在滤...