用2023a版matlab写一段将四副256*256的灰度图像I1、I2、I3、I4进行图像归一化处理,图像归一化的原理是:首先计算并确定原始图像对各种仿射变换具有较强不变性的图像矩,然后根据得到的图像矩确定变换函数的具体参数,最后利用变换函数将原始图像转换为具有标准形式的图像,其归一化过程主要包括4个步骤:平移归一化、X-shear归一化、Y-shear归一化和缩放归一化
时间: 2023-10-25 15:14:34 浏览: 82
灰度图像彩色化 matlab
4星 · 用户满意度95%
以下是代码实现:
```matlab
% 读入四幅256*256的灰度图像
I1 = imread('image1.png');
I2 = imread('image2.png');
I3 = imread('image3.png');
I4 = imread('image4.png');
% 计算图像矩
m00_1 = sum(sum(I1));
m10_1 = sum(sum(bsxfun(@times, I1, (1:size(I1,2)))));
m01_1 = sum(sum(bsxfun(@times, I1, (1:size(I1,1))')));
m11_1 = sum(sum(bsxfun(@times, bsxfun(@times, I1, (1:size(I1,2))), (1:size(I1,1))')));
m20_1 = sum(sum(bsxfun(@times, I1, (1:size(I1,2)).^2)));
m02_1 = sum(sum(bsxfun(@times, I1, (1:size(I1,1)').^2)));
m00_2 = sum(sum(I2));
m10_2 = sum(sum(bsxfun(@times, I2, (1:size(I2,2)))));
m01_2 = sum(sum(bsxfun(@times, I2, (1:size(I2,1))')));
m11_2 = sum(sum(bsxfun(@times, bsxfun(@times, I2, (1:size(I2,2))), (1:size(I2,1))')));
m20_2 = sum(sum(bsxfun(@times, I2, (1:size(I2,2)).^2)));
m02_2 = sum(sum(bsxfun(@times, I2, (1:size(I2,1)').^2)));
m00_3 = sum(sum(I3));
m10_3 = sum(sum(bsxfun(@times, I3, (1:size(I3,2)))));
m01_3 = sum(sum(bsxfun(@times, I3, (1:size(I3,1))')));
m11_3 = sum(sum(bsxfun(@times, bsxfun(@times, I3, (1:size(I3,2))), (1:size(I3,1))')));
m20_3 = sum(sum(bsxfun(@times, I3, (1:size(I3,2)).^2)));
m02_3 = sum(sum(bsxfun(@times, I3, (1:size(I3,1)').^2)));
m00_4 = sum(sum(I4));
m10_4 = sum(sum(bsxfun(@times, I4, (1:size(I4,2)))));
m01_4 = sum(sum(bsxfun(@times, I4, (1:size(I4,1))')));
m11_4 = sum(sum(bsxfun(@times, bsxfun(@times, I4, (1:size(I4,2))), (1:size(I4,1))')));
m20_4 = sum(sum(bsxfun(@times, I4, (1:size(I4,2)).^2)));
m02_4 = sum(sum(bsxfun(@times, I4, (1:size(I4,1)').^2)));
% 计算变换函数参数
tx = -m10_1/m00_1;
ty = -m01_1/m00_1;
a = (m20_1/m00_1-tx^2)/(m00_1-ty^2);
b = (m11_1/m00_1-tx*ty)/(m00_1-ty^2);
c = (m02_1/m00_1-ty^2)/(m00_1-tx^2);
% 平移归一化
T1 = [1 0 tx; 0 1 ty; 0 0 1];
I1_norm = imwarp(I1, affine2d(T1));
% X-shear归一化
T2 = [1 0 0; b 1 0; 0 0 1];
I1_norm = imwarp(I1_norm, affine2d(T2));
% Y-shear归一化
T3 = [1 a 0; 0 1 0; 0 0 1];
I1_norm = imwarp(I1_norm, affine2d(T3));
% 缩放归一化
T4 = [1 0 0; 0 c 0; 0 0 1];
I1_norm = imwarp(I1_norm, affine2d(T4));
% 重复上述过程对I2、I3、I4进行归一化处理
% ...
% 显示归一化后的四幅图像
figure;
subplot(2,2,1); imshow(I1_norm);
subplot(2,2,2); imshow(I2_norm);
subplot(2,2,3); imshow(I3_norm);
subplot(2,2,4); imshow(I4_norm);
```
需要注意的是,上述代码中只给出了对一副图像的归一化处理过程,需要将其重复四次分别对`I1`、`I2`、`I3`、`I4`进行处理。此外,由于图像归一化的过程涉及到多次仿射变换,可以使用`imwarp`函数实现。
阅读全文