Correct image distortion based on matlab undistortImage function

5 次查看(过去 30 天)
hi,
I obtain the camera intrinsic and distortion parameters through the calibration, and I want to correct the image of the test piece of the experiment. The code is presented in below, but error is also emerged. can someone knowledge that what's going on
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
IntrinsicMatrix = [5512.29 0 0; 0 5506.17 0; 614.99 255.44 1];
radialDistortion = [-0.09527 4.23896];
cameraParams = cameraParameters('IntrinsicMatrix',IntrinsicMatrix,'RadialDistortion',radialDistortion);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray{k} = imread(fullFileName);
%imshow(imageArray{k}); % Display image.
undistortImage{k} = undistortImage(imageArray{k},cameraParams);
drawnow; % Force display to update immediately.
end
figure; imshowpair(imresize(imageArray{1},0.5),imresize(undistortImage{1},0.5),'montage');
title('Original Image (left) vs. Corrected Image (right)');

回答(1 个)

Githin George
Githin George 2023-10-27
Hello,
I understand you are facing an error when calling the “undistortImage” function.
I tried to reproduce this issue at my end, and I noticed that the problem is caused by the following line.
undistortImage{k} = undistortImage(imageArray{k},cameraParams);
Note that you are shadowing the function “undistortImage” after first function call by declaring a variable with the same name.
Therefore, changing the variable name in the LHS of the assignment statement will fix the issue.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by