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 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2017-1-15
编辑:Image Analyst
2017-1-15
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 个评论
Image Analyst
2017-1-15
You're welcome. There are better ways to determine where the padding of 90 starts and stops rather than a loop over all pixels. You could just say
aboveNinety = J >= 90;
rowsTeDelete = all(aboveNinety, 2);
J(rowsTeDelete, :) = [];
columnsTeDelete = all(aboveNinety, 1);
J(:, columnsTeDelete) = [];
% Now they're gone!
If by chance there is a row or column in the middle of your picture that is all 90 or above, it would also get thrown out. If you don't want that and you only want to delete the outermost rectangular ring padding the image, I'd do it a different way.
belowNinety = J < 90;
row1 = find(all(belowNinety, 2), 1, 'first');
row2 = find(all(belowNinety, 2), 1, 'last');
J(row1:row2, :) = [];
col1 = find(all(belowNinety, 1), 1, 'first');
col2 = find(all(belowNinety, 1), 1, 'last');
J(:, col1:col2) = [];
% Now they're gone!
更多回答(1 个)
John BG
2017-1-15
Hi Ahmad
this code does what you want
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176626/image.jpeg)
.
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)
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176627/image.jpeg)
.
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 个评论
Jan
2017-1-15
编辑:Jan
2017-1-15
Although this is a nice and exhaustive answer, I think the problem concerned the file name only and not the cropping.
Prefer one command per line only. This is easier to read and Matlab's JIT acceleration can improve the code dynamically.
The square brackets are the concateation operator. There is no need to apply this for a vector, which is concatenated with nothing. Therefore it is a waste of time. Doing this for indices in a vector slows down the processing remarkably, because for x(a:b) the boundaries are checked twice only, while for x([a:b]) the check is applied to any element:
x = zeros(1, 10000);
tic; for k = 1:10000; x(1:k)=k; end; toc
tic; for k = 1:10000; x([1:k])=k; end; toc
>> Elapsed time is 0.153457 seconds.
>> Elapsed time is 0.458885 seconds.
There is no benefit in converting the indices to UINT64. Matlab is optimized for using doubles as indices. Therefore the type conversion wastes time only due to the same effect as above:
x = zeros(1, 10000);
tic; for k = 1:10000; x(1:k)=k; end; toc
tic; for k = 1:10000; x(uint64(1:k))=k; end; toc
>> Elapsed time is 0.142847 seconds.
>> Elapsed time is 0.714072 seconds.
An idea would be to create the index vector as UINT64 directly instead of converting it after it was created as double vector:
tic; for k = 1:10000; x(uint64(1):uint64(k))=k; end; toc
>> Elapsed time is 0.422060 seconds.
But the standard double indices without square brackets are simpler, nicer and faster. It is not only a question of taste.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!