对给定的两幅图像(灰度、真彩)使用变换矩阵进行坐标变换(同时缩放、旋转),显示 变换后图像及信息数据。 要求:1) 旋转:以图像左下角为圆心点,逆时针旋转 30 度 2) 缩放:sx=0.9,sy=1.8 3) 将变换后的图像写出在文件中,并通过此文件获取图像信息与原图像进行比 较。
时间: 2024-02-09 08:12:12 浏览: 41
以下是MATLAB代码实现:
```matlab
% 读取灰度图像
img_gray = imread('gray_img.png');
% 显示灰度图像
figure;
imshow(img_gray);
title('Original Gray Image');
% 读取真彩图像
img_color = imread('color_img.png');
% 显示真彩图像
figure;
imshow(img_color);
title('Original Color Image');
% 定义变换矩阵
theta = 30 * pi / 180; % 旋转角度
sx = 0.9; % x方向缩放比例
sy = 1.8; % y方向缩放比例
T = [cos(theta), -sin(theta), 0; sin(theta), cos(theta), 0; 0, 0, 1] * [sx, 0, 0; 0, sy, 0; 0, 0, 1];
% 对灰度图像进行坐标变换
tform_gray = affine2d(T);
img_gray_transformed = imwarp(img_gray, tform_gray);
% 显示变换后的灰度图像
figure;
imshow(img_gray_transformed);
title('Transformed Gray Image');
% 对真彩图像进行坐标变换
tform_color = affine2d(T);
img_color_transformed = imwarp(img_color, tform_color);
% 显示变换后的真彩图像
figure;
imshow(img_color_transformed);
title('Transformed Color Image');
% 将变换后的图像写出到文件中
imwrite(img_gray_transformed, 'gray_img_transformed.png');
imwrite(img_color_transformed, 'color_img_transformed.png');
% 获取变换前后的图像信息比较
info_gray = imfinfo('gray_img.png');
info_gray_transformed = imfinfo('gray_img_transformed.png');
info_color = imfinfo('color_img.png');
info_color_transformed = imfinfo('color_img_transformed.png');
% 打印输出图像信息比较结果
disp('Gray Image Information Comparison:');
disp(['Original Image Size: ', num2str(info_gray.Width), ' x ', num2str(info_gray.Height)]);
disp(['Transformed Image Size: ', num2str(info_gray_transformed.Width), ' x ', num2str(info_gray_transformed.Height)]);
disp(' ');
disp('Color Image Information Comparison:');
disp(['Original Image Size: ', num2str(info_color.Width), ' x ', num2str(info_color.Height)]);
disp(['Transformed Image Size: ', num2str(info_color_transformed.Width), ' x ', num2str(info_color_transformed.Height)]);
```
需要注意的是,该代码中的`gray_img.png`和`color_img.png`分别代表灰度图像和真彩图像的文件名。在运行代码时,需要将这两个文件放在MATLAB当前工作目录下。另外,该代码中的变换矩阵是根据题目中给出的参数手动计算得到的,如果题目中给出的参数有误,需要相应地修改变换矩阵的定义。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)