1、Image Processing Figure 2.1 Figure 2.2In order to get a good transform, the points should be chosen that the spans between points are enough to obtain the correct ratio of two images.4. Create and execute an m-file that uses the set of corresponding points and creates a transformation structure (use
2、 maketform or cp2tform).The relative code is shown below, and the parameter input_points and base_points are defined in the last step by command cpselect.TFORM = cp2tform(input_points, base_points, projective);5. Extend the m-file of 4: apply the transform to the second image by using the transforma
3、tion structure in imtransform. Hint: use the options XData, YData and XYscale to assure that the output image is large enough to contain both images (read the help of imtransform!). In order to find the right position you may want to calculate where the corners of the second image should be located
4、in the transformed image. You can calculate that using tformfwd.The code is shown as below, the figure of transformed image2 is shown with x and y axis in Image Processing figure, imshow(img2_tr,XData,xdata,YData,ydata), axis onfigure, imshow(img1), axis onFigure 3.1 Figure 3.2In order to get the su
5、fficient size of the final image and find the correct location of the image1 and transformed image2 in it, the following parameters are obtained.i j a = size(img1);m n b = size(img2_tr);x = round (xdata(1);y = round (-ydata(1);Because xdata(1)0 and ydata(1)0 is also added for more general use.column
6、_size = max(m, y+i);row_size = max(x+n, j);Image Processing the correct location of im2_tr in the extended image should be from Y=1 to Y=m and X=x+1 to X=x+n. Following is the code building the new matrix of the extended size and locating the two images in each extended image.img_out = uint8(zeros(c
7、olumn_size, row_size, 3);img1_large = uint8(zeros(column_size, row_size, 3);img2_large = uint8(zeros(column_size, row_size, 3);img1_large(y+1):(y+i), 1:(j), 1:3) = img1;img2_large(1:(m), (x+1):(x+n), 1:3) = img2_tr;The result for image1 and image2 are shown in Figure 5.1 and Figure 5.2 respectively.
8、Image Processing Figure 67. Show the result on the screen and evaluate the result. Can you identify possible problems?As seen in the Figure 6, the overlapped area of the sum of two images is much brighter than before. That is because the RGB value of any point in this area is the sum of the two RGB
9、values of two original images. In order to solve the problem, the command imsubtract is used to remove some part of the RGB value, in my code, the new RGB value of any point in this area is mainly decided by that of image1 (new value V = V(img1) V(img2)+ V(img2) = V(img1). The result of images subst
10、raction and the final image are shown in Figure 7 and Figure 8. The code to generate the final image is also included with the command imwrite.img_sub = imsubtract(img1_large, img2_large);figure, imshow(img_sub);img_out = imadd(img_sub,img2_large);imwrite(img_out, output.jpg);Image Processing figure
11、(1);imshow(img1,);img2 = imread(schier_right.jpg);figure(2);imshow(img2,);% find the corresponding points in the two images for transformcpselect(rgb2gray(img2), rgb2gray(img1);pause;% build the transform structureTFORM = cp2tform(input_points, base_points, projective);% apply the transform on the i
12、mage and displayimg2_tr xdata ydata = imtransform(img2, TFORM);figure(3), imshow(img2_tr,XData,xdata,YData,ydata), axis onfigure(4), imshow(img1), axis on% find the size of image1 and transformed image2i j a = size(img1);m n b = size(img2_tr);% extend the two images, ensuring the new image can conta
13、in the stitched image, and correctly locate the img1 and img2_trx = round (xdata(1); if(ydata(1)0)y = round(-ydata(1);column_size = max(m, y+i);row_size = max(x+n, j);img_out = uint8(zeros(column_size, row_size, 3);img1_large = uint8(zeros(column_size, row_size, 3);img2_large = uint8(zeros(column_si
14、ze, row_size, 3);img1_large(y+1):(y+i), 1:(j), 1:3) = img1;img2_large(1:(m), (x+1):(x+n), 1:3) = img2_tr;elsey = round(ydata(1);column_size = max(y+m, i);row_size = max(x+n, j);Image Processing img1_large = uint8(zeros(column_size, row_size, 3);img2_large = uint8(zeros(column_size, row_size, 3);img1
15、_large(1:(i), 1:(j), 1:3) = img1;img2_large(y+1):(y+m), (x+1):(x+n), 1:3) = img2_tr;end% display the directly stitched imagefigure(5), imshow(img1_large);figure(6), imshow(img2_large);figure(7), imshow(imadd(img1_large,img2_large);% imsubtract the two images and display the new stitched imageimg_sub = imsubtract(img1_large, img2_large);figure(8), imshow(img_sub);img_out = imadd(img_sub,img2_large);figure(9), imshow(img_out);% output the final stitched imageimwrite(img_out, output.jpg);