Can anyone with MATLAB installed test my code?

  • Context: MATLAB 
  • Thread starter Thread starter klawson88
  • Start date Start date
  • Tags Tags
    Code Matlab Test
Click For Summary
SUMMARY

The discussion centers on a MATLAB code snippet designed to perform an affine transformation between two images. The user requires assistance testing the code due to a lack of access to MATLAB on campus before an impending assignment deadline. Key issues identified include incorrect function calls, specifically using 'Disp' instead of 'disp', and the need to replace 'cp2form' with 'cp2tform' from the Image Processing Toolbox. The user has also corrected rounding issues in the code to ensure proper functionality.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of image processing concepts
  • Knowledge of affine transformations
  • Access to MATLAB's Image Processing Toolbox
NEXT STEPS
  • Learn about MATLAB's 'ginput' function for point selection in images
  • Explore the 'imtransform' function for image manipulation in MATLAB
  • Study the use of 'cp2tform' for affine transformations in image processing
  • Investigate error handling in MATLAB scripts to improve code robustness
USEFUL FOR

Students and professionals in image processing, MATLAB programmers, and anyone needing to perform affine transformations on images for academic or practical applications.

klawson88
Messages
3
Reaction score
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.

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');
 

Attachments

Physics news on Phys.org
Function cp2tform in frameit.m is a part of Image Processing Toolbox. I don't have it, so the execution didn't complete. But here are some other errors I have corrected:

1. In wrapper.m, lines 10 and 18, Disp will be disp (no capital)

2. In wrapper.m, change lines 28 and 29 to this:
Matlab:
tarX = round(tarX);
tarY = round(tarY);
sourX = round(sourX);
sourY = round(sourY);

3. In frameit.m, cp2form will be cp2tform
 

Similar threads

Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
1
Views
8K