OpenCV学习4:图像锐化

前言

开局一张图,内容全靠编。

简介

图像锐化(image sharpening)是补偿图像的轮廓,增强图像的边缘及灰度跳变的部分,使图像变得清晰。

函数声明
void sharpen(const cv::Mat &image, cv::Mat &result);
函数定义
void sharpen(const cv::Mat &image, cv::Mat &result) {
	// 判断是否需要分配图像数据。如果需要,就分配
	result.create(image.size(), image.type());
	int nchannels = image.channels(); // 获得通道数
									  // 处理所有行(除了第一行和最后一行)
	for (int j = 1; j < image.rows - 1; j++) {
		const uchar* previous = image.ptr<const uchar>(j - 1); // 上一行
		const uchar* current = image.ptr<const uchar>(j); // 当前行
		const uchar* next = image.ptr<const uchar>(j + 1); // 下一行
		uchar* output = result.ptr<uchar>(j); // 输出行
		for (int i = nchannels; i < (image.cols - 1)*nchannels; i++) {
			// 应用锐化算子
			*output++ = cv::saturate_cast<uchar>(
				5 * current[i] - current[i - nchannels] -
				current[i + nchannels] - previous[i] - next[i]);
		}
	}
	// 把未处理的像素设为 0 
	result.row(0).setTo(cv::Scalar(0,0,0));
	result.row(result.rows - 1).setTo(cv::Scalar(0,0,0));
	result.col(0).setTo(cv::Scalar(0,0,0));
	result.col(result.cols - 1).setTo(cv::Scalar(0,0,0));
}

图像扫描中使用了三个指针,一个表示当前行、一个表示上面的行、一个表示下面的行。另外,因为在计算每一个像素时都需要访问与它相邻的像素,所以有些像素的值是无法计算的,比如第一行、最后一行和第一列、最后一列的像素。

实现原理

在计算输出像素的值时,我们调用了 cv::saturate_cast 模板函数,并传入运算结果。这是因为计算像素的数学表达式的结果经常超出允许的范围(即小于 0 或大于 255)。使用这个函数可把结果调整到 8 位无符号数的范围内,具体做法是把小于 0 的数值调整为 0,大于 255 的数值调整为 255——这就是 cv::saturate_cast函数的作用。此外,如果输入参数是浮点数,就会得到最接近的整数。可以在调用这个函数时显式地指定其他数据类型,以确保结果在该数据类型定义的范围之内。
由于边框上的像素没有完整的相邻像素,因此不能用前面的方法计算,需要另行处理。这里简单地把它们设置为 0。

测试代码:
#include <opencv2/core.hpp> 
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

void sharpen(const cv::Mat &image, cv::Mat &result);

int main(int argc, char* argv[])
{
	cv::Mat img;
	cv::Mat result;
	img=cv::imread("test.bmp");
	if(img.empty())
	{
		std::cout << "read error"<<std::endl;
	}
	else
	{
		
		sharpen(img, result);
		//显示结果
		cv::imshow("img", img);
		cv::imshow("result", result);
		cv::waitKey(0);
	}	
	system("pause");
	return 0;
}

void sharpen(const cv::Mat &image, cv::Mat &result) {
	// 判断是否需要分配图像数据。如果需要,就分配
	result.create(image.size(), image.type());
	int nchannels = image.channels(); // 获得通道数
									  // 处理所有行(除了第一行和最后一行)
	for (int j = 1; j < image.rows - 1; j++) {
		const uchar* previous = image.ptr<const uchar>(j - 1); // 上一行
		const uchar* current = image.ptr<const uchar>(j); // 当前行
		const uchar* next = image.ptr<const uchar>(j + 1); // 下一行
		uchar* output = result.ptr<uchar>(j); // 输出行
		for (int i = nchannels; i < (image.cols - 1)*nchannels; i++) {
			// 应用锐化算子
			*output++ = cv::saturate_cast<uchar>(
				5 * current[i] - current[i - nchannels] -
				current[i + nchannels] - previous[i] - next[i]);
		}
	}
	// 把未处理的像素设为 0 
	result.row(0).setTo(cv::Scalar(0,0,0));
	result.row(result.rows - 1).setTo(cv::Scalar(0,0,0));
	result.col(0).setTo(cv::Scalar(0,0,0));
	result.col(result.cols - 1).setTo(cv::Scalar(0,0,0));
}
测试结果:

原图:
在这里插入图片描述
处理后图片:

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值