Digital Image Processing Homework1

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Xmax = (1-Xmin)+Xmax
Xmin = 1
Ymax = (1-Ymin)+Ymax
Ymin = 1
forw = 1:Xmax
forv = 1:Ymax
img_out(v,w)=NaN;%initial the output7 image
end
end
forx = 1:N
fory = 1:M
0, 0, 1];
% Tran_m = [2, 0, 0;
% 0, 0.5, 0;
% 0, 0, 1];
Cri_points = [1, 1, 0;
N, 1, 0;
1, M, 0;
N, M, 0];
Cri_points_new = round(Cri_points*Tran_m)
Xmin = min(Cri_points_new(:,1))% minimum for column 1 of Cri_points
%title('Rotation Affine Matrix Forward Mapping')
title('Scaling Affine Matrix Forward Mapping')
%title('Shear(vertical) Affine Matrix Forward Mapping')
%title('Shear(horizontal) Affine Matrix Forward Mapping')
pix4=[N M 1]*rot;
height=round(max([abs(pix1(H)-pix4(H))+1 abs(pix2(H)-pix3(H))+1]));
width=round(max([abs(pix1(W)-pix4(W))+1 abs(pix2(W)-pix3(W))+1]));
[Digital Image Processing]
[1st Mini-Project Report]
Part1.Technical Discussion of Results
Forward Mapping Procedures(rotation affine matrix):
1. Image read in function gets thematrix including the location of pixels and the grey value on each point; 2.Take four end points' location matrix; 3. Transform these four points into new four points by time the rotation affine matrix; 4. Get the size of new image, and get the x-y shift value to convert the location into regular coordinates; 5. Using double loops to transform each point with adding shift value; 6. Mapping the grey value from original points' location to new points' location.
imgn=zeros(height,width);
delta_y=abs(min([pix1(H) pix2(H) pix3(H) pix4(H)]));
delta_x=abs(min([pix1(W) pix2(W) pix3(W) pix4(W)]));
fory=1-delta_y:height-delta_y
Summary for Other Results (scaling, shear vertical, shear horizontal matrix):
The inverse mapping with shear(vertical) affine matrix failed. Other situations are all good.And the background theory is the same as withrotation affine matrixdiscus
Forward Mapping:
Inverse Mapping:
Part3. Appendix: Program listings
Forward Mapping:
clearall;
clc
warning('off','all');
img_color = imread('figs\wcu_logo_s.jpg');
forx=1-delta_x:width-delta_x
pix=[y x 1]*inv_rot;
ifpix(H)>=1 && pix(W)>=1 && pix(H)<=N && pix(W)<=M
ifrem(pix(H),1)==0 && rem(pix(W),1)==0
imgn(y+delta_y,x+delta_x)=img(pix(H),pix(W));
Xmax = max(Cri_points_new(:,1))% maximum for column 1 of Cri_points
Ymin = min(Cri_points_new(:,2))% minimum for column 2 of Cri_points
Ymax = max(Cri_points_new(:,2))% maximum for column 2 of Cri_points
Summary for Results (rotation affine matrix):
The dark points disappear comparing to forward mapping. Because all the points in new image were inversely transformed to original image. So, no point's location is empty. Also, by using the interpolation function to approximate the grey value can improve the precision.
Y1=single(Y1);
interp_result1=interp1(X1,Y1,pix(H),'nearest');
imgn(y+delta_y,x+delta_x)=interp_result1;
elseifrem(pix(H),1)==0 && rem(pix(W),1)~=0
X2 = floor(pix(W)):1:ceil(pix(W));
p = [x, y, 1];
tp = round(p*Tran_m + [1-min(Cri_points_new(:,1)), 1-min(Cri_points_new(:,2)),0]);%
img_out(tp(2),tp(1))=img_in(y,x);
end
end
figure; imshow(img_out,[min(img_out(:)),max(img_out(:))])
theta=pi/4;
rot=[cos(theta) sin(theta) 0;-sin(theta) cos(theta) 0;0 0 1];
% rot=[1 1 0;0 1 0;0 0 1];
inv_rot=inv(rot);
pix1=[1 1 1]*rot;
pix2=[1 M 1]*rot;
pix3=[N 1 1]*rot;
Y2 = [img(pix(H),floor(pix(W))) img(pix(H),ceil(pix(W)))];
interp_result2=interp1(X2,Y2,pix(W),'nearest');
imgn(y+delta_y,x+delta_x)=interp_result2;
else
Summary for Results(rotation affine matrix):
There are a lot of dark points in new image. It seems like that these dark points has some regular rule. The reason for these dark points is that forward mapping miss a lot of points during the transformation. So, the missing points will be empty. However, we can interpolate these empty points according to nearest neighbors to make the image looks better.
elseifrem(pix(H),1)~=0 && rem(pix(W),1)==0
pix(W)=single(pix(W));
pix(H)=single(pix(H));
X1 = floor(pix(H)):1:ceil(pix(H));
X1=single(X1);
Y1 = [img(floor(pix(H)),pix(W)) img(ceil(pix(H)),pix(W))];
InverseMapping Procedures(rotation affine matrix):
1. Image read in function gets the matrix including the location of pixels and the grey value on each point; 2.Take four end points' location matrix; 3. Inversely transform these four points into new four points by time the rotation affine matrix; 4. Get the size of new image, and get the x-y shift value to convert the location into regular coordinates; 5. Using double loops to inversely transform each point from new image with adding shift value to original image; 6. Using interpolation function to get the interpolation greyvalue of each transformed point; 7. Mapping these grey values to new image.
Inverse Mapping:
clearall;
clc
warning('off','all');
H=1;
W=2;
img_color=imread('figs\wcu_logo_s.jpg');
img=img_color(:,:,1);
[N M]=size(img);% N stands for vertical height, M stands for horizontal width
X3 = floor(pix(H)):1:ceil(pix(H));
Y3 = floor(pix(W)):1:ceil(pix(W));
Z3 = [img(floor(pix(H)),floor(pix(W))) img(floor(pix(H)),ceil(pix(W)))
img_in = img_color(:,:,1);% the R channel, to be 2D
[M,N] = size(img_in);% M is Row, N is Columun
theta=pi/4;
Tran_m = [cos(theta), -sin(theta), 0;
sin(theta), cos(theta), 0;
相关文档
最新文档