Shrink image without using imresize

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);

 採用された回答

KSSV
KSSV 2020 年 6 月 24 日
編集済み: KSSV 2020 年 6 月 24 日

0 投票

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 件のコメント

Sean Ford
Sean Ford 2020 年 6 月 24 日
編集済み: Sean Ford 2020 年 6 月 24 日
Sorry, I should have been clearer
img is a 1200x1100x3 uint8
Here's what I tried based on your response:
[m,n,q] = size(I) ;
[X,Y,Z] = meshgrid(1:m,1:n,1:q) ;
xi = linspace(1,m,650);
yi = linspace(1,n,650);
zi = linspace(1,q,3);
[Xi,Yi,Zi] = meshgrid(xi,yi,zi) ;
Ii = interp3(X,Y,Z,double(I),Xi,Yi,Zi) ;
I get an error
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.
KSSV
KSSV 2020 年 6 月 24 日
Edited the code....
Sean Ford
Sean Ford 2020 年 6 月 24 日
I tried with your edited code KSSV and I get the same error message.
Image Analyst
Image Analyst 2020 年 6 月 24 日
Why does it need to be that exact size? For example you're feeding it into a deep learning network that requires that size?
KSSV
KSSV 2020 年 6 月 24 日
Check the dimensions of X, Y, I do they have same rows and columns...?
Sean Ford
Sean Ford 2020 年 6 月 24 日
Image Analyst - It doesn't need to be any exact size, I was just giving an example. This truly is simply shrinking the image to any size I desire. Nothing beyond that. I have a large image, I need to resize to a smaller image (not crop).
KSSV - X and Y have the same number of rows and columns. (1200x1100) each.
Image Analyst
Image Analyst 2020 年 6 月 24 日
Sean:
I used his code and got no errors. Try this:
% Setup demo image.
rgbImage = imread('Peppers.png');
% Resize to 1200 x 1100 - the poster's size.
rgbImage = imresize(rgbImage, [1200, 1100]);
% Display original image.
subplot(1, 2, 1);
imshow(rgbImage);
axis('on', 'image');
title('Original Image', 'FontSize', 20);
% Now do the resizing using interp3().
[rows, columns, numberOfColorChannels] = size(rgbImage)
[X,Y,Z] = meshgrid(1:columns,1:rows,1:numberOfColorChannels);
xi = linspace(1,columns,650);
yi = linspace(1,rows,650);
zi = linspace(1,numberOfColorChannels,3);
[Xi,Yi,Zi] = meshgrid(xi,yi,zi) ;
resizedImage = uint8(interp3(X,Y,Z,double(rgbImage),Xi,Yi,Zi));
% Display result.
subplot(1, 2, 2);
imshow(resizedImage);
axis('on', 'image');
title('Resized Image', 'FontSize', 20);
Sean Ford
Sean Ford 2020 年 6 月 24 日
That did it! Thanks!!
Stefan Grandl
Stefan Grandl 2021 年 7 月 21 日
Perfect as always, Image Analyst! Thanks a lot!

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2022 年 12 月 7 日
編集済み: DGM 2022 年 12 月 7 日

0 投票

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.

質問済み:

2020 年 6 月 24 日

編集済み:

DGM
2022 年 12 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by