MATLAB图像处理之图像配准实战

onion = imread(‘onion.png’);%读取onion图像
peppers = imread(‘peppers.png’);%读取peppers图像
figure; subplot(121)
imshow(onion)%显示onion图像
subplot(122),
imshow(peppers)%显示peppers图像
rect_onion = [111 33 65 58];%确定onion图像的区域
rect_peppers = [163 47 143 151];%确定peppers图像的区域
sub_onion = imcrop(onion,rect_onion);%对onion图像裁剪
sub_peppers = imcrop(peppers,rect_peppers);%对peppers图像裁剪
figure; subplot(121),
imshow(sub_onion)%显示裁剪后的onion子图像
subplot(122),
imshow(sub_peppers)%显示裁剪好的peppers子图像
c = normxcorr2(sub_onion(:,:,1),…
sub_peppers(:,:,1));%对红色色带进行归一化互相关
figure,
surf©, %显示两幅子图像的归一化互相关
shading flat
[max_c, imax] = max(abs(c());%确定归一化互相关最大值及其位置
[ypeak, xpeak] = ind2sub(size©,imax(1));%把一维坐标变为二维坐标
corr_offset = [(xpeak-size(sub_onion,2))
(ypeak-size(sub_onion,1))];%利用相关找到的偏移量
rect_offset = [(rect_peppers(1)-rect_onion(1))
(rect_peppers(2)-rect_onion(2))];%位置引起的偏移量
offset = corr_offset + rect_offset;%总的偏移量
xoffset = offset(1);%x轴方向的偏移量
yoffset = offset(2);%y轴方向的偏移量
xbegin = round(xoffset+1);%x轴起始位置
xend = round(xoffset+ size(onion,2));%x轴结束位置
ybegin = round(yoffset+1);%y轴起始位置
yend = round(yoffset+size(onion,1));%y轴结束位置
extracted_onion = peppers(ybegin:yend,xbegin:xend,:);%提取onion子图
if isequal(onion,extracted_onion)
disp(‘onion.png was extracted from peppers.png’)%判断两幅图是否相同
end
recovered_onion = uint8(zeros(size(peppers)));
recovered_onion(ybegin:yend,xbegin:xend,:) = onion;%恢复的onion图像
figure,
imshow(recovered_onion)%显示恢复的onion图像
[m,n,p] = size(peppers);%peppers图像大小
mask = ones(m,n);
i = find(recovered_onion(:,:,1)==0);
mask(i) = .2; %可使用不同的值进行实验
figure,
imshow(peppers(:,:,1)) %显示peppers图像的红色色带
hold on
h = imshow(recovered_onion); % 显示恢复的onion图像
set(h,‘AlphaData’,mask)
MATLAB图像处理之图像配准实战_第1张图片
MATLAB图像处理之图像配准实战_第2张图片
MATLAB图像处理之图像配准实战_第3张图片
MATLAB图像处理之图像配准实战_第4张图片
MATLAB图像处理之图像配准实战_第5张图片

你可能感兴趣的:(MATLAB可视化,opencv,计算机视觉,matlab,机器学习)