Matlab image processing (1) Convert color images to grayscale images (must read for beginners)

Teach you step by step how to convert color images into grayscale images using Matlab

RGB = imread('F:/1/tuxiang.jpg');%Read the image into the workspace
Y = rgb2gray(RGB);% Grayscale the image
imshow(Y)% displays grayscale image

This is the most basic code to convert a color image into a grayscale image. The code is explained in detail below:

‘RGB’ is set by yourself and can be replaced by any other letter, it can be a or b

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

(‘F:/1/hetao.jpg’) is the file path. You need to set this yourself. Be careful to use English quotation marks ‘ ‘

Same for Y and RGB

‘rgb2gray’ is matlab’s own function, which means to grayscale the image in the brackets.

‘imshow’ is a built-in function of matlab, which means to display the grayscale image.

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');%Read the image into the workspace
Y = rgb2gray(RGB);% Grayscale the image
imshowpair(RGB,Y,'montage') %Display the original image and its grayscale image side by side

Montage means editing. ‘imshowpair’ is a built-in function of matlab, which means to compare and display the following two images. Montage combines the two images into one image.

Related Posts

YOLO series — YOLOV7 algorithm (1): Use a custom data set to run through the YOLOV7 algorithm

Matlab obtains the pitch frequency and formant information of the speech and marks it on the spectrogram

Six-axis robotic arm DIY (4) Mechanical model reconstruction and DH method modeling

3d object detection

Detailed explanation of the usage of fspecial function in matlab for image processing

Complete interpretation of YOLOv5 network structure [source code + hand-drawn network structure + module structure]

2022 “Graduate Students’ Scientific Research Literacy Improvement” Series Public Welfare Lectures CNKI Research Test

Explain the profound in simple terms – understand DeepSORT (Principles) in one article with zero basic knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*