how can i imagie zoom with Convolution method

15 Ansichten (letzte 30 Tage)
Hamad
Hamad am 25 Jan. 2023
Bearbeitet: Image Analyst am 25 Jan. 2023
for example i have a matrix of [3 5 7;2 7 6; 3 4 9]
and i want to zoom it with the method of Convolution which i add zeros all around it to become like this:
Next, we use convolution mask, which is slide a cross the extended image, and perform simple arithmetic operation at each pixel location :
For example, if we put the mask over the upper-left corner of the image, we obtain (from right to left, and top to bottom):
1/4(0) +1/2(0) +1/4(0) +1/2(0) +1(3) +1/2(0) + 1/4(0) +1/2(0) +1/4(0) =3
The next step is to slide the mask over by one pixel and repeat the process, as follows:
1/4(0) +1/2(0) +1/4(0) +1/2(3) +1(0) +1/2(5) + 1/4(0) +1/2(0) +1/4(0) =4
until the end, then move down one pixel and repeat the process until the end so i have a new matrix
i had a hard time trying to figure the code, so i would really appreciate the help
  4 Kommentare
Walter Roberson
Walter Roberson am 25 Jan. 2023
That is exactly what conv2 is designed to do. (In theory it might flipud(fliplr()) the mask you provide, but your mask is symmetric so it does not matter about that detail.)
Walter Roberson
Walter Roberson am 25 Jan. 2023
If you are trying to tell us that you are doing a homework assignment in which you are not permitted to use conv2() and need to write that functionality yourself, then we would suggest that it is easy enough to write as two for loops.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 25 Jan. 2023
Bearbeitet: Image Analyst am 25 Jan. 2023
Convolution is fast and efficient. It's inserting the zeros that's going to be a pain, and slow.
The final result is an interpolation.
To do that interpolation more efficiently (without inserting zeros) you can use imresize or interp2
If you need a manual convolution program, see my attached demo.

Walter Roberson
Walter Roberson am 25 Jan. 2023
It is deliberate that the following does not do exactly what you want to do. It is also deliberate that it has some extra steps that are not needed for your purposes. This code illustrates that the task can be done; figuring out what parts of it are surplus is part of your learning experience.
img = imresize(imread('flamingos.jpg'), [32 24]);
imshow(img);
max(img, [], 'all')
ans = uint8 255
img2 = zeros(size(img,1)*2+1, size(img,2)*2+1, size(img,3), 'like', img);
img2(2:2:end, 2:2:end, :) = img;
imshow(img2);
mask = [1/9 1/3 1/9; 1/3 2/3 1/3; 1/9 1/3 1/9]
mask = 3×3
0.1111 0.3333 0.1111 0.3333 0.6667 0.3333 0.1111 0.3333 0.1111
img3a(:,:,3) = conv2(double(img2(:,:,3)), mask);
img3a(:,:,2) = conv2(double(img2(:,:,2)), mask);
img3a(:,:,1) = conv2(double(img2(:,:,1)), mask);
max(img3a,[],'all')
ans = 170
img3 = uint8(rescale(img3a, 0, 255));
imshow(img3)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by