Shrink image without using imresize

64 vues (au cours des 30 derniers jours)
Sean Ford
Sean Ford le 24 Juin 2020
Modifié(e) : DGM le 7 Déc 2022
First, I will say I do not have the Image Processing Toolbox and cannot get it so please do not turn this into yet another debate about why I should use built in functions. I would love to, but I can't for various reasons.
I'm using Matlab 2017a (9.2.0.556344)
I have a video frame that's 1200x1100 (I know, odd dimensions) color RGB image.
I want to end up with a 650x650 color RGB image.
Here's what I tried, which just results in a cropped image. I want a shrunken image. How can I shrink this large image without using imresize or any other function in the Image Processing Toolbox?
v_obj = VideoReader('vid.mp4');
img = readFrame(v_obj);
[Xq,Yq,Zq] = meshgrid(1:1:650,1:1:650,1:1:3);
Vq = interp3(double(img),Xq,Yq,Zq);

Réponse acceptée

KSSV
KSSV le 24 Juin 2020
Modifié(e) : KSSV le 24 Juin 2020
Let I be your image of size 1200x1100.
[m,n,p] = size(I) ;
[X,Y] = meshgrid(1:m,1:n) ;
xi = linspace(1,m,650) ;
yi = linsace(1,n,650) ;
[Xi,Yi] = meshgrid(xi,yi) ;
Ii = zeros(650,650,3) ;
for i = 1:p
Ii(:,:,i) = interp2(X,Y,I(:,:,i),Xi,Yi) ;
end
  9 commentaires
Sean Ford
Sean Ford le 24 Juin 2020
That did it! Thanks!!
Stefan Grandl
Stefan Grandl le 21 Juil 2021
Perfect as always, Image Analyst! Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (1)

DGM
DGM le 7 Déc 2022
Modifié(e) : DGM le 7 Déc 2022
Imresize() is currently available in the base toolbox, so you don't need IPT anymore. This change occurred somewhere after R2015b and before (or at) R2019b. I can find no mention of the change in the PDF documentation for either base MATLAB or IPT.
Otherwise, if you need to resize images and you don't have IPT, you can use MIMT imresizeFB(). That is an approximate replacement for imresize(), and supports a similar set of options. One difference of note is that imresizeFB() does not support indexed-color images. It also doesn't support Lanczos kernels.
inpict = imread('peppers.png'); % the source image
imsize(inpict)
ans =
384 512 3 1
outpict = imresizeFB(inpict,[200 400],'bilinear'); % explicit output geometry
imsize(outpict)
ans =
200 400 3 1
outpict = imresizeFB(inpict,[200 NaN],'bilinear'); % implicit output geometry
imsize(outpict)
ans =
200 267 3 1
outpict = imresizeFB(inpict,1.5,'bilinear'); % scaling factor
imsize(outpict)
ans =
576 768 3 1
No loops, no meshgrid, no class handling.
Can you do it directly using interp2()? Yes. If you expect results similar to imresize(), it's more complicated than that -- especially in the case where you're shrinking the image. If the scaling factor is less than 1, imresize() and imresizeFB() perform antialiasing for cleaner output. Depends if that matters to you.

Community Treasure Hunt

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

Start Hunting!

Translated by