Shifting pixels in an image without loops

26 Ansichten (letzte 30 Tage)
Matt.
Matt. am 17 Sep. 2019
Beantwortet: Image Analyst am 18 Sep. 2019
Hi, I would like to shift some pixels in an image (Ny,Nx,Nc) given a 2D shift map (Ny,Nx).
I can easily do it with loops but I am having troubles to vectorize it:
% test shift of image with a map
clear all
close all
% example of image (vertical gradient) + horizontal red stripe
img=repmat(uint8((1:1980)'*255/1980),[1,2880,3]);
img(800:900,:,1)=255;
figure
imagesc(img)
title('original image')
% example of shift map (vertical gradient) +vertical stripe
shift_map=repmat((1:1980)'*1/4,[1,2880]);
shift_map(:,1000:1500)=255;
figure
imagesc(shift_map)
title('original shift map')
%% with loops
img_out_1=inf(size(img));
tic
for x=1:size(img,2)
for y=1:size(img,1)
indx_new=round(x-shift_map(y,x));
if 0<indx_new && indx_new<=size(img,2)
img_out_1(y,indx_new,:)=img(y,x,:);
end
end
end
% median filter to smooth the missing values
img_out_1=medfilt1(img_out_1,3,[],2);
toc
figure
imagesc(uint8(img_out_1))
title('final image')
I have managed to remove the loop on x and it is already much faster:
%% with loop on y only
img_out_2=inf(size(img));
tic
Nx=size(shift_map,2);
x=linspace(1,Nx,Nx);
for y=1:size(img,1)
indx_new=round(x-shift_map(y,:));
ind=find(0<indx_new & indx_new<Nx);
img_out_2(y,indx_new(ind),:)=img(y,x(ind),:);
end
img_out_2=medfilt1(img_out_2,3,[],2);
toc
figure
imagesc(uint8(img_out_2))
title('final image 2')
Naively I tried a similar approach to remove the loop on x, but I am getting very confused as my inddices ind_x becomes a matrix. Obviously img_out_3(yy(ind),indx_new(ind),:) fails with Requested 5212348x5212348x3 (75908.1GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
img_out_3=inf(size(img));
tic
Nx=size(img,2);
Ny=size(img,1);
Nc=size(img,3);
x=linspace(1,Nx,Nx);
y=linspace(1,Ny,Ny);
[yy,xx]=meshgrid(y,x);
indx_new=round(x-shift_map);
ind=find(0<indx_new & indx_new<Nx);
img_out_3(yy(ind),indx_new(ind),:)=img(yy(ind),xx(ind),:);
img_out_3=medfilt1(img_out_3,3,[],2);
toc
figure
imagesc(uint8(img_out_3))
title('final image 3')
Thanks for the heads up,
Matt
  2 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 17 Sep. 2019
Bearbeitet: KALYAN ACHARJYA am 17 Sep. 2019
Shifting pixels in an image without loops
Sometimes simple representation of the problemmis much simpler to undestand than actual code.
  1. Suppose you have an image,
  2. Now you want to shift those pixels to ....??? what happen on present pixels location?
  3. When the pixel shift to other location, is it oerwrite on present pixels?
  4. Location: source and destination pixels location?
Can you share the detail, it would be much easier to understand?
Matt.
Matt. am 18 Sep. 2019
Thanks for the help. I will try to clarify my problem. Te first part of my code is doing what I want but it is very inefficient.
1) so I have an image M(x,y) plus a function that gives the shift in the x direction S(x,y).
2) I want to create an image of M where each pixels are moved in x by the corresponding S(x,y) value.
On the pixel location two things can happen:
-either it is the image of an other pixel and the new value is updated,
-either it is not updated and the value is inf.
3) yes. If two original pixel ends up to the same pixel/location, we can keep the latest value. It is not my biggest concern so far so a solution that keeps the first value is also ok for me.
4)in fact I want to get M(x+S(x,y),y)
Origin is the pixel at posiion (x,y). I want to move it to position (x+S(x,y),y)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 18 Sep. 2019
Try imtranslate().

Kategorien

Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by