说在前面
- opencv版本:4.0.1
- 操作系统:win10
- vs版本:2017
- 官方文档:Mask operations on matrices
- 其他说明:自学,记录,demo
Mask operations on matrices
- 通过 mask matrix 对图像中每个像素的值重新计算,例如:
[ 0 − 1 0 − 1 5 − 1 0 − 1 0 ] \left[ \begin{matrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \\ \end{matrix} \right] ⎣⎡0−10−15−10−10⎦⎤ - 即
I ( i , j ) = 5 ∗ I ( i , j ) − [ I ( i − 1 , j ) + I ( i + 1 , j ) + I ( i , j − 1 ) + I ( i , j + 1 ) ] I(i,j) = 5*I(i,j) - [I(i-1,j) + I(i+1,j) + I(i,j-1) + I(i,j+1)] I(i,j)=5∗I(i,j)−[I(i−1,j)+I(i+1,j)+I(i,j−1)+I(i,j+1)] - 像素的值记为
I
I
I,例如中间那个像素的值记为
I
(
i
,
j
)
I(i,j)
I(i,j)
Code
- 主要流程
- 从第一行第一列的这个像素(下图中深蓝色)开始(第0行/第0列/最后一行/最后一列 无意义,没有前一行和前一列、后一行和后一列)
- 对该像素的每个通道应用前面的计算公式,如下图,深蓝色表示
I
(
i
,
j
)
I(i,j)
I(i,j)
浅蓝色分别为
I ( i , j − 1 ) I ( i − 1 , j ) I ( i + 1 , j ) I ( i , j + 1 ) \begin{matrix} & I(i,j-1) & \\ I(i-1,j) & & I(i+1,j) \\ & I(i,j+1) & \\ \end{matrix} I(i−1,j)I(i,j−1)I(i,j+1)I(i+1,j)
-
具体表现在程序中的时候,我们使用下面的方法来表示上一行、当前行、下一行;
-
并且由于使用了uchar *,可以访问到每个像素的每个通道,因此我们使用 current[ i - nChannels ] (即 [当前位置 i - 通道数] 的方式) 来访问前一个像素的相同通道的值
const uchar* previous = myImage.ptr<uchar>(j - 1); const uchar* current = myImage.ptr<uchar>(j ); const uchar* next = myImage.ptr<uchar>(j + 1); //...... 5*current[i] - current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]
-
因为该算法需要访问前一行、后一行、前一列以及后一列的数据,那么图像的首行、首列、尾行、尾列均没有意义,示例demo中直接将其置0。
Result.row(0).setTo(Scalar(0)); Result.row(Result.rows-1).setTo(Scalar(0)); Result.col(0).setTo(Scalar(0)); Result.col(Result.cols-1).setTo(Scalar(0)); -
- 代码
void Sharpen(const Mat& myImage,Mat& Result)
{
CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
const int nChannels = myImage.channels();
Result.create(myImage.size(),myImage.type());
for(int j = 1 ; j < myImage.rows-1; ++j)
{
const uchar* previous = myImage.ptr<uchar>(j - 1);
const uchar* current = myImage.ptr<uchar>(j );
const uchar* next = myImage.ptr<uchar>(j + 1);
uchar* output = Result.ptr<uchar>(j);
for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
{
*output++ = saturate_cast<uchar>(5*current[i]
-current[i-nChannels] - current[i+nChannels]
- previous[i] - next[i]);
}
}
Result.row(0).setTo(Scalar(0));
Result.row(Result.rows-1).setTo(Scalar(0));
Result.col(0).setTo(Scalar(0));
Result.col(Result.cols-1).setTo(Scalar(0));
}
- saturate_cast
opencv中用于防止数值溢出的一个函数
uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN)
short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)
The filter2D function
- opencv封装的方法
Mat kernel = (Mat_<char>(3,3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
filter2D( src, dst1, src.depth(), kernel );
-
depth()函数:返回Mat中通道的类型(在该例中src.depth() 的值为0,表示CV_8U)
- CV_8U - 8-bit unsigned integers ( 0…255 )
- CV_8S - 8-bit signed integers ( -128…127 )
- CV_16U - 16-bit unsigned integers ( 0…65535 )
- CV_16S - 16-bit signed integers ( -32768…32767 )
- CV_32S - 32-bit signed integers ( -2147483648…2147483647 )
- CV_32F - 32-bit floating-point numbers ( -FLT_MAX…FLT_MAX, INF, NAN )
- CV_64F - 64-bit floating-point numbers ( -DBL_MAX…DBL_MAX, INF, NAN )
测试结果
上面的为Sharpen函数
下面的为Filter2D函数
图片输出结果二者差不多
END-2019.6.26