Mat基本操作

矩阵基本操作

opencv最基本的结构体是Mat,opencv提供了一系列Mat基本操作
加载图片cv::imgread
修改图片(色彩变化)cv::cvtColor 第三个参数表示从源色彩空间转换到目的色彩空间
保存图片cv::imgwrite
显示图像cv::namedWindows cv::imshow

#include "stdafx.h"
#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    Mat src = imread("F:/build_android.png",IMREAD_GRAYSCALE);
    Mat output_img = Mat::zeros(src.size(),src.type());
    namedWindow("test opencv", CV_WINDOW_AUTOSIZE);//CV_WINDOW_AUTOSIZE不允许任务修改窗口大小
    imshow("show img", src);

    namedWindow("test opencv2", CV_WINDOW_AUTOSIZE);
    
    cvtColor(src, output_img, COLOR_BGR2HLS);
    imshow("show img2", output_img);

    int cols = src.cols*src.channels();//图像宽度等于图像的cols乘以图像的通道数
    int rows = src.rows;//图像高度等于图像的rows
    for (int row = 1; row < rows-1; row++) {

    }
    //imwrite("F:/hlstest.png",output_img);
    waitKey(0);
    return 0;
}

获取图像像素指针

CV_Assert(myImage.depth()==CV_8U)
Mat.ptr<uchar>(int i=0)获取像素矩阵的指针,索引i表示第几行,从0开始计数
获取当前行指针const uchar* current = myImage.ptr<uchar>(row);
获取当前像素点P(row,col)的像素值p(row,col) = current[col]

像素范围处理saturate_cast<uchar>

saturate_cast<uchar>(-100),返回0
saturate_cast<uchar>(2888),返回255
小于0返回0,大于255返回255 这个函数确保像素值在0到255之间

矩阵掩膜操作(对比度提高,立体感增强)

数字图像处理中,掩模为二维矩阵数组,有时也用多值图像。数字图像处理中,图像掩模主要用于:

  • 提取感兴趣区,用预先制作的感兴趣区掩模与待处理图像相乘,得到感兴趣区图像,感兴趣区内图像值保持不变,而区外图像值都为0。
  • 屏蔽作用,用掩模对图像上某些区域作屏蔽,使其不参加处理或不参加处理参数的计算,或仅对屏蔽区作处理或统计。
  • 结构特征提取,用相似性变量或图像匹配方法检测和提取图像中与掩模相似的结构特征。
  • 特殊形状图像的制作。

掩膜操作实现图像对比度调整,根据掩膜来重新计算每个像素的像素值,掩膜(mask)也被称为 kernel。
通过掩膜操作实现图像对比度提高。


mask.png

上面矩阵像素操作可以用下面公式来表示
I(i,j) = 5*I(i,j) - [I(i-1,j) + I(i+1,j) + I(i,j-1) + I(i,j+1)]

#include <iostream>
#include <opencv2/opencv.hpp>
#include <math.h>

using namespace cv;

int main(int argc, const char * argv[]) {
    Mat src, dst;
    //加载图像
    src = imread("F:/build_android.jpg");
    
    if (!src.data) {
        printf("could not load image\n");
        return -1;
    }
    //显示
    namedWindow("input Image", CV_WINDOW_AUTOSIZE);
    imshow("input Image", src);

     //掩膜操作
     int cols = (src.cols-1) * src.channels();//图像宽度等于图像的cols乘以图像的通道数
     int offsetx = src.channels();
     int rows = src.rows;//图像高度等于图像的rows
     dst = Mat::zeros(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 = offsetx; col < cols; col++) {
         output[col] = saturate_cast<uchar>(5*current[col] - (current[col-offsetx] + current[col+offsetx] + previous[col] + next[col]));
       }
     }

    //显示
    namedWindow("contrast Image", CV_WINDOW_AUTOSIZE);
    imshow("contrast Image", dst);
    
    waitKey(0);
    return 0;
}

那么每次处理图片我们都需要把上面一长串代码拷贝到工程里面去吗,上述代码太多,其实opencv已经实现了上面功能。
filter2D函数可以实现上面效果,在第四个参数添加掩膜矩阵就可以实现相同效果

//定义掩膜
Mat kern = (Mat_<char>(3,3) <<  0, -1,  0,
                               -1,  5, -1,
                                0, -1,  0);

上面代码可以用下面代码替代

double t = getTickCount();  //获得当前时间
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1 ,0);
//src.depth() 表示与原图深度一样,-1也表示一样
filter2D(src, dst, src.depth(), kernel);
double time = (getTickCount() - t) / getTickFrequency();
printf("time consume %.5f", time);
namedWindow("contrast Image", CV_WINDOW_AUTOSIZE);
imshow("contrast Image", dst);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容