matlab如何获得一个二值掩码,1.3矩阵的掩码操作 - osc_uarhdl2n的个人空间 - OSCHINA - 中文开源技术交流社区...

矩阵的掩码操作:根据掩码矩阵重新计算矩阵中每个像素的值。从数学观点看,利用自己设置的权值,对

像素邻域内的值做了加权平均。突出像素点,图片有了锐化的效果。

对图像的每个像素应用下列公式

I(i,j)=5*I(i,j)-I(i+1,j)-I(i,j+1)-I(i-1,j)-I(i,j-1)

#include

#include

#include

#include

#include

using namespace cv;

using namespace std;

void CreateLookupTable(uchar* table, uchar divideWith);

Mat& ScanImageAndReduceC(Mat& I, const uchar* table);

Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);

Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* table);

int main() {

Mat I, J;

I = imread("D:\\OpenCVT\\4.jpg", CV_LOAD_IMAGE_COLOR);

if (!I.data) {

cout << "The image could not be loaded" << endl;

return -1;

}

Mat Kernel = (Mat_(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);

filter2D(I, J, I.depth(), Kernel);

namedWindow("源图像", CV_WINDOW_NORMAL);

imshow("源图像", I);

namedWindow("矩阵掩码操作后", CV_WINDOW_NORMAL);

imshow("矩阵掩码操作后", J);

waitKey(0);

return 0;

}

void CreateLookupTable(uchar* table, uchar divideWith) {

for (int i = 0; i < 256; i++) {

table[i] = (i / divideWith)*divideWith;

}

}

Mat& ScanImageAndReduceC(Mat& I, const uchar* table) {

//检测只能为uchar类型

CV_Assert(I.depth() != sizeof(uchar));

int channels = I.channels();

int nRows = I.rows*channels;

int nCols = I.cols;

if (I.isContinuous()) {

nCols*= nRows;

nRows = 1;

}

int i, j;

uchar *p;

for (i = 0; i < nRows; ++i) {

p = I.ptr(i);

for (j = 0; j < nCols; ++j) {

p[j] = table[p[j]];

}

}

return I;

}

Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table) {

CV_Assert(I.depth() != sizeof(uchar));

const int channels = I.channels();

switch (channels) {

case 1: {

MatIterator_it, end;

for (it = I.begin(), end = I.end(); it != end; ++it) {

*it = table[*it];

}

break;

}

case 3: {

MatIterator_it, end;

for (it = I.begin(), end = I.end(); it != end; ++it) {

(*it)[0] = table[(*it)[0]];

(*it)[1] = table[(*it)[1]];

(*it)[2] = table[(*it)[2]];

}

}

}

return I;

}

Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* table) {

return I;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值