---
# System prepended metadata

title: 利用Matlab處理圖片

---

# 利用Matlab處理圖片
## 運用Matlab程式將圖片轉接灰階與壓縮。
matlab程式:
``` cpp=
 a=imread('s:\Wang.jpg');//讀檔
 graya=rgb2gray(a);//轉灰階
 imshow(graya);//顯示圖片
 b=imresize(graya,1/20);//縮小二十倍
 rom=fopen('C:\Users\ray11\Downloads\rom.txt','wt');//寫入記事本('位置')
//寫入內容
for m=1:6 //圖片高
fprintf(rom,'%d:case(x)\n',m-1);
for y=1:8 //圖片長
fprintf(rom,'%d:data=8''d%d;\n',y,b(m,y));
end
fprintf(rom,'default:data=0;\n endcase\n');
end
```
顯示結果:
![](https://i.imgur.com/Arm6Gm0.png =80%x)

---
## 運用Matlab程式將圖片加入雜訊。
參考網址:https://ww2.mathworks.cn/help/images/ref/imnoise.html#d124e34598
```cpp=
J = imnoise(I,'gaussian') 将方差为 0.01 的零均值高斯白噪声添加到灰度图像 I。

J = imnoise(I,'gaussian',m) 添加高斯白噪声，均值为 m，方差为 0.01。

J = imnoise(I,'gaussian',m,var_gauss) 添加高斯白噪声，均值为 m，方差为 var_gauss。

J = imnoise(I,'localvar',var_local) 添加局部方差为 var_local 的零均值高斯白噪声。

J = imnoise(I,'localvar',intensity_map,var_local) 添加零均值高斯白噪声。噪声的局部方差 var_local 是 I 中图像强度值的函数。图像强度值到噪声方差的映射由向量 intensity_map 指定。

J = imnoise(I,'poisson') 从数据中生成泊松噪声，而不是向数据中添加人为噪声。有关详细信息，请参阅算法。

J = imnoise(I,'salt & pepper') 添加椒盐噪声，默认噪声密度为 0.05。这会影响大约 5% 的像素。

示例
J = imnoise(I,'salt & pepper',d) 添加椒盐噪声，其中 d 是噪声密度。这会影响大约 d*numel(I) 个像素。

J = imnoise(I,'speckle') 使用方程 J = I+n*I 添加乘性噪声，其中 n 是均值为 0、方差为 0.05 的均匀分布随机噪声。

J = imnoise(I,'speckle',var_speckle) 添加方差为 var_speckle 的乘性噪声。
```
### 實作將圖片加入雜訊
``` cpp=
>> a=imread('s:\Wang.jpg');//讀檔
>> graya=rgb2gray(a);//轉灰階
>> imshow(graya);//顯示圖片
>> b=imresize(graya,1/20);//縮小二十倍
>> noisb = imnoise(b,'salt & pepper',0.08);//將圖片b加入胡椒鹽雜訊,後面為要加入的密度
>> rom=fopen('C:\Users\ray11\Downloads\rom.txt','wt');//寫入記事本('位置')
//寫入內容
>> for m=1:6 //圖片高
fprintf(rom,'%d:case(x)\n',m-1);
for y=1:8 //圖片長
fprintf(rom,'%d:data=8''d%d;\n',y,nois(m,y));
end
fprintf(rom,'default:data=0;\n endcase\n');
end
```
### 結果:
原圖

加入胡椒鹽的原圖
![](https://i.imgur.com/8fjQ7p8.png =80%x)
像素值
![](https://i.imgur.com/twNMbr8.png)
記事本資料
![](https://i.imgur.com/X7pRZhb.png =80%x)

---
## 運用Matlab程式將圖片自訂位置剪貼

先將特定的範圍進行減下，之後建立一張全黑的圖片將減下的部分貼至全黑圖片上。
``` cpp= 
%裁減頭部位置
img = imread('D:\myimage2.png');

% Specify the position of the crop
x = 170;  % x-coordinate of top-left corner of crop
y = 150;  % y-coordinate of top-left corner of crop
width = 60;  % width of the crop
height = 60;  % height of the crop

% Crop the image using the specified position
cropped_img = img(y:y+height-1, x:x+width-1, :);

% Display the original and cropped images side-by-side
subplot(1,3,1); imshow(img); title('Original Image');
subplot(1,3,2); imshow(cropped_img); title('Cropped Image');
subplot(1,3,3); imshow(background); title('Finsh Image');

%save the cropped image
imwrite(cropped_img, 'D:\cropped_image.jpg');

%貼至黑色照片上

cropped_img = imread('cropped_image.jpg');

% Create a black image of size 416x416
background = zeros(416, 416, 3, 'uint8');

% Calculate the position to paste the cropped image
x = 1;  % x-coordinate of top-left corner of crop on background
y = 1;  % y-coordinate of top-left corner of crop on background


% Paste the cropped image onto the background
background(y:y+size(cropped_img,1)-1, x:x+size(cropped_img,2)-1, :) = cropped_img;

%save the cropped image
imwrite(background, 'D:\finsh_image.jpg');

```
### 結果:
![](https://i.imgur.com/sm26dOq.png)

## 將彩色圖片分別提出R,G,B
``` cpp=
image = imread('D:\lena_std.tif');
[height, width, channels] = size(image);
gray_image = rgb2gray(image);

for y = 1:height
    for x = 1:width
        red = image(y, x, 1);
        green = image(y, x, 2);
        blue = image(y, x, 3);
        % do something with the pixel values
    end
end

//red
red_channel = image(:,:,1); 
green_channel = zeros(height, width, 'uint8');
blue_channel = zeros(height, width, 'uint8');
red_image = cat(3, red_channel, green_channel, blue_channel);
//green
red_channe2 = zeros(height, width, 'uint8');
green_channe2 = image(:,:,1);
blue_channe2 = zeros(height, width, 'uint8');
green_image = cat(3, red_channe2, green_channe2, blue_channe2);
//blue
red_channe3 = zeros(height, width, 'uint8');
green_channe3 = zeros(height, width, 'uint8');
blue_channe3 = image(:,:,1);
blue_image = cat(3, red_channe3, green_channe3, blue_channe3);
//gray
gray_image2 = (red_image + green_image + blue_image) /3;


//show
subplot(1,4,1);imshow(red_image);title('Red');
subplot(1,4,2);imshow(green_image);title('Green');
subplot(1,4,3);imshow(blue_image);title('Blue');
subplot(1,4,4);imshow(gray_image2);title('Gray');

```

### 結果:
![](https://i.imgur.com/RGGRF12.png)

## 練習
寫一個Lenabit_108xxx.m Lena 圖轉成灰階後，依照 bit 切成8個 bit 的圖層.用 subplot (4, 2, *) 把 8 層存成一張圖 Lenabit_108xxx.jpg 把 NKUST 的 logo 改成 512*512 二值化(黑白圖), 放到 Lena 的第8及第1bit, 存成Lena_logo8.jpg 及Lena_logo1.jpg 並寫一個 PSNR 程式, 計算Lena_logo8 及 Lena_logo1跟原Lena灰階的 PSNR 值. .

```cpp=
myimage = imread('D:\lena_std.tif');
myimage_gray = rgb2gray(myimage);
watermark = imread('D:\watermark.jpg'); 
watermark_gray = rgb2gray(watermark); 

msb_1 = bitshift(myimage_gray, -7);
msb_2 = bitshift(myimage_gray, -6);
msb_3 = bitshift(myimage_gray, -5);
msb_4 = bitshift(myimage_gray, -4);
msb_5 = bitshift(myimage_gray, -3);
msb_6 = bitshift(myimage_gray, -2);
msb_7 = bitshift(myimage_gray, -1);
msb_8 = bitshift(myimage_gray, 0);


msb_image1 = logical(msb_1);
%msb_image2 = logical(msb_2);
%msb_image3 = logical(msb_3);
%msb_image4 = logical(msb_4);
%msb_image5 = logical(msb_5);
%msb_image6 = logical(msb_6);
%msb_image7 = logical(msb_7);
msb_image8 = logical(msb_8);

% 設定浮水印的透明度
alpha = 0.2;

% 調整浮水印大小與原圖相同
%watermark_gray = imresize(watermark_gray, size(msb_1, 1:2));

% 在原圖上加上浮水印
watermarked_img1 = imlincomb(1, msb_1, alpha, watermark_gray);
watermarked_img2 = imlincomb(1, msb_8, alpha, watermark_gray);

Lena_logo1 = bitor(bitor(bitor(bitor(bitor(bitor(bitor(watermarked_img1, msb_2), msb_3), msb_4), msb_5), msb_6), msb_7), msb_8);
Lena_logo8 = bitor(bitor(bitor(bitor(bitor(bitor(bitor(watermarked_img1, msb_2), msb_3), msb_4), msb_5), msb_6), msb_7), watermarked_img2);

%PSNR1
img1 = im2double(Lena_logo1);
img2 = im2double(Lena_logo8);
img3 = im2double(gray_image);
% 計算 PSNR (Peak Signal-to-Noise Ratio)
max_pixel_value = 1.0;
% 計算 MSE (Mean Squared Error)
mse = mean((img3(:) - img1(:)).^2);

psnr = 10 * log10((max_pixel_value^2) / mse);

%PSNR8
% 計算 MSE (Mean Squared Error)
mse8 = mean((img3(:) - img2(:)).^2);

psnr8 = 10 * log10((max_pixel_value^2) / mse8);

% 顯示 PSNR 值
fprintf('The PSNR between the Lena_logo1 and gray_image is %.2f dB.\n', psnr);
% 顯示 PSNR 值
fprintf('The PSNR between the Lena_logo8 and gray_image is %.2f dB.\n', psnr8);

subplot(2,2,1);
imshow(Lena_logo1);
subplot(2,2,2);
imshow(Lena_logo8);


subplot(2,2,3);
imshow(gray_image);
title('合并后的灰度图像');

```
### 結果:
![](https://i.imgur.com/nRqOPar.png)
