2. Matlab image three methods gray value processing

1. Basic knowledge

Color image : Each pixel is represented by three components of R, G, and B, and the value range of each channel is 0~255. (Usually a color image is composed of three pages, which are R, G, and B, and each page is a two-dimensional matrix)

Grayscale image : An image with only one sampled color per pixel, usually displayed as a gray scale from the darkest black to the brightest white. The gray value is distributed between 0 and 255.

Binary image (black and white image): There are only two possibilities for each pixel, 0 and 1.0 represent black, and 1 represents white. The data type is usually 1 binary bit.

2. Three methods of image grayscale

Maximum value method : Make the value of R, G, and B equal to the largest of the 3 values, R=G=B=max (R, G, B), and the maximum value method will form a grayscale image with high brightness.

Average method : Calculate the average value from the values ​​of R, G, and B, R=G=B=(R+G+B)/3, and the average method will form a softer grayscale image.

Weighted average method : assign different weights to R, G, and B according to importance or other indicators, and make the values ​​of R, G, and B weighted average, R=G=B=WR+VG+UB,W,V , and U represent the weight respectively. Studies have shown that people have the highest sensitivity to green, followed by red, and the lowest sensitivity to blue, so W>V>U. Experiments and theory prove that when W=0.30, V=0.59, When U=0.11, the most reasonable grayscale image can be obtained.

%代码如下
clc;%clc的作用就是清屏幕
clear;%clear是删除所有的变量
close all;%close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\1.jpg');%绝对路径的读取
[n m a]=size(i);%判断图像的大小
z= rgb2gray(i);%调用MATLAB函数实现灰度化
i1=zeros(n,m);
i2=zeros(n,m);
i3=zeros(n,m);

for x=1:n%通过双循环对图像进行灰度化处理
    for y=1:m
     i1(x,y)=max(i(x,y,1),max(i(x,y,2),i(x,y,3)));  %第一种方法实现灰度化
      i2(x,y)=(i(x,y,1)+i(x,y,2)+i(x,y,3))/3;%第二种方法实现灰度化
        i3(x,y)=0.3*i(x,y,1)+0.59*i(x,y,2)+0.11*i(x,y,3);%第三种方法实现灰度化
    end
end
figure,imshow(i);title('原图像')
figure,imshow(z);title('调用系统函数实现灰度化')
figure,imshow(uint8(i1));title('第一种方法')
figure,imshow(uint8(i2));title('第二种方法')
figure,imshow(uint8(i3));title('第三种方法')

insert image description here

%也可以把地下显示代码改成
subplot(231);imshow(i);title('原图像')
subplot(232);imshow(z);title('调用系统函数实现灰度化')
subplot(233);imshow(uint8(i1));title('第一种方法')
subplot(234);imshow(uint8(i2));title('第二种方法')
subplot(235);imshow(uint8(i3));title('第三种方法')

insert image description here

Guess you like

Origin blog.csdn.net/qq_55433305/article/details/126897890