How do I save an image with a name same as the original image?
조회 수: 3 (최근 30일)
이전 댓글 표시
I need to save an image like the original image, for example:
original image = 001A.jpg
saved image = 001A(1).jpg
clear all
clc
%Detect objects using Viola-Jones Algorithm
imgdir = 'C:\Users\Lewis\Documents\MATLAB\testingimage';
DestDir = 'C:\Users\Lewis\Documents\MATLAB\violajones\croppedimage';
%To detect Face
FDetect = vision.CascadeObjectDetector;
%Read the input image
images = dir(fullfile(imgdir, '*.jpg'));
numfiles = length(images);
myimage = cell(1,numfiles);
for k = 1:numfiles
myimage{k} = imread(fullfile( imgdir, images(k).name));
%Returns Bounding Box values based on number of objects
BB = step(FDetect,myimage{k});
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r');
croppedimage = imcrop(myimage{k},BB(i,:));
end
filename = ['croppedTE' ,num2str(k),'.jpg'];
imwrite(croppedimage, fullfile(DestDir, filename));
end
댓글 수: 0
채택된 답변
Guillaume
2016년 10월 2일
편집: Image Analyst
2016년 10월 2일
Your question is puzzling: since you manage to come up with some fairly complex code it should be obvious to you what needs changing in order to do what you want. The other option is this is not your code, in which case you should try to understand what each step does. Again, the solution should then be obvious:
Change the filename creation line to:
filename = [images(k).name, '(', num2str(k), ').jpg');
Note that I prefer using sprintf instead of string concatenation and num2str, so I'd write the above as:
filename = sprintf('%s(%d).jpg', images(k).name, k);
댓글 수: 1
Image Analyst
2016년 10월 2일
Guillaume's answer is a good one. I'd recommend that you don't use jpg if you're going to do image analysis though. Use PNG format instead.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Display and Exploration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!