1. 程式人生 > >[matlab影象處理] 灰度化 (自己計算灰度化)

[matlab影象處理] 灰度化 (自己計算灰度化)

close all;%關閉所有視窗
clear;%清除變數的狀態資料
clc;%清除命令列

wangpic=imread('lenargb.bmp');
figure;%開啟一個新視窗
imshow(wangpic);
title('1 lena的原圖');

wangpicgray=rgb2gray(wangpic);
figure;%開啟一個新視窗
imshow(wangpicgray);
title('2lena的灰度圖');

%自己灰度化
[width,heigth,bmsize]=size(wangpic);
myjsgray=uint8(zeros(width,heigth));
for i=1:width
     for j=1:heigth
         myjsgray(i,j)=wangpic(i,j,1)*0.299+wangpic(i,j,2)*0.587+wangpic(i,j,3)*0.114;
     end
end   
figure;
imshow(myjsgray);
title('3 lena 自己計算灰度圖');


wanpic=imread('guazi.bmp');
figure;%開啟一個新視窗
imshow(wanpic);
title('4瓜子的原圖');

wanpicgray=rgb2gray(wanpic);
figure;%開啟一個新視窗
imshow(wanpicgray);
title('5 瓜子的灰度圖');

%自己灰度化
[width,heigth,bmsize]=size(wanpic);
myjsgrayguazi=uint8(zeros(width,heigth));
for i=1:width
     for j=1:heigth
         myjsgrayguazi(i,j)=wanpic(i,j,1)*0.299+wanpic(i,j,2)*0.587+wanpic(i,j,3)*0.114;
     end
end   
figure;
imshow(myjsgrayguazi);
title('6 瓜子 自己計算灰度圖');