1. 程式人生 > >數字影象處理(一)

數字影象處理(一)

 

測試環境:Win10+Matlab2018

1.隨機生成灰度圖

X = rand(25, 25);
Y = round(255*X);
class(Y)  %判斷型別
Z = 255 - Y; %將灰度值進行反轉
subplot(1,2,1)
imshow(uint8(Y))
xlabel('Y')
subplot(1,2,2)
imshow(uint8(Z))
xlabel('Z')

2.隨機生成彩色圖

rgb = rand(25,25,3)
Im_color = round(rgb*255);
figure(4)
size(Im_color)
class(Im_color)
imshow(uint8(Im_color))

3.讀一幅影象,將顏色翻轉再輸出

B = imread('lighthouse.png');
C = 255 - B;
subplot(1,2,1)
imshow(B)
xlabel('B')
subplot(1,2,2)
imshow(C)
xlabel('C')

4.讀圖並繪製其直方圖

Img = imread('lighthouse.png');
Img = rgb2gray(Img);

count = tabulate(Img(:)); %統計灰度值的頻率[灰度值;頻率;百分比]
temp = count(:,2);
[c,m] = max(temp); %找到頻率最大的灰度值

subplot(1,2,1)
imshow(Img)
xlabel('灰度圖')
subplot(1,2,2)
bar(count(:,2))
text(m,temp(m),num2str(m),'VerticalAlignment','bottom',...
'HorizontalAlignment','center','FontSize',9,'color','r','FontWeight','bold')
xlabel('灰度直方圖')