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

조회 수: 1 (최근 30일)
chee gang ngui
chee gang ngui 2022년 5월 25일
답변: chee gang ngui 2022년 6월 3일
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?

답변 (3개)

Jan
Jan 2022년 5월 25일
img = imresize(I,0.5);
imwrite(img, 'YourImage.jpg')

Image Analyst
Image Analyst 2022년 5월 26일
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
Image Analyst
Image Analyst 2022년 5월 27일
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);

댓글을 달려면 로그인하십시오.


chee gang ngui
chee gang ngui 2022년 6월 3일
thank you!

카테고리

Help CenterFile Exchange에서 Deep Learning for Image Processing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by