- #1
klawson88
- 3
- 0
Long story short, we have access to MATLAB on the computers on my campus. My plan was to code at home and return to campus to test my code, but a few things have come up that won't allow me to return to campus today. Deadline for an assignment is fast approaching (couple of hours), I just need to test it before handing it in (online).
All you have to do is specify the location of 2 images on your computer and a name for the result image in wrapper(targetFileName,sourceFileName,resultFileName). What the code is supposed to do is display the source image and ask you to click 4 points on it which surround an area you want to extract. It will then display the "target" image and ask you to click 4 points on that image that encompass the area that you want to replace. It will then perform an affine transformation and place the extracted area to the selected area of the target image and save it as what you put for resultFileName.
The .m files are attached and the source code is below. If anyone could help it would be greatly appreciated! If an error arises please reply and tell me what it is.
Thanks.
All you have to do is specify the location of 2 images on your computer and a name for the result image in wrapper(targetFileName,sourceFileName,resultFileName). What the code is supposed to do is display the source image and ask you to click 4 points on it which surround an area you want to extract. It will then display the "target" image and ask you to click 4 points on that image that encompass the area that you want to replace. It will then perform an affine transformation and place the extracted area to the selected area of the target image and save it as what you put for resultFileName.
The .m files are attached and the source code is below. If anyone could help it would be greatly appreciated! If an error arises please reply and tell me what it is.
Thanks.
Code:
function w = wrapper(targetFileName, sourceFileName, resultFileName)
%Reads the target and source image files in
targetArray = imread(targetFileName);
sourceArray = imread(sourceFileName);
%Shows the source image and asks the user to click on 4 points,
%storing the coordinates in a matrix
Disp('Click on 4 points on the image that encompass the area you want to extract')
imshow(sourceArray);
[sourX,sourY]= ginput(4);
%Shows the target image and asks the user to click on 4 points,
%storing the coordinates in a matrix
Disp('Click on 4 points on the image that encompass the area you want to replace ')
imshow(targetArray);
[tarX,tarY] = ginput(4);
%Rounds the x and y values of the user-selected points to the
%nearest integer
[tarX,tarY] = round([tarX,tarY]);
[sourX,sourY] = round([sourX,sourY]);
%Calls the frameit function
frameit(targetArray, sourceArray, tarX, tarY, sourX, sourY, resultFileName);
Code:
function fi = frameit(targetArray, sourceArray, tarX, tarY, sourX, sourY, resultFileName)
%Performs an affine transformation on the user-selected area of
%the source image to make it fit to the user-selected area of the
%target image
transform = cp2form([sourX,sourY], [tarX,tarY], 'affine');
%Extracts the user-selected area from the source image
subSource = sourceArray([min(sourX):max(sourX)], [min(sourY):max(sourY)], : );
%Resizes the sub-image so that it has the same dimensions as the user
%selected area in the target image
%subSource = imresize(subSource, [max(tarX)-min(tarX), max(tarY)-min(tarY)]);
%Transforms the sub-image according to the transformation that was
%computed before
sourceTransform = imtransform(subSource, transform);
%Replaces the user-selected area in the target image with the transformed
%user-selected area of the source image
targetArray((min(tarX):size(sourceTransform, 1), (min(tarY):size(sourceTransform, 2), : ) = sourceTransform;
%Shows the resulting image and saves it as a jpg file
result = targetArray;
imshow(result);
imwrite(result, resultFileName, 'jpg');