필터 지우기
필터 지우기

Use imwrite to save processed images to different folder.

조회 수: 12 (최근 30일)
Guan-Lin Chen
Guan-Lin Chen 2020년 10월 12일
댓글: Guan-Lin Chen 2020년 10월 13일
Dear all,
I need help with saving processed images to different folder with imwrite.
Currently, I can save all processed images to a single folder.
Img_filename=ls('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\*.jpg');
imageSize = size(Img_filename);
Img_filenum = size(Img_filename,1);
for img=1:Img_filenum
img_temp=double((rgb2gray(imread(Img_filename(img,:)))));
-----------processing--------
count = count+1;
FileName = fullfile('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\folder_1',sprintf('%03d_circle_cropped.jpg',count));
imwrite(MM, FileName)
end
However, I have 1000 different images in 1 folder and after processing, it will generate 500 images and wanted to save the first 500 processed images into folder_1. And the second 500 processed images to folder_2 and the third 500 images to folder_3 and so on...
How to re-write the imwrite function?
Thank you!

답변 (1개)

Image Analyst
Image Analyst 2020년 10월 12일
folder = sprintf('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\folder_%3.3d', img);
baseFileName = sprintf('%03d_circle_cropped.jpg', img);
FileName = fullfile(folder, baseFileName);
  댓글 수: 7
Guan-Lin Chen
Guan-Lin Chen 2020년 10월 13일
Img_filename=ls('C:\Users\User\Desktop\GF_BSIF\*.jpg'); % has 1000 images
Img_filenum = size(Img_filename,1);
count = 0;
for I=1:Img_filenum
Img = imread(Img_filename(I,:));
imageSize = size(Img);
c_row= 50; %center of circle row value. DON'T CHANGE
radius= 50; %radius of circle
StepSizeRow= 50;
StepSizeCol= 50;
%%
c_col=50; %center of circle column value. CHANGE BY 120
col_min= c_col-radius+1; %column val left side of bounding box. CHANGE BY 120
col_max= c_col+radius; %column val right side of bounding box. CHANGE BY 120
[cols, rows, rgb]= size(Img);
for j = c_col:StepSizeCol: cols-radius
row_min=c_row-radius+1; %row val top of bounding box. DON'T CHANGE
row_max=c_row+radius; %row val bottom of bounding box. DON'T CHANGE
for i= c_row:StepSizeRow:rows-radius
ci = [j, i, radius]; % center and radius of circle ([c_col, c_row, r]) og size (1920, 2560)
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2));
mask = uint8((xx.^2 + yy.^2)<ci(3)^2);
croppedImage = uint8(zeros(size(Img)));
croppedImage(:,:,1) = Img(:,:,1).*uint8(mask);
croppedImage(:,:,2) = Img(:,:,2).*uint8(mask);
croppedImage(:,:,3) = Img(:,:,3).*uint8(mask);
sp(1) = col_min; %min(floor(p(1)), floor(p(2))); %xmin
sp(2) = row_min; %min(floor(p(3)), floor(p(4))); %ymin
sp(3) = col_max; %max(ceil(p(1)), ceil(p(2))); %xmax
sp(4) = row_max; %max(ceil(p(3)), ceil(p(4))); %ymax
row_min=row_min+StepSizeRow;
row_max=row_max+StepSizeRow;
% Index into the original image to create the new image
MM = croppedImage( sp(1): sp(3),sp(2):sp(4),:);
%Display the subsetted image with appropriate axis ratio
count = count+1;
%FileName = fullfile('C:\Users\User\Desktop\Tensorflow\1cm 0.27H circle cropped',sprintf('Cropped_Parallel_5x_20_400Radius_%d.jpg',count));
%FileName = fullfile('C:\Users\User\Desktop\GF_BSIF\temp',sprintf('%03d_circle_cropped.jpg',count));
folder = sprintf('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\folder_%d',I)
baseFileName = sprintf('%03d_circle_cropped.jpg', count);
FileName = fullfile(folder, baseFileName);
imwrite(MM, FileName)
I try to rewite the function but have error:
Warning: Escaped character '\U' is not valid. See 'doc sprintf' for supported special characters.
> In batch_crop (line 53)
Error using imwrite (line 528)
Unable to open file "C:\001_circle_cropped.jpg" for writing. You might not have write permission.
Error in batch_crop (line 56)
imwrite(MM, FileName)
Guan-Lin Chen
Guan-Lin Chen 2020년 10월 13일
Just figure it out!!!
Thank you!!!!

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

Community Treasure Hunt

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

Start Hunting!

Translated by