Removing unessecary background/ details from the Image
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
DGM
2024-3-10
编辑:DGM
2024-3-10
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 个评论
DGM
2024-3-10
编辑:DGM
2024-3-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!