I read set of images and I removed the borders of them, I need to save the resulted images in a new folder but i couldnt. I used this code but I didnt get a result, I need a help, plz?

1 view (last 30 days)
Ahmad Yahya
Ahmad Yahya on 15 Jan 2017
Commented: Ahmad Yahya on 15 Jan 2017
srcFiles = dir('C:\Users\..\ImagesWithBorder\*.tif'); % the folder in which our images exists
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\..\ImagesWithBorder\',srcFiles(i).name);
I = imread(filename);
% figure, imshow(I);
J = rgb2gray(I);
A=double(J);
[r c]=size(A);
for row = 1 : r
for col = 1 : c
if J(row,col) >= 90
J(row,col) = 0;
end
end
end
J(all(J==0,2),:)=[]
J(:,all(J==0,1))=[]
Finalfilename = sprintf('C:\\Users\\ahmadjalal2013\\Desktop\\Thesis_Images\\ImagesWithoutBorder\\%02d',i);
imwrite ( J, 'Finalfilename', 'tif');
end

Accepted Answer

Image Analyst
Image Analyst on 15 Jan 2017
Edited: Image Analyst on 15 Jan 2017
You shouldn't have single quotes around the filename when you pass it into imwrite(). Here, use this code instead:
outputFolder = 'C:/Users/ahmadjalal2013/Desktop/Thesis_Images/ImagesWithoutBorder';
baseFileName = sprintf('%02d.tif', i);
fullFileName = fullfile(outputFolder, baseFileName); % Prepend folder.
imwrite(J, fullFileName);
Note that there is a .tif extension in the baseFileName so you do not need to pass it into imwrite(). Also note that forward slashes work just fine with Windows in path names.
  5 Comments

Sign in to comment.

More Answers (1)

John BG
John BG on 15 Jan 2017
Hi Ahmad
this code does what you want
.
A=imread('im1.jpg');imshow(A)
p=ginput(2)
p1max=max(p(:,1));p2max=max(p(:,2));p1min=min(p(:,1));p2min=min(p(:,2));
A(:,[1:p1min],:)=[];
A([1:p2min],:,:)=[];
A(:,[uint64(p1max-p1min+1):end],:)=[];
A([uint64(p2max-p2min+1):end],:,:)=[];
imshow(A)
.
.
EXPLANATION
1.
as example let's start with this image
A=imread('im1.jpg');imshow(A)
2. Selecting 2 diagonal points that define the borders you want to remove. I use command ginput but you already have the values
p=ginput(2)
3. Calculating border values from ginput points
p1max=max(p(:,1));p2max=max(p(:,2));p1min=min(p(:,1));p2min=min(p(:,2));
4. Removing on borders, one side of the frame at a time
A(:,[1:p1min],:)=[];
A([1:p2min],:,:)=[];
A(:,[uint64(p1max-p1min+1):end],:)=[];
A([uint64(p2max-p2min+1):end],:,:)=[];
5.
Check
imshow(A)
Ahmad
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
  2 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!