1.預處理-灰度化圖片使用大全

1、快捷键:

补全提示:ctrl + J
方法参数提示:ctrl + shift +空格

2、首先介绍灰度图像和彩色图像

①灰度图像

单通道图像,每个像素点只能有有一个值表示颜色,它的像素值在0到255之间,0是黑色,255是白色,中间值是一些不同等级的灰色。

②彩色图像

三通道图像,每个像素点有三个值表示颜色,分别是 红(R)、绿(G)、蓝(B)

3、Mat初始化方法

//方法一:构造函数(高,宽,类型:8位无符号单通道)
Mat dst = Mat(src.rows, src.cols, CV_8UC1);
//方法二:
Mat dst(src.rows, src.cols, CV_8UC1);
//方法三:
Mat dst;
dst.create(src.rows, src.cols, CV_8UC1);
//方法四:(前三种方法中的前两个变量均可以替换成Size类型的变量)
Mat dst(Size(src.cols, src.rows), CV_8UC1);
4、像素的遍历

一副彩色图每个像素点有三个值,一副灰度图像一个像素点有一个值。同一副图的彩色与灰色图像的cols和rows相同,但是step不同,三倍关系。

代码实现:彩色图--->灰色图---->彩色图(注:并不是真正的灰色图转换成彩色图,而是单通道转换成三通道。比如一副黑白图,默认读入的形式就是真色彩,也就是三通道,但它还是个黑白图)

#include<stdio.h>
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(){
    String path = "C:\\Users\\HQH\\Desktop\\test.jpg";
    Mat src = imread(path);
    for (int i = 0; i < src.rows; i++){
        for (int j = 0; j < src.cols; j++){
            cout << (int)src.data[i*src.step + j * 3] << " ";
            cout << (int)src.data[i*src.step + j * 3 + 1] << " ";
            cout << (int)src.data[i*src.step + j * 3 + 2] << " ";
        }
        cout << endl;
    }
    cvtColor(src, src, CV_BGR2GRAY);
    for (int i = 0; i < src.rows; i++){
        for (int j = 0; j < src.cols; j++){
            cout << (int)src.data[i*src.step + j] << " ";
        }
        cout << endl;
    }
    cvtColor(src, src, CV_GRAY2BGR);
    for (int i = 0; i < src.rows; i++){
        for (int j = 0; j < src.cols; j++){
            cout << (int)src.data[i*src.step + j * 3] << " ";
            cout << (int)src.data[i*src.step + j * 3 + 1] << " ";
            cout << (int)src.data[i*src.step + j * 3 + 2] << " ";
        }
        cout << endl;
    }
    return 0;
}
5、图片的灰度化

方法一:读取时修改参数,默认参数是1,当为0时,按灰度图读取。(注意即使原图是黑白图,默认也是以真色彩读取)

方法二:cvtColor(src,dst,转变形式)函数的使用

方法三:根据彩色图像转换成灰度图像的公式:Gray=0.11*B + 0.59*G + 0.3*R;

#include<stdio.h>
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
//直接读取法
void Gray1(String path){
    Mat src = imread(path,0);
    imshow("gray1", src);
    waitKey();
}
//间接函数调用法
void Gray2(String path){
    Mat src = imread(path);
    cvtColor(src,src,CV_BGR2GRAY);
    imshow("gray2", src);
    waitKey();
}
//遍历像素法
void Gray3(String path){
    Mat src = imread(path);
    int height = src.rows;
    int width = src.cols;
    int step1 = src.step;
    Mat dst(Size(src.cols, src.rows), CV_8UC1);
    int step2 = dst.step;
    int channels = src.channels();
    //注意src和dst的step不一样
    for (int i = 0; i < height; i++){
        for (int j = 0; j < width; j++){
            dst.data[i*step2 + j] = (int)(0.11*src.data[i*step1 + channels * j] + 0.59*src.data[i*step1 + channels * j + 1] + 0.3*src.data[i*step1 + channels * j + 2]);
        }
    }
    imshow("gray3", dst);
    waitKey();
}
int main(){
    //路径
    String path = "C:\\Users\\HQH\\Desktop\\test.jpg";
    Gray1(path);
    Gray2(path);
    Gray3(path);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值