Removing unessecary background/ details from the Image

1 view (last 30 days)
Hi,
Here is one Image, "Out.jpg", I have attached the original file with this post.
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

Accepted Answer

DGM
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
Abdul Hannan
Abdul Hannan on 10 Mar 2024
Edited: Abdul Hannan on 10 Mar 2024
Thanks @DGM, for resolving the issue.
These *.JPG images are output of DJI Thermal Analysis Tool 3. I think they are exporting in *.JPG for user's understanding, as most of the users are comportable with JPGs. Its just my own theroy.
DGM
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.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!