OpenCV_tutorials 02 core module-Mask operations on matrices

#include<iostream>

#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

void sharpen(const Mat &src, Mat &dst);
void sharpenByfilter2D(const Mat &src, Mat &dst);

int main()
{
	Mat src = imread("C:\\Users\\dell\\Desktop\\xin1.jpg", 1);
	if (src.empty())
	{
		cout << "Source image load failed." << endl;
		return -1;
	}
	Mat dst1;
	sharpen(src, dst1);
	Mat dst2;
	sharpenByfilter2D(src, dst2);
	return 0;
}

//The Basic method to use mask operation on matrix
void sharpen(const Mat &src, Mat &dst)
{
	double t = (double)getTickCount();

	//accept only uchar images
	CV_Assert(src.depth() == CV_8U);

	dst.create(src.size(), src.type());
	const int nchannels = src.channels();

	for (int i = 1; i < src.rows - 1; i++)
	{
		const uchar *pPrevious = src.ptr<uchar>(i - 1);
		const uchar *pCurrent = src.ptr<uchar>(i);
		const uchar *pNext = src.ptr<uchar>(i + 1);

		uchar *pDst = dst.ptr<uchar>(i);
		for (int j = nchannels; j < (src.cols - 1) * nchannels; j++)
		{
			//pDst[j] = 5 * pCurrent[j] - (pCurrent[j - 1] + pCurrent[j + 1] + pPrevious[j] + pNext[j]);	//错误,要考虑三通道情况
			//pDst[j] = 5 * pCurrent[j] - (pCurrent[j - nchannels] + pCurrent[j + nchannels] + pPrevious[j] + pNext[j]);//考虑值超出uchar范围情况
			pDst[j] = saturate_cast<uchar>(5 * pCurrent[j] - (pCurrent[j - nchannels] + pCurrent[j + nchannels] + pPrevious[j] + pNext[j]));
		}
	}

	dst.row(0).setTo(Scalar(0));
	dst.row(src.rows - 1).setTo(Scalar(0));
	dst.col(0).setTo(Scalar(0));
	dst.col(src.cols - 1).setTo(Scalar(0));

	double tt = ((double)getTickCount() - t) / getTickFrequency();
	cout << "The basic method cost time : " << tt << "s." << endl;
}

//Use filter2D function
void sharpenByfilter2D(const Mat &src, Mat &dst)
{
	//double t = (double)getTickCount();

	CV_Assert(src.depth() == CV_8U);

	Mat kern = (Mat_<char>(3, 3) << 0, -1, 0,
									-1, 5, -1,
									0, -1, 0);

	double t = (double)getTickCount();
	filter2D(src, dst, src.depth(), kern);

	double tt = ((double)getTickCount() - t) / getTickFrequency();
	cout << "The filter2D function cost time : " << tt << "s." << endl;
}

1.需要注意的几个问题,都是自己踩过的坑,采坑的好处之一就是增加自己的思考,而不是只是凭记忆敲代码。

(1)我一开始写的pDst[j] = 5 * pCurrent[j] - (pCurrent[j - 1] + pCurrent[j + 1] + pPrevious[j] + pNext[j]); 这句话只考虑了单通道的情况,没有考虑三通道的情况。我输入的原图是一幅彩色图,按照这种错误的方式会得下列效果


原图


pDst[j] = 5 * pCurrent[j] - (pCurrent[j - 1] + pCurrent[j + 1] + pPrevious[j] + pNext[j]);产生的效果图

(2)对上述问题进行修改之后,我改成pDst[j] = 5 * pCurrent[j] - (pCurrent[j - nchannels] + pCurrent[j + nchannels] + pPrevious[j] + pNext[j]);但是这句话依然有问题,在GAKKI的眼睛和嘴巴处出现奇怪的像素点,为什么会出现少数点的异常情况,因为按照pDst[j] = 5 * pCurrent[j] - (pCurrent[j - nchannels] + pCurrent[j + nchannels] + pPrevious[j] + pNext[j])求像素点的值会出现某些点超出uchar类型的值的范围(0-255),对于超出范围的点就会显示的很奇怪了。


超出uchar值范围的点异常

