Message Boards Message Boards


How can I make it faster? (ParallelTable)

Posted 9 years ago
1 Reply
1 Total Likes


Imagine a detector with square pixels, the pixels arranged in a 256 x 256 matrix. Every pixel has an associated intensity value, normalized in such a way that the biggest value is at the center and it is 1.0. A pixel is characterized with a triplet{i,j,value}, where i is the row, j is the column and value is the intensity. Taking the central {128,128} pixel I draw an annuli with an inner radius and outer radius of {6.89148, 11.4858} pixels. Pixel {0,0}is the lower left corner. This annuli will cover some central area of the detector.
Then starting at the bottom line of the covered pixels, I look a 3x3 matrix at the leftmost pixel of the mentioned pixel line.
Then I look at the center of this 3x3 matrix if it is a maximum intensity or a minimum among the nine in the 3x3 matrix, and if it is, then I collect that value with its pixel coordinates.
Then I slide this 3x3 matrix till I get to the last such position where I still have a full 3x3 matrix. If I cannot get a full 3x3 matrix, - let say hit the inner radius somewhere -, then I just slide further. If no more such position is attainable on the given pixel line then I slide up one pixel, look for the leftmost starting position and repeat the above process. I do this till I cover all the pixels under the annuli.

Here is the code below that does this. For small annuli it runs relatively fast, but for bigger annuli - {43.6461, 48.2404} - it is not so fast, even on a 4 core MacPro. So I am interested to see which ways can I still speed it up. Oh, and at once I do it with 67 different configurations. Annuli200[[k]] is one such annuli where k goes from 1 to 67. Annuli[[k]] contains a bunch of triplets, the first one for the bottom left pixel and the last one for the upper right pixel in the given annuli. Thanks ahead, János

pixelScale = 21.766; (* mas/pixel *)
masdelta = 50/pixelScale;
mas200 = 200/pixelScale;

rr = ParallelTable[Reap[
   firsti = First[First[annuli200[[k]] ] ];                                       (* Bottom pixel line in the annuli *)
    lasti = First[Last[annuli200[[k]] ] ];                                           (* Top pixel line in the annuli *)
    firstj = 
       First[Select[annuli200[[k]] , #[[1]] == firsti &] ] ] ];       (* The starting column on the bottom pixel line *)
    delta = 2;
    While[firsti + delta <= lasti,
     firstinline = First[Select[annuli200[[k]], #[[1]] == firsti &]];  (* the per moment starting triplet on the per moment pixel line *)
     firstj = 
        First[Select[annuli200[[k]] , #[[1]] == firsti &] ] ] ];    (* the per moment column on the per moment pixel line *)
     lastinline = Last[Select[annuli200[[k]], #[[1]] == firsti &]];   (* the per moment end triplet on the per moment pixel line *)
     lastj = First[Rest[lastinline]];                                  (* the last column on the per moment pixel line *)
     While[firstj <= lastj - delta,
      dd = 
        firsti <= #[[1]] <= firsti + delta && 
          firstj <= #[[2]] <= firstj + delta &];
      If [Length[dd] == 9,
       If[! MemberQ[Union[Map[dd[[5, 3]] >= #[[3]] &, dd]], False],
        Sow[Select[dd, #[[3]] == Max[dd[[All, 3]] ] &], maxn];
       If[! MemberQ[Union[Map[dd[[5, 3]] <= #[[3]] &, dd]], False],
        Sow[Select[dd, #[[3]] == Min[dd[[All, 3]] ] &], minn];
     ], {maxn, minn}], {k, 1, Length[annuli200]}];
POSTED BY: Janos Lobb

As best I understand, a pixel is collected if and only if the two conditions below are satisfied.

(1) It and its square neighborhood (so-called "Noore neighborhood") lie entirely in the annulus.

(2) It has the maximum value in that neighborhood.

So you can preprocess the entire array to figure out who satisfies the second condition. Then for each annulus you simply figure out which pixels from that list also satisfy the first. Form an efficiency viewpoint it is probably easier to find out which pixels satisfy the first requirement and then check if they satisfy the second as well.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract