Correct image distortion based on matlab undistortImage function

5 vues (au cours des 30 derniers jours)
chihyu
chihyu le 10 Juin 2022
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)');

Réponses (1)

Githin George
Githin George le 27 Oct 2023
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.

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by