为了解决这个值超过范围的异常,可以使用saturate_cast,修改之后pDst[j] = saturate_cast<uchar>(5 * pCurrent[j] - (pCurrent[j - nchannels] + pCurrent[j + nchannels] + pPrevious[j] + pNext[j]));这才能得到正确的结果


2.边界的处理

(1)对于方法1中对边界的处理非常简单粗暴:

dst.row(0).setTo(Scalar(0));
dst.row(src.rows - 1).setTo(Scalar(0));
dst.col(0).setTo(Scalar(0));

dst.col(src.cols - 1).setTo(Scalar(0));

效果:


可以看到,图像中第一行、最后一行,第一列、最后一列的像素点的三个通道像素值都被置0.

(2)filter2D默认的边界处理方式是怎么样的?


通过查看源码可以发现默认的边界处理方式是BORDER_REFLECT_101:   gfedcb|abcdefgh|gfedcba

原图边界:


将gfedcb|abcdefgh|gfedcba这种扩展方式应用在此测试原图上就是:


所以,最后的边界效果:


学习博客:

https://blog.csdn.net/zouxy09/article/details/49080029

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【作 者】Per Christian Hansen 【出版社】Society for Industrial and Applied Mathematic 【出版日期】October 29, 2006 【ISBN】0898716187 9780898716184 【形态项】9.8 x 6.7 x 0.3 inches 【语 言】English 【价 格】$63.00 Deblurring Images: Matrices, Spectra, and Filtering (Fundamentals of Algorithms 3) (Fundamentals of Algorithms) By Per Christian Hansen Publisher: Society for Industrial and Applied Mathematic Number Of Pages: 130 Publication Date: 2006-10-29 ISBN-10 / ASIN: 0898716187 ISBN-13 / EAN: 9780898716184 Binding: Paperback “The book’s focus on imaging problems is very unique among the competing books on inverse and ill-posed problems. …It gives a nice introduction into the MATLAB world of images and deblurring problems.” — Martin Hanke, Professor, Institut für Mathematik, Johannes-Gutenberg-Universität. When we use a camera, we want the recorded image to be a faithful representation of the scene that we see, but every image is more or less blurry. In image deblurring, the goal is to recover the original, sharp image by using a mathematical model of the blurring process. The key issue is that some information on the lost details is indeed present in the blurred image, but this “hidden” information can be recovered only if we know the details of the blurring process. Deblurring Images: Matrices, Spectra, and Filtering describes the deblurring algorithms and techniques collectively known as spectral filtering methods, in which the singular value decomposition—or a similar decomposition with spectral properties—is used to introduce the necessary regularization or filtering in the reconstructed image. The concise MATLAB® implementations described in the book provide a template of techniques that can be used to restore blurred images from many applications. This book’s treatment of image deblurring is unique in two ways: it includes algorithmic and implementation details; and by keeping the formulations in terms of matrices, vectors, and matrix computations, it makes the material accessible to a wide range of readers. Students and researchers in engineering will gain an understanding of the linear algebra behind filtering methods, while readers in applied mathematics, numerical analysis, and computational science will be exposed to modern techniques to solve realistic large-scale problems in image processing. With a focus on practical and efficient algorithms, Deblurring Images: Matrices, Spectra, and Filtering includes many examples, sample image data, and MATLAB codes that allow readers to experiment with the algorithms. It also incorporates introductory material, such as how to manipulate images within the MATLAB environment, making it a stand-alone text. Pointers to the literature are given for techniques not covered in the book. Audience This book is intended for beginners in the field of image restoration and regularization. Readers should be familiar with basic concepts of linear algebra and matrix computations, including the singular value decomposition and orthogonal transformations. A background in signal processing and a familiarity with regularization methods or with ill-posed problems are not needed. For readers who already have this knowledge, this book gives a new and practical perspective on the use of regularization methods to solve real problems. Preface; How to Get the Software; List of Symbols; Chapter 1: The Image Deblurring Problem; Chapter 2: Manipulating Images in MATLAB; Chapter 3: The Blurring Function; Chapter 4: Structured Matrix Computations; Chapter 5: SVD and Spectral Analysis; Chapter 6: Regularization by Spectral Filtering; Chapter 7: Color Images, Smoothing Norms, and Other Topics; Appendix: MATLAB Functions; Bibliography; Index

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值