How to change figure image to jpg image (image undistortion)

1 Ansicht (letzte 30 Tage)
chee gang ngui
chee gang ngui am 25 Mai 2022
Beantwortet: chee gang ngui am 3 Jun. 2022
IntrinsicMatrix = [4094.8006 0 0; 0 4081.8042 0; 1787.2784 1472.9443 1];
radialDistortion = [-0.1963 0.1539];
tangentialdistortion = [-0.0048 -0.0061];
cameraParams = cameraParameters('IntrinsicMatrix',IntrinsicMatrix,'RadialDistortion',radialDistortion, 'TangentialDistortion',tangentialdistortion);
I = imread ('C:\Users\nguic\Documents\MATLAB\rootimage\capture_2-18-05-22-17:44.jpg')% Direction to read file.
J = undistortImage(I,cameraParams);
figure; imshow(imresize(I,0.5)); # this sentence will generate figure image, so what should I change to get a jpg image?

Antworten (3)

Jan
Jan am 25 Mai 2022
img = imresize(I,0.5);
imwrite(img, 'YourImage.jpg')

Image Analyst
Image Analyst am 26 Mai 2022
I would never use jpg if you ever plan on using that image for image analysis. Use PNG format. It's lossless compression, has none of the bad compression artifacts JPG images can have, and is pretty much the de facto standard these days. Anyway, you can use imwrite like Jan said.
Also, use more descriptive variable names (like you did in the first 4 lines) than I and J. Using single letter variables will soon make your code look like an alphabet soup of a program that is hard to maintain. Add comments too. That helps maintainability.
More robust code:
% Read in input image.
fullInputFileName = 'C:\Users\nguic\Documents\MATLAB\rootimage\capture_2-18-05-22-17:44.jpg'
% Check that the file actually exists.
if ~isfile(fullInputFileName)
% Exit if the image file is not found.
errorMessage = sprintf('Input file not found:\n\n%s', fullInputFileName);
uiwait(errordlg(errorMessage));
return;
end
originalRGBImage = imread(fullInputFileName)% Direction to read file.
% Repair the image by undoing the distortion.
undistortedImage = undistortImage(originalRGBImage, cameraParams);
imshow(undistortedImage, []);
drawnow; % Cause it to refresh screen immediately.
% Resize the image. Cut its size down by half in each dimension.
resizedImage = imresize(undistortedImage, 0.5);
imshow(resizedImage);
drawnow; % Cause it to refresh screen immediately.
% Construct output filename
outputFileName = strrep(fullInputFileName, ':', ''); % Get rid of colons because they are not allowed.
outputFileName = strrep(outputFileName, '.jpg', '.png'); % Replace jpg with png.
% Save the array to disk.
imwrite(resizedImage, outputFileName);
  7 Kommentare
Image Analyst
Image Analyst am 27 Mai 2022
Actually we can't get rid of the drive letter colon so we have to get rid of the colons in your time stamps only after the third character in the filename. Try this:
% Read in input image.
folder = 'C:\Users\nguic\Documents\MATLAB\rootimage'
if ~isfolder(folder)
folder = pwd;
end
fullInputFileName = fullfile(folder, '1.jpg');
% Check that the file actually exists.
if ~isfile(fullInputFileName)
% Exit if the image file is not found.
errorMessage = sprintf('Input file not found:\n\n%s', fullInputFileName);
uiwait(errordlg(errorMessage));
return;
end
originalRGBImage = imread(fullInputFileName); % Direction to read file.
% Repair the image by undoing the distortion.
undistortedImage = undistortImage(originalRGBImage, cameraParams);
subplot(2, 1, 1);
imshow(undistortedImage, []);
drawnow; % Cause it to refresh screen immediately.
% Resize the image. Cut its size down by half in each dimension.
resizedImage = imresize(undistortedImage, 0.5);
subplot(2, 1, 2);
imshow(resizedImage);
drawnow; % Cause it to refresh screen imme
% Construct output filename
% Get rid of colons after column 3 because they are not allowed,
% and the user had some in there because the time was encoded into the file name..
strNoColons = strrep(fullInputFileName(3:end), ':', '');
outputFileName = [fullInputFileName(1:2), strNoColons];
[folder, baseFileNameNoExt, ext] = fileparts(outputFileName);
outputFileName = fullfile(folder, [baseFileNameNoExt, '.png'])
fprintf('Writing "%s".\n', outputFileName);
% Save the array to disk.
imwrite(resizedImage, outputFileName);

Melden Sie sich an, um zu kommentieren.


chee gang ngui
chee gang ngui am 3 Jun. 2022
thank you!

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by