0% found this document useful (0 votes)
44 views

Basics: 1. Write A MATLAB Code To Read, Show and Write Image

The document provides MATLAB code examples for basic image processing tasks: 1) Reading, displaying, and writing an image file 2) Converting an RGB image to grayscale using pixel-to-pixel conversion, averaging pixel values, a luminance formula, and an inbuilt MATLAB function 3) Converting an RGB image to black and white by thresholding the luminance value and setting pixels above or below the threshold to 1 or 0, respectively, or using an inbuilt MATLAB function.

Uploaded by

Darshan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Basics: 1. Write A MATLAB Code To Read, Show and Write Image

The document provides MATLAB code examples for basic image processing tasks: 1) Reading, displaying, and writing an image file 2) Converting an RGB image to grayscale using pixel-to-pixel conversion, averaging pixel values, a luminance formula, and an inbuilt MATLAB function 3) Converting an RGB image to black and white by thresholding the luminance value and setting pixels above or below the threshold to 1 or 0, respectively, or using an inbuilt MATLAB function.

Uploaded by

Darshan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment 0

BASICS
1. Write a MATLAB code to Read, Show and Write Image.
clc;
clear all;
close all;
img = uigetfile('*.jpg','Select Image')
a=imread(img);
imshow(a);
imwrite(a,'ohm.jpg');

2. Write a MATLAB code to convert RGB image to grayscale.


clc;
clear all;
close all;
img=uigetfile('*.jpg');
a=imread(img);

Method 1:Pixel to Pixel conversion:

R=a(:,:,1);
G=a(:,:,2);
B=a(:,:,3);
for i=1:1:600
for j=1:1:800
GRAY(i,j)=(0.3*R(i,j))+(0.59*G(i,j))+(0.11*B(i,j));
end
end

Method 2: Averaging

GRAY=(R+G+B)/3;

Method 3: Luminance formula:

GRAY=(0.3*R)+(0.59*G)+(0.11*B);

Method 4: Inbuilt MATLAB function:

Experiment 0
GRAY=rgb2gray(a);

BASICS

imshow(GRAY);

3. Write a MATLAB code to convert RGB image to Monochrome/Binary/Black and White


image.
clc;
clear all;
close all;
a=uigetfile('*.jpg');
imread(a);
for i=1:1:600
for j=1:1:800
X=(0.3*R(i,j))+(0.59*G(i,j))+(0.11*B(i,j));
if (X>150)
GRAY(i,j)=1;
else
GRAY(i,j)=0;
end
end
end
OR:
abw=im2bw(a);
imshow(abw);

You might also like