opencv笔记——图像操作

部分笔记参考自点击这里
  • 学习图像操作的原因:
    前面我们已经学习了通过指针来访问图像矩阵中的每一个元素,从而达到访问图像,对图像进行操作的目的,但是由于指针操作存在一定的危险性,可能会由于指针操作而指向系统关键的运行区域,从而导致出现程序系统崩溃,影响电脑等问题,因而我们还有必要学习其他的简单地、基本的、适合初学者的操作图像的办法。

  • 内容:
    具体内容为:对图像进行操作,首先要能够读写图像,然后要能够读写像素,最终目的是像素值修改

  • 读写图像用到的主要函数是:
    imread()可以指定加载为灰度或者RGB图像
    imwrite()可以用来保存图像文件,类型由扩展名决定

  • 读写像素:
    读一个灰度图像的像素的方法为:
    img.at(x,y);
    读一个三通道RGB图像像素的方法为:
    img.at(x,y)
    int b = img.at(x,y)[0];
    int g = img.at(x,y)[1];
    int r = img.at(x,y)[2];
    Vec3b代表的是三通道uchar型数据,
    Vec3f代表的是三通道float型数据。

下面通过代码来学习,实践出真知

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
    Mat srcimage=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/luosi.jpg");
    if(srcimage.empty()) {
        cout<<"could not load the image"<<endl; return -1;
    }
    else
    {
        namedWindow("原图");
        imshow("原图",srcimage);
    }
    /*通过像素指针读取图像像素*/
    Mat one_image=Mat::zeros(srcimage.size(),srcimage.type());
    int channels=srcimage.channels();
    cout<<"The channels of srcimage is:"<<channels<<endl;
    int cols=srcimage.cols*channels;
    int rows=srcimage.rows;
    for(int row=0;row<rows;row++) {
        const uchar* current=srcimage.ptr<uchar>(row);
        uchar* output=one_image.ptr<uchar>(row);
        for(int col=0;col<cols;col++) {
            output[col]=current[col];
        }
    }
    namedWindow("效果图");
    imshow("效果图",one_image);
    /*读取一个单通道灰色图像*/
    Mat two_image;
    cvtColor(srcimage,two_image,CV_BGR2GRAY);
    namedWindow("灰度图像");
    imshow("灰度图像",two_image);
    Mat three_image(two_image.size(),two_image.type());
    int two_channels=two_image.channels();
    cout<<"the channels of two_image is:"<<two_image.channels()<<endl;
    int two_cols = two_image.cols;
    int two_rows=two_image.rows;
    for(int row=0;row<two_rows;row++)
        for(int col=0;col<two_cols;col++) {
            const uchar current=two_image.at<uchar>(row,col);
        three_image.at<uchar>(row,col)=current;
        }
    namedWindow("单通道像素读取效果图");
    imshow("单通道像素读取效果图",three_image);
    /*取单通道反差图像*/
    Mat four_image(two_image.size(),two_image.type());
    for(int row=0;row<two_rows;row++)
    for(int col=0;col<two_cols;col++) {
        int xiangsu=two_image.at<uchar>(row,col);
            four_image.at<uchar>(row,col)=255-xiangsu;
    }
    namedWindow("取反差图像效果图");
    imshow("取反差图像效果图",four_image);
    /*读取BGR三通道图像并取反差效果*/;
    Mat five_image(srcimage.size(),srcimage.type());
    rows=srcimage.rows;
    cols=srcimage.cols;
    for(int row=0;row<rows;row++)
    for(int col=0;col<cols;col++) {
        int b=srcimage.at<Vec3b>(row,col)[0];//读取蓝色通道像素值
        int g=srcimage.at<Vec3b>(row,col)[1];//读取绿色通道像素值
        int r=srcimage.at<Vec3b>(row,col)[2];//读取红色通道像素值
        /*取反差值*/
        five_image.at<Vec3b>(row,col)[0]=255-b;
        five_image.at<Vec3b>(row,col)[1]=255-g;
        five_image.at<Vec3b>(row,col)[2]=255-r;
        }
        namedWindow("三通道图像取反差效果图");
        imshow("三通道图像取反差效果图",five_image);
    waitKey(0);
    return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无所畏惧的man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值