Removing unessecary background/ details from the Image
1 view (last 30 days)
Show older comments
Hi,
Here is one Image, "Out.jpg", I have attached the original file with this post.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1638761/image.png)
I want to remove transparent backgorund/ pixels and this colorbar area (right side) as I am only intrested in inside colored image (left side). How I can get exact dimension of this inside image to crop it with no quality loss.
I have seen cropData option available, but how can I decide what values will be correct for me to crop it. Is there any better option available ?
Following my above query, I have developed this loop script but I am getting error, if someone can fix this.
p=dir('Data\*.jpg'); %input files
filenames = {p.name};
nfiles = length(filenames);
for N = 1 : nfiles
thisfile = filenames{N};
newname = sprintf("%C.png", N);
Im = imread(thisfile);
x_start = 9; % start x
x_end = 520; % end x
y_start = 1; % start y
y_end = 640; % end x
Im_new = Im([x_start:x_end],[y_start:y_end],:);
out_path = 'Data'; % Give path here
fullFileName = fullfile(out_path, newname);
imwrite(Im_new,fullFileName) % replace the croped image
end
I am getting following error.
"Error using imwrite
Unable to determine the file format from the file name.
Error in Images_Mapping_Thermal_NoGPS_V12 (line 89)
imwrite(Im_new,fullFileName) % replace the croped image"
Kindly highlight the mistake.
Regards
0 Comments
Accepted Answer
DGM
on 10 Mar 2024
Edited: DGM
on 10 Mar 2024
Talk about a trick question. If the attached file were a JPG, you would need to go back to the source in order to get undamaged image content or alpha to work with. Luckily, it's not actually a JPG. It's a PNG with the wrong extension. Changing the extension doesn't change the file format.
The error is because your filename string is empty. One way to fix:
% use zero padding to help enforce proper sorting
newname = sprintf('%04d.png', N);
If you're trying to automate the process of finding the rectangle:
[inpict,~,alpha] = imread('out.png'); % it's a PNG, not a JPG
mask = alpha>128; % reduce alpha to a logical mask
mask = bwareafilt(mask,1); % pick the largest blob
[~,rows,cols] = crop2box(mask); % get the extents of the blob
outpict = inpict(rows,cols,:); % crop that region
imshow(outpict,'border','tight')
... though if you know that the position is the same for all images, there's no sense in using a more expensive routine.
2 Comments
DGM
on 10 Mar 2024
Edited: DGM
on 10 Mar 2024
I don't doubt that someone might do that, but I really can't think of a good reason, since it would only increase the probability that the files are mishandled. I certainly wouldn't expect to even request alpha when reading a JPG with imread().
FWIW, I figured that out because I looked at the metadata to check if it had any unexpected colorspace info. I was surprised to see that it was RGB+alpha which is something a JPG can't be. I looked at the other tags and it appeared to be a PNG. I then confirmed it by looking directly at the header. It's just a renamed PNG file.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!