Resizing pixels with independent scaling factor?

How can be pixels resized with independent scaling factor?
Keeping size of final image same as initial image.
e.g.
+ =
Resizing method 'nearest' would be enough.
Right I am using some kind of 'for' loop but want to avoid it for faster processing.
Starting experiment data could be:
%% Creation of starting image
w=8;
im=255*ones(w,w,1, 'uint8');
im(2,2,:)=0;
im(6,6,:)=0;
%% Inputs to resize pixels
row=[2;6]; col=[2;6];
scale=[3;5]; % length of each of these inputs are same, explanation: pixel{2,2} will have a scaling factor 3 and so on..
% fractional scaling factor can be entertained as per nearest resizing method.
% length of these inputs are large, of the order of number of pixels in the image, thats why for loop takes time.
Any innovative ideas are welcome
Note: During resizing process sometimes pixels may overlap, in this case, new values will overwrite the old values
i.e., pixels processed due to bottom values of [row, col & scale] can overwrite the previous values.
Thanks

3 Comments

Resizing pixels or Resizing pixels numbers?
Keeping size of final image same as initial image.
Can you elaborate, how you calculate the size of an image?
Thanks for consideration @kalyan.
Initial image (SIze is [8x8])
im_ask2.JPG
Final image (SIze is [8x8])
imResized_ask2.JPG
@JAI: Please post the current working code, if you want us to improve it. Maybe the loop is the most efficient method, when it is optimized.

Sign in to comment.

 Accepted Answer

Jan
Jan on 15 Feb 2019
Edited: Jan on 15 Feb 2019
Let's start with a working loop:
w = 8;
im = repmat(uint8(255), w, w);
im(2,2,:) = 0;
im(6,6,:) = 0;
row = [2;6];
col = [2;6];
scale = [3;5];
for k = 1:numel(row)
s = (scale(k) - 1) / 2;
w(row(k) - s:row(k) + s, col(k) - s:col(k) + s) = ...
w(row(k), col(k));
end
Is this your current code?
Maybe this is slightly faster or nicer:
s = (scale - 1) / 2;
iRow = row - s; % Safer: max(1, row - s);
fRow = row + s; % min(w, row + s);
iCol = col - s; % max(1, col - s);
fCol = col + s; % min(w, col + 2);
for k = 1:numel(row)
w(iRow(k):fRow(k), iCol(k):fCol(k)) = w(row(k), col(k));
end
The assignment inside the loop is vectorized already. If an overlap is possible, a fully vectorized method is hard or impossible. I assume, that the creation of the large index array will take more time than this compact loop.

6 Comments

Yes this is exactly my code is.. ?
Do you think for loop is unavoidable.
I mean to improve the performance, vectorization is impossible?
Ok, you answered my question in your prevous comment.
Thank you @Jan
If this is the time-critical part of your code, please mention the real size of the inputs. A C-Mex function will be faster, most likely. Do you have a C-compiler installed?
Aha.
I am compiling this with below code.
codegen('myfunction.m','-args',{im, row, col, scale})
image size is [450, 800] so the row size is [450x800,1].
As I am targeting real-time processing, so I have 30ms for execution of each frame.
I have i7 processor.
I tried on gpu compilation also, which is 10x times faster but it detiorates the quality, may be because it parallaize the for loop but somwhere skips the order of the loop. Or may be other reason I dont know.
GPUvsCPU.jpg
GPU mex compilation
cfg = coder.gpuConfig('mex');
cfg.GpuConfig.ComputeCapability = '6.1';
codegen -args {im, row, col, scale} -config cfg myfunction
A parallelization is strange here: The code overwrites elements of the input data and the order of loops matters. Then a parallel version cannot create the same output.
Ok Jan, I underestood.

Sign in to comment.

More Answers (0)

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 15 Feb 2019

Commented:

on 18 Feb 2019

Community Treasure Hunt

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

Start Hunting!