Matlab image processing (1) Convert color image to grayscale image (beginner must read)

Teach you to use Matlab to convert color images to grayscale images

RGB = imread('F:/1/tuxiang.jpg');%将图像读入工作区
Y = rgb2gray(RGB);%将图像灰度化
imshow(Y)%显示灰度图像

This is the most basic code for converting a color image to a grayscale image, and the code is explained in detail next:

'RGB' is set by itself, and can be replaced by any other letter, which can be a or b

'imread' is a function that comes with matlab, which means reading the following objects into the workspace is an essential step

( 'F:/1/hetao.jpg' ) is the file path, this should be set by yourself, pay attention to use English quotation marks ' '

Y and RGB are the same

'rgb2gray' is a function that comes with matlab, which means to grayscale the image in the brackets behind

'imshow' is a function that comes with matlab, which means to display the image that has been grayscaled

Note that if you want to save the image, remember to save it in jpg format. Generally, the fig format is saved by default and cannot be used later.

If you want to compare the grayscale image with the original image, you can use the imshowpair function

RGB = imread('F:/1/hetao.jpg');%将图像读入工作区
Y = rgb2gray(RGB);%将图像灰度化
imshowpair(RGB,Y,'montage') %将原始图像与其灰度图像并排显示

Montage means editing, 'imshowpair' is a function that comes with matlab, which means to compare and display the following two images, and montage combines the two images into one image

Guess you like

Origin blog.csdn.net/m0_51140097/article/details/124216675