用2023a版matlab写一段将四副256*256的灰度图像I1、I2、I3、I4进行图像归一化处理,图像归一化的原理是:首先计算并确定原始图像对各种仿射变换具有较强不变性的图像矩,然后根据得到的图像矩确定变换函数的具体参数,最后利用变换函数将原始图像转换为具有标准形式的图像,其归一化过程主要包括4个步骤:平移归一化、X-shearing归一化、Y-shearing归一化和缩放归一化,原始图像在经过以上四个步骤后,便能够转化为标准图像,从而能够降低仿射变换对图像的影响
时间: 2023-07-16 12:16:39 浏览: 123
以下是用2023a版matlab编写的代码:
```
% 读入四张灰度图像
I1 = imread('image1.png');
I2 = imread('image2.png');
I3 = imread('image3.png');
I4 = imread('image4.png');
% 计算图像矩
m00 = sum(sum(I1));
m10 = sum(sum(bsxfun(@times, I1, (1:size(I1, 2)))));
m01 = sum(sum(bsxfun(@times, I1, (1:size(I1, 1))')));
m11 = sum(sum(bsxfun(@times, bsxfun(@minus, (1:size(I1, 2)), mean(1:size(I1, 2))), bsxfun(@minus, (1:size(I1, 1))', mean(1:size(I1, 1))))));
% 计算变换函数的参数
tx = -m10/m00;
ty = -m01/m00;
theta = atan2((m01/m00-mean(1:size(I1, 1))'), (m10/m00-mean(1:size(I1, 2))));
sx = sqrt((m11/m00) - (m01/m00)^2)/sqrt((size(I1, 1)^2)+(size(I1, 2)^2));
sy = sqrt((m11/m00) - (m10/m00)^2)/sqrt((size(I1, 1)^2)+(size(I1, 2)^2));
% 平移归一化
tform1 = affine2d([1 0 0; 0 1 0; tx ty 1]);
I1 = imwarp(I1, tform1);
I2 = imwarp(I2, tform1);
I3 = imwarp(I3, tform1);
I4 = imwarp(I4, tform1);
% X-shearing归一化
tform2 = affine2d([1 0 0; sx 1 0; 0 0 1]);
I1 = imwarp(I1, tform2);
I2 = imwarp(I2, tform2);
I3 = imwarp(I3, tform2);
I4 = imwarp(I4, tform2);
% Y-shearing归一化
tform3 = affine2d([1 sy 0; 0 1 0; 0 0 1]);
I1 = imwarp(I1, tform3);
I2 = imwarp(I2, tform3);
I3 = imwarp(I3, tform3);
I4 = imwarp(I4, tform3);
% 缩放归一化
tform4 = affine2d([1 0 0; 0 1 0; 0 0 1]);
scale = max(size(I1));
tform4.T(1,1) = scale/size(I1, 2);
tform4.T(2,2) = scale/size(I1, 1);
I1 = imwarp(I1, tform4);
I2 = imwarp(I2, tform4);
I3 = imwarp(I3, tform4);
I4 = imwarp(I4, tform4);
% 显示归一化后的图像
figure;
subplot(2,2,1); imshow(I1); title('Image 1');
subplot(2,2,2); imshow(I2); title('Image 2');
subplot(2,2,3); imshow(I3); title('Image 3');
subplot(2,2,4); imshow(I4); title('Image 4');
```
代码中首先读入四张灰度图像,然后计算图像矩并确定变换函数的具体参数。接着,按照平移归一化、X-shearing归一化、Y-shearing归一化和缩放归一化的顺序进行图像归一化处理。最后,显示归一化后的四张图像。
阅读全文