OpenCV2 像素基本操作

先上代码。。。

#include <cv.h>
#include<highgui\highgui.hpp>
#include <string>
#include <cstdio>

void slat(cv::Mat &img, int n);
void colorReduce(cv::Mat &img, int div = 64);
void sharpen(const cv::Mat &img, cv::Mat &outimg);

using namespace std;
string sample_png = "D:\\sample.png";
string sample2_png = "D:\\sample2.png";
string shit_png = "D:\\shit.png";
//column 列 x 宽
//row    行 y 高
// img.at<type>(int y, int x);

int main()
{
    cv::Mat image = cv::imread(sample_png);
    cv::namedWindow(sample_png);
    cv::imshow(sample_png, image);
    //1
    //椒盐噪声
    //cv::Mat imageOut = cv::Mat(image);
    //拷贝构造函数 图像数据共用的是一块内存。 
    cv::namedWindow(sample_png + "_OUT1");
    cv::Mat imageOut = cv::Mat(image);
    slat(imageOut, 1024);
    cv::imshow(sample_png + "_OUT1", imageOut);
    //椒盐噪声 结束
    //2
    //cv::Mat_<type> 指定元素 image也不变 引用 
    // Mat_& operator = (const Mat& m)   引用
    cv::namedWindow(sample_png + "_OUT2");
    cv::Mat_ <cv::Vec3b> imageOut2 = image.clone();
    for (int i = 0; i < 10240; i++)
    {
    int x = rand() % imageOut2.cols;
    int y = rand() % imageOut2.rows;
    imageOut2(y, x)[0] = 255;
    imageOut2(y, x)[1] = 255;
    imageOut2(y, x)[2] = 255;
    }
    cv::imshow(sample_png + "_OUT2", image);
    cv::imwrite("D:\\sample_save.png", image);
    //cv::Mat_ 结束
    //3
    //指针操作 函数声明里设置默认参数
    cv::namedWindow(sample_png + "_OUT3");
    cv::Mat imageOut3 = image.clone();
    colorReduce(imageOut3,128);
    cv::imshow(sample_png + "_OUT3", imageOut3);
    cv::imshow(sample_png, image);
    //指针操作 结束
    //4 
    //clone 图像复制
    cv::Mat imgClone = image.clone();
    //clone 结束
    //5 
    //create 图像  
    //create 如果现有的和原来的一样 就回直接返回,不会进行操作
    //判断源图片和现有图片是不是一样的 不管out图片有没有分配内存 都没问题了 相当于做了一个检查
    cv::Mat imgCreate;
    imgCreate.create(cv::Size(image.cols,image.rows), image.type());
    cv::namedWindow(sample_png + "_OUT5");
    cv::imshow(sample_png + "_OUT5", imgCreate);
    //create 
    //6
    //锐化 拉普拉斯 0 -1 0
    //            -1 5 -1
    //             0 -1 0
    //遍历图像邻域
    //// 8bit, color or not
    //IMREAD_UNCHANGED = -1,
        // 8bit, gray
    //  IMREAD_GRAYSCALE = 0,
        // ?, color
    //  IMREAD_COLOR = 1,
        // any depth, ?
    //  IMREAD_ANYDEPTH = 2,
        // ?, any color
    //  IMREAD_ANYCOLOR = 4
    cv::Mat image2 = cv::imread(sample2_png,0);
    cv::namedWindow(sample2_png);
    cout << image2.channels()<<endl;
    cv::imshow(sample2_png, image2);
    cv::namedWindow(sample2_png + "_OUT6");
    cv::Mat outimg;
    sharpen(image2, outimg);
    cv::imshow(sample2_png + "_OUT6", outimg);
    //遍历图像邻域 结束
    //7
    //ROI
    cv::Mat shit = cv::imread(shit_png);
    cv::namedWindow(sample_png + "_OUT7");
    cv::Mat_ <cv::Vec3b> imageOut7 = image.clone();
    //cv::Mat imageROI = imageOut7(cv::Rect(imageOut7.cols - shit.cols - 500, imageOut7.rows - shit.rows - 50, shit.cols, shit.rows));
    cv::Mat imageROI = imageOut7(cv::Range(50, 50 + shit.cols), cv::Range(200, 200 + shit.rows));
    //imageROI = imageROI + shit;
    imageROI = imageROI - shit;
    imageOut7.copyTo(shit, imageOut7);
    cv::imshow(sample_png + "_OUT7", imageOut7);


    cv::waitKey(0);
    return 0;
}

void slat(cv::Mat &img, int n)
{
    for (int i = 0; i < n; i++)
    {
        int x = rand() % img.cols;
        int y = rand() % img.rows;
        if (img.channels() == 1)
        {
            img.at<uchar>(y, x) = 255;
        }
        else if (img.channels() == 3)
        {
            //typedef Vec<uchar, 3> Vec3b
            img.at<cv::Vec3b>(y, x)[0] = 255;
            img.at<cv::Vec3b>(y, x)[1] = 255;
            img.at<cv::Vec3b>(y, x)[2] = 255;
        }
    }
}

