opencv

图像掩膜

2019-09-30  本文已影响0人  wwda

知识点

unsigned char 整数范围为0到255
Mat.ptr<uchar>(row) --返回row行的首地址,用法类同数组
------真实的像素矩阵---------
(r,g,b)(r,g,b)(r,g,b)
(r,g,b)(r,g,b)(r,g,b)
(r,g,b)(r,g,b)(r,g,b)


#include"file.h"
#include<opencv2/opencv.hpp>
#include<iostream>
int main()
{
    cv::Mat src, dst;
    src = cv::imread("F:/test.jpg");
    if (!src.data) {
        printf("not load image\n");
    }
    cv::imshow("output image", src);
    /*
      矩阵的掩膜操作
    */
    //图像的列数
    //因为rgb三个值作为像素值,列数乘以通道数才是真正的列数
    int cols = (src.cols-1)*src.channels();
    
    //行数
    int rows = src.rows;
    //offsex=3
    int offsex = src.channels();
    //printf("channels:%d\n", src.channels());
    //原图像的副本
    dst = cv::Mat(src.size(), src.type());
    for (int row = 1; row < (rows - 1); row++) {
        //访问像素指针
        const uchar*previous = src.ptr<uchar>(row - 1);
        const uchar*current = src.ptr<uchar>(row);
        const uchar*next = src.ptr<uchar>(row + 1);
        uchar* output = dst.ptr<uchar>(row);
        for (int col = offsex; col < cols; col++) {
        //调整对比度
        //各自与相邻像素的同类通道值计算
        output[col] = 5 * current[col] - (current[col - offsex] + current[col + offsex] + previous[col] + next[col]);

        }
        /*
           ------真实的像素矩阵------
          (r,g,b)(r,g,b)(r,g,b)
          (r,g,b)(r,g,b)(r,g,b)
          (r,g,b)(r,g,b)(r,g,b)
          -------------------------
            0 -1 0
            -2 5 -3
            0  -4 0
          结果:
           25-(-1+-2+-3+-4)
        */
    }
    imshow("changed image", dst);
    cv::waitKey(0);
    return 0;
}
image.png

掩膜后结果分析:

对比更加鲜明了,图片清晰度似乎提高了一些。

上一篇下一篇

猜你喜欢

热点阅读