Clear Filters
Clear Filters

Pixel swaps between arrays-Conditional...

2 views (last 30 days)
Dear all,
I need to do some pixels swaps between two 3D arrays (i.e. two series of images). The giver array and the taker array. Both arrays have the same size, eg. 236x236x50. The mean2 of each plane of giver and taker are descending from sheet to sheet. Overall the mean2 values of the giver array are larger than the values of the taker array. The giver array sheets need to give individual randomly chosen pixels per sheet to any recieving sheet of the taker array. In return the taker array gives the pixel at the same (xy) position to the giver array. The taker array will does recieve higher values. The whole process is repeated until each plane of giver and taker correspond to the overall average. I used a while loop to define the conditions (taker may not get any larger than the overall average and giver may not get any smaller) and some randi to randomly choose pixels per plane. Overall I assume that 1-4% of all pixels need to be swapped in order to achieve the desired result. My current script can do one by one pixle and is therefore very very slow. It needs to be improved... Swapped pixels need to stay at the same position in the plane and are swapped only along the third dimension. M Is the total average... Many thanks.
giver=GFPsort(:,:,1:50);
taker=GFPsort(:,:,51:100);
for e=1:50
while mean2(giver(:,:,e))>= M && mean2(taker(:,:,51-e))<= M ;
pxid=randi([1 236],1,2);
a=giver(pxid(1),pxid(2),e);% problem: how to identify pixel, with randi I do get either pairs of values or single values, how to find specific pixel...?)
b=taker(pxid(1),pxid(2),51-e);
giver(pxid(1),pxid(2),e)=b;
taker(pxid(1),pxid(2),51-e)=a;
end
end
  4 Comments
Image Analyst
Image Analyst on 19 Dec 2019
OK - I've just never heard that terminology before. The third dimension is usually called "slice", "channel", or "plane". You don't have to call mean2 every time, if that's what's slowing it down. You can call it once, then when you transfer one pixel at a time, you can just update the mean by subtracting the old value's contribution to the mean and adding in the new pixel's contribution. You can use randperm() to get a list of all pixels, and then just go through that list updating the means until you reach the criteria. Then do the swap of all the pixels en masse, just once when you're all done with that plane.
Martin Offterdinger
Martin Offterdinger on 20 Dec 2019
Thanks Image Analyst! Your suggestion to replace the mean2 in the while loop by a simple update of the means using the swapped pixel values helped a lot. it is much faster now...

Sign in to comment.

Answers (0)

Categories

Find more on Image Processing Toolbox 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!