void colorReduce(cv::Mat &img, int div)
{
    int nl = img.rows;
    int nc = img.cols*img.channels();
    for (int i = 0; i < nl; i++)
    {
        //每行首地址
        uchar* data = img.ptr<uchar>(i);
        for (int j = 0; j < nc; j++)
        {
            data[j] = data[j] / div*div + div / 2;
        }
    }
}
void colorReduce(cv::Mat &img, cv::Mat &outimg ,int div)
{
    outimg.create(img.rows, img.cols, img.type());
    int nl = img.rows;
    int nc = img.cols*img.channels();
    for (int i = 0; i < nl; i++)
    {
        //每行首地址
        uchar* data = img.ptr<uchar>(i);
        for (int j = 0; j < nc; j++)
        {
            data[j] = data[j] / div*div + div / 2;
        }
    }
}
//cv::saturate_cast<uchar>();
//溢出保护
void sharpen(const cv::Mat &img, cv::Mat &outimg)
{
    outimg.create(img.size(), img.type());
    int h = img.rows;
    int w = img.cols;
    int w2 = w - 1;
    int h2 = h - 1;
    for (int y = 1; y < h2; y++)
    {
        const uchar* pre = img.ptr<const uchar>(y - 1);//上一行
        const uchar* cur = img.ptr<const uchar>(y);//当前行
        const uchar* next = img.ptr<const uchar>(y + 1);//下一行
        uchar* output = outimg.ptr<uchar>(y);
        for (int x = 1; x < w2; x++)
        {
            //cv::saturate_cast<uchar>( );
            *output++ = cv::saturate_cast<uchar>(5 * cur[x] - cur[x - 1] - cur[x + 1] - pre[x] - next[x]);
        }
    }
    //彩色 outimg.row(0).setTo(cv::Scalar(1,2,3));
    outimg.row(0).setTo(cv::Scalar(0));
    outimg.row(h2).setTo(cv::Scalar(0));
    outimg.col(0).setTo(cv::Scalar(0));
    outimg.col(w2).setTo(cv::Scalar(0));
}

1.读图片 第二个参数是类型

enum
{
    // 8bit, color or not
    IMREAD_UNCHANGED  =-1,
    // 8bit, gray
    IMREAD_GRAYSCALE  =0,
    // ?, color
    IMREAD_COLOR      =1,
    // any depth, ?
    IMREAD_ANYDEPTH   =2,
    // ?, any color
    IMREAD_ANYCOLOR   =4
};
string sample_png = "D:\\sample.png";
cv::Mat image = cv::imread(sample_png);

2.Mat_类型和Mat很像,在初始的时候指定了矩阵的元素的类型。
Mat_重载了(),可以直接访问元素,比起Mat的a看起来跟更方便一些。vec 向量。

/* \typedef

   Shorter aliases for the most popular specializations of Vec<T,n>
*/
typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;

typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;

typedef Vec<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;

typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;

typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;

typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;

imageOut2 = image.clone();这种是复制,否则图像内容共享同一块内存区域。

cv::Mat_ <cv::Vec3b> imageOut2 = image.clone();

3.也可以用指针遍历矩阵元素,例子是void colorReduce(cv::Mat &img, int div)这个函数。一行一行的遍历。uchar* data = img.ptr<uchar>(i);然后一个循环,访问一行的数据。
4.outimg.create(img.rows, img.cols, img.type());
这个可以改变图像的大小,但是并不会改变图像的数据。高明的地方在于,如果要改变的大小和原图是一样大的,就直接返回。
这有啥用呢。我们有时候想直接对原图进行操作,有些时候想对原图创建一个副本进行操作。void sharpen(const cv::Mat &img, cv::Mat &outimg)
这个函数,两个引用参数,img是原图,outimg 是结果的图像的引用。如果是想在原图上进行运算,输出图像那个参数那就直接传原图就好。要是想把结果保存到新图像上去。。就传进去另一个Mat就好了。其中第一举的outimg.create(img.size(), img.type()); 的作用就是保证输出是合法的。
5.opencv 重载了大部分算数运算符。例如+和-
图像进行加减运算的时候,要注意格式一样的问题。
RIO 可以认为是图像的一小部分,一个矩形。和原图共享内存。
下面是创建ROI的两种方法。

cv::Mat imageROI = imageOut7(cv::Rect(imageOut7.cols - shit.cols - 500, imageOut7.rows - shit.rows - 50, shit.cols, shit.rows));
cv::Mat imageROI = imageOut7(cv::Range(50, 50 + shit.cols), cv::Range(200, 200 + shit.rows));

程序运行结果图
原图有噪声,说明,加噪声的imageOut2和原图的数据是公用的一块内存。
cv::Mat_ <cv::Vec3b> imageOut2 = image.clone();


slat.png

colorReduce的运行结果


colorReduce.png

锐化后的结果
sharpen.png

像素数学操作的结果
shit.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容