点运算(对比度增强、对比度拉伸或灰度变换),是一种通过图像中的每一个像素值(即像素点上的灰度值)进行运算的图像处理方式。
一、
(1)编写用于图像移动的函数move.m:
function J=move(I,a,b);
%定义一个函数名字move,I表示输入图像,a和b描述I图像沿着x轴和y轴移动的距离
%不考虑平移以后,图像溢出情况,找不到对应点的地方都赋值为1
[M,N,G]=size(I);
I=im2double(I);
J=ones(M,N,G); %初始化新图像矩阵全为1,大小与输入图像相同
for i=1:M
for j=1:N
if((i+a)>=1&&(i+a<=M)&&(j+b>=1)&&(j+b<=N)); %判断平移以后行列坐标是否超出范围
J(i+a,j+b,:)=I(i,j,:); %进行图像平移
end
end
end
(2)调用函数move(),实现对原图像的移动:
close all;
clear all;
clc;
I=imread('lenna.bmp');
a=50;b=50; %设置平移坐标
J1=move(I,a,b); %移动原图像
a=-50;b=50; %设置平移坐标
J2=move(I,a,b); %移动原图像
a=50;b=-50; %设置平移坐标
J3=move(I,a,b); %移动原图像
a=-50;b=-50; %设置平移坐标
J4=move(I,a,b); %移动原图像
figure;
subplot(1,2,1),imshow(J1),axis on;
subplot(1,2,2),imshow(J2),axis on;
figure;
subplot(1,2,1),imshow(J3),axis on;
subplot(1,2,2),imshow(J4),axis on;
二、
编写用于图像移动的函数move1.m:
function J=move(I,a,b);
%定义一个函数名字move,I表示输入图像,a和b描述I图像沿着x轴和y轴移动的距离
%考虑平移以后图像溢出情况,采用扩大显示区域的方法
[M,N,G]=size(I);
I=im2double(I);
J=ones(M+abs(a),N+abs(b),G); %初始化新图像矩阵全为1,大小根据考虑x轴和y轴的平移范围
for i=1:M
for j=1:N
if(a<0 && b<0); %如果进行右下移动,对新图像矩阵进行赋值
J(i,j,:)=I(i,j,:);
else if(a>0 && b>0); %如果进行右上移动,对新图像矩阵进行赋值
J(i+a,j+b,:)=I(i,j,:);
else if(a>0 && b<0); %如果进行左上移动,对新图像矩阵进行赋值
J(i+a,j,:)=I(i,j,:);
else %如果进行左下移动,对新图像矩阵进行赋值
J(i,j+b,:)=I(i,j,:);
end
end
end
end
end
close all;
clear all;
clc;
[I,map]=imread('peppers.png');
Ta = maketform('affine',...
[cosd(30)-sind(30) 0; sind(30) cosd(30) 0; 0 0 1]'); %创建旋转参数结构体
Ia=imtransform(I,Ta); %实现图像旋转
Tb=maketform('affine',[5 0 0; 0 10.5 0; 0 0 1]); %创建缩放参数结构体
Ib=imtransform(I,Tb); %实现图像缩放
xform=[1 0 55; 0 1 115; 0 0 1]'; %创建图像平移参数结构体
Tc=maketform('affine',xform);
Ic=imtransform(I,Tc,'XData',.. %进行图像平移
[1 (size(I,2)+xform(3,1))],'YData',...
[1 (size(I,1)+xform(3,2))],'FillValues',255);
Td=maketform('affine',[1 4 0; 2 1 0; 0 0 1]); %创建图像整体切变得参数结构体
Id=imtransform(I,Td,'FillValues',255); %实现图像整体切变
figure,
subplot(121),imshow(Ia),axis on;
subplot(121),imshow(Ib),axis on;
figure,
subplot(121),imshow(Ic),axis on;
subplot(121),imshow(Id),axis on;
I=imread('pepper.png');
fun=@(block_struct) imrotate(block_struct.data,30); %获取分离块操作的函数句柄
I1=blockproc(I,[64 64],fun); %进行分离块操作
fun=@(block_struct) std2(block_struct.data); %获取分离块操作的函数句柄
I2=blockproc(I,[32 32],fun); %进行分离块操作
fun=@(block_struct) block_struct.data(:,:,[3 1 2]); %获取分离块操作的函数句柄
blockproc(I,[100 100],fun,'Destination','brg_pepper.tif'); %进行分离块操作
figure;
subplot(131),imshow(I1);
subplot(132),imshow(I2,[]);
subplot(133),imshow('brg_peppers.tif');
I=imread('pout.tif');
BW1=roicolor(I,55,100); %基于灰度图像ROI区域选取
c=[87 171 201 165 79 32 87];
r=[133 133 205 259 259 209 133]; %定义ROI顶点位置
BW=roipoly(I,c,r); %根据c和r选择ROI区域
I1=roifill(I,BW); %创建motion滤波器并说明参数
h=fspecial('motion',20,45); %进行区域滤波
I2=roiflt2(h,I,BW);
figure,
subplot(121),imshow(BW1); %显示处理结果
subplot(121),imshow(BW); %显示ROI区域
figure,
subplot(121),imshow(I1); %显示填充效果
subplot(121),imshow(I2); %显示区域滤波效果