OpenCV&图像处理_1:矩阵,以及图像读写操作

本文是OpenCV图像处理系列的第一部分,主要介绍矩阵概念及其在图像表示中的应用,同时详细讲解如何使用OpenCV进行图像的读取和写入操作。适合初学者入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Attention]:阅读本文前请先阅读我OpenCV&图像处理_0这篇帖子。

一:矩阵,图像的容器

How we get and store the pixels values may vary according to our needs, but in the end all images inside a computer world may be reduced to numerical matrices and other information describing the matrix itself
计算机里存储的图像最终只是数字矩阵以及一些描述矩阵本身的信息。

二:OpenCV里图像的加载、修改、存储及显示
下面是我对OpenCV里源代码对图像加载、修改、存储和显示的一些个人解读:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <iostream>
/*core section, as here are defined the basic building blocks of the library.
cocre section, 定义了OpenCV函数库的基本块,包含有大量的算法和数据结构
highgui module, as this contains the functions for input and output operations.
highgui module, 包含输入输出操作*/
using namespace cv;
using namespace std;

int main( int argc,char** argv){
    Mat image;
    image = imread( "E:/VS2010Projects/image/lena_RGB.jpg" , CV_LOAD_IMAGE_UNCHANGED);
/*CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
加载原图,如果有alpha通道也加载alpha通道
CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one
加载灰度图,RGB可以转化为灰度图
CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format
加载彩色图,灰度图不可以转化为彩色图*/

    if(! image.data ){                              // Check for invalid input
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
                Mat Gray_image1;
                cvtColor( image, Gray_image1, CV_BGR2GRAY );
                Mat Gray_image2;
                cvtColor( image, Gray_image1, CV_RGB2GRAY );
                namedWindow( "Display window", WINDOW_AUTOSIZE );
/*CV_WINDOW_AUTOSIZE图像多大就显示窗口多大
CV_WINDOW_NORMAL可以调整窗口大小*/
                imwrite( "E:/VS2010Projects/image/lena_BGR2Gray.jpg" ,Gray_image1);
                imwrite( "E:/VS2010Projects/image/lena_RGB2Gray.jpg" ,Gray_image1);
                imshow( "Display window", image);
    waitKey(0);
    return 0;
}
个人认为这些会用就好了,其中的关键在于:
cvtColor()函数怎么将一副彩色图像转化为灰度图?

答案如上图公式所示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值