OpenCV4.0(2)腌膜处理

OpenCV4.0学习笔记(2)腌膜处理

腌膜理解

在最近学习了opencv图像的腌膜处理方法。

在我在学习的过程对于图像腌膜处理的理解即是:

腌膜其实相当于一个滤波,对图片进行处理

类似在修图软件中的将图片锐化,调高对比度,使得图像更加的清晰。

因为图像是由一个个的像素点组成的,腌膜在图片中提取出一块3*3的小矩阵,将小矩阵中的像素值按比例重新计算,使得对比度更高。

原理:

在这里插入图片描述

腌膜在opencv中的使用

在opencv4.0中使用方法和旧版本的使用方法一致.
但是同样的不需要额外使用namedWindow函数重新打开窗口,可以直接使用imshow函数显示函数
在原理上掩膜是使用指针提取出图像的像素点,通过对rows和colum根据掩膜的数学公式对3*3的像素矩阵进行处理;

	int cols = (img.cols - 1) * img.channels();
 	int offsetx = img.channels();	
 	int rows = img.rows;
	dst = Mat::zeros(img.size(), img.type());
 	for (int row = 1 ; row < (rows - 1); row++)
 	{
  		const uchar*previous = img.ptr<uchar>(row - 1);
  		const uchar*current = img.ptr<uchar>(row);
  		const uchar*next = img.ptr<uchar>(row + 1);
  		uchar * output = dst.ptr<uchar>(row);
  		for (int col = offsetx; col < cols; col++)
  		{
   			output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
  		}
 	}

filter2D函数

但是在实际操作中有更加简单的操作方式,通过调用函数filter2D直接调用刚才定义的掩膜对图像进行处理。
即:
第一步:定义要使用的腌膜
第二步:调用filter2D函数用已经定义好的好、腌膜对图像进行处理

 Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
 filter2D(img, dst, img.depth(), kernel);

saturate_cast< uchar >()函数

对于图像的处理过程中经常出现图像中的某些点的像素值超出或者跌出0-255之间,导致图片部分区域变成沙粒状。
而saturate_cast_< char >函数则可以保证图像RGB的值位于0-255之间。

saturate_cast<uchar>(-100);		//该点的像素值会返回0
saturate_cast<ucahr>(288);		//该点的像素值会返回255
saturate_casr<uchar>(100);		//改点的像素值会返回100

本次学习所练习的代码为:


#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>

using namespace cv;
int main(int arge, char** argv)
{
       Mat img, dst;
              img = imread("E:\\VOC2007\\JPEGImages\\0001.jpg", 1);
       if (img.empty())
       {
              printf("could not show the image...");
              return -1;
       }
       imshow("opencv setup done", img);
       /*
       int cols = (img.cols - 1) * img.channels();
       int offsetx = img.channels();
       int rows = img.rows;
       dst = Mat::zeros(img.size(), img.type());
       for (int row = 1 ; row < (rows - 1); row++)
       {
              const uchar*previous = img.ptr<uchar>(row - 1);
              const uchar*current = img.ptr<uchar>(row);
              const uchar*next = img.ptr<uchar>(row + 1);
              uchar * output = dst.ptr<uchar>(row);
              for (int col = offsetx; col < cols; col++)

              {
                     output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
              }
       }
       */
       Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
       filter2D(img, dst, img.depth(), kernel);
       imshow("the changed image", dst);
       waitKey(0);
       return 0;
}    
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗头狗不狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值