Batch processing images for downsampling
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi,
I would like to perform downsampling for like over 700 images. It is pretty time-consuming to do it one by one. How can I achieve it at one time?
Thanks a lot.
채택된 답변
Mathieu NOE
2023년 6월 13일
hello
see example below - adapt to your own needs
%% Initalize the data
dataDir= fullfile(pwd); % select appropriate directory
exts = {'.jpg','.png','.tif'}; % choose valid file extensions like {'.jpg', '.png'}
resize_size = 100; % pixels size for output img
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions',exts,'LabelSource','foldernames');
countEachLabel(imds);
numImages = numel(imds.Files);
for i = 1:numImages
img = readimage(imds, i);
[m,n,p] = size(img);
% compute scale factor (same on both dimensions)
scale_factor = min(resize_size/m,resize_size/n);
img3= imresize(img, scale_factor);
figure(i),
img4= imshow(img3, 'InitialMagnification', 800);
drawnow;
Train{i} = (img3); %output image stored in cell
end
댓글 수: 8
another example
% select appropriate options below and adapt code to your specific needs ...
% files = dir('*.tif'); % select all tiff files in current directory
files = dir('handheld*.tif'); % select only tiff files with "handheld" in the filename
% resize options (sacle factor or pixels)
scale_factor = 0.5;
% select portion of the image
% 451*279*3. I will like to extract a portion with dimensions of 120*80*3
x_dim = 120;
y_dim = 80;
x_offset = 100; % please make sure the "corner" of the cropped image is correct
y_offset = 50;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, scale_factor);
% select (crop)
H{ck}= img(x_offset+(1:x_dim),y_offset+(1:y_dim),: );
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.tiff'];
imwrite(H{ck},fullfile(out_dir,filename));
end
Many thanks. I am choosing the second one. Is it ok if I use .png format for the second script?
Hello again, I changed .tiff to .png format. Also, my scale factor is 0.2. I didn't want to select a portion from the images so I just wrote the original size which is 140x140. I commented out crop section down there. After all these adjustments, I could run the code but only one image was downsampled. Why has that happened? Thanks a lot.
could you please share your final code ?
% select appropriate options below and adapt code to your specific needs ...
files = dir('*.png'); % select all tiff files in current directory
%files = dir('handheld*.tif'); % select only tiff files with "handheld" in the filename
% resize options (sacle factor or pixels)
scale_factor = 0.2;
% select portion of the image
% 451*279*3. I will like to extract a portion with dimensions of 120*80*3
x_dim = 140;
y_dim = 140;
x_offset = 100; % please make sure the "corner" of the cropped image is correct
y_offset = 50;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, scale_factor);
% select (crop)
%H{ck}= img(x_offset+(1:x_dim),y_offset+(1:y_dim),: );
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.tiff'];
imwrite(H{ck},fullfile(out_dir,filename));
end
Also, size of the output image is 560x420. I would like it to be 140x140 and with bicubic interpolation as like in this example but it also gives me a different size.
% Load the original image
origImage = imread('Planet_20221104_1667.png');
% Set the desired scale factor to achieve 10m resolution
scaleFactor = 0.20;
% Resize the image using bicubic interpolation
downsampledImage = imresize(origImage, scaleFactor, 'bicubic');
% Save the downsampled image to a new file
imwrite(downsampledImage, 'Planet_20221104_0364_1.png');
many thanks for your helps.
here you are my friend :
% select appropriate options below and adapt code to your specific needs ...
files = dir('*.png'); % select all png files in current directory
% resize options (sacle factor or pixels)
x_dim = 140;
y_dim = 140;
% output directory
out_dir = fullfile(pwd,'\out'); % from current directory to sub directory \out
% main loop
for ck = 1:length(files)
Filename = files(ck).name;
img = imread(Filename);
% resize
H{ck}= imresize(img, [x_dim y_dim], 'bicubic');
% plot (just to check)
figure(ck), imagesc(H{ck});
% insert your own code here (save for example)
filename = ['out' num2str(ck) '.png'];
imwrite(H{ck},fullfile(out_dir,filename));
end
Many thanks. Much appreciated.
as always, my pleasure !
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
