specific transformation with tformarray

1 view (last 30 days)
mb
mb on 23 Jan 2015
Commented: mb on 23 Jan 2015
Hi, I want to displace specific pixel values in an image pixelwise. For example the below code takes a box of values 50 and moves them 16 pixels to the right. However I would like to move for example some fraction p from left to right, e.g. P=0.5 such that on the right the end values are sum of the old values and the new displaced intensities multiplied with 0.5. And that on the left location this transferred intensity is subtracted. For example here the left values would be 50*0.5 and right values 50*0.5 + 30 ... It seems this might be doable with tformarray but i dont seem to get it work any ideas?
Thank you very much!
clear
sphere = zeros(256,256);
[X,Y] = meshgrid(-127:128,-127:128);
[THETA,RHO] = cart2pol(X,Y);
sphere(RHO<50) = 50;
sphere(RHO<40) = 40;
sphere(RHO<30) = 30;
sphere(RHO<20) = 20;
Tx = zeros(size(sphere,1),size(sphere,2));
Ty = zeros(size(sphere));
Tx(126:130,100:104,1) = Tx(126:130,100:104,1) - 16;
[x y ]= meshgrid(1:size(sphere,1) ,1:size(sphere,2));
R = makeresampler('cubic','fill');
tmap_b = cat(3, Tx+x,y);
Iout = tformarray(sphere,[],R,[1 2],[1 2],[],tmap_b,0);
imtool(Iout,[])
clear RHO THETA X Y R tmap_b Ty Tx Int x y

Accepted Answer

Guillaume
Guillaume on 23 Jan 2015
I don't think there's any function in the toolbox that does what you want, tformarray, imtransform, imwrap and co. don't modify the origin and don't usually combine the destination.
For a 2d matrix, assuming no overlap between source and destination it's trivial to implement though
function img = mytransform(img, boundingbox, shift, ratio)
%img: a 2D grayscale image
%boundingbox: an array of four values, [top right bottom left] indicating the source box
%shift: an array of two values, [dx dy] indicating the shift
%ratio: %of intensity to remove from origin and add to destination, in the range 0-1
box = img(boundingbox(1):boundingbox(3), boundingbox(2):boundingbox(4));
img(boundingbox(1):boundingbox(3), boundingbox(2):boundingbox(4)) = box * (1-ratio); %remove %ratio * box from origin
boundingbox([1 3]) = boundingbox([1 3]) + shift(2); %shift [top bottom] by dy
boundingbox([2 4]) = boundingbox([2 4]) + shift(1); %shift [left right] by dx
img(boundingbox(1):boundingbox(3), boundingbox(2):boundingbox(4)) = img(boundingbox(1):boundingbox(3), boundingbox(2):boundingbox(4)) + box * ratio; %add %ratio * box to destination
end
Usage:
Iout = mytransform(sphere, [126 100 130 104], [-16 0], 0.5);

More Answers (0)

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!