Jump to content

Flattening 3d Arrays


Recommended Posts

Hello all. (I've read through the forums looking for a solution for my problem, no luck)

I am trying to flatten a 3d array into a 2d array. More specifically, if you picture a RGB image, for example, you have the first page consisting of Red values, (8 bit, if you like), the second page consisting of green values, and third page consisting of blue values.

I'm wondering what the fastest (computationally) method of flattening the image, such that the result is a 2d array like this:

<- Cols ->

RGB

RGB

RGB

...

RGB

I have this vi written, but it is s....l....o.........w. (It contains example data, and is unfortunately 5 megs... sorry) The example data isn't RGB, but an 8 "band" image.

Please Help!

Link to comment

The problem is that you are using Build Array inside the loop. This requires LabVIEW to reallocate memory on every iteration (I think they try some optimization, but there is only so much they can do.) Instead of using shift registers which start empty and hold an ever-growing array, you should initialize with a full array containing all zero values, and then use Replace Array Subset, which is an in-place operation, to drop the correct values at each location.

Not only is build array slow, but it gets slower as the array grows.

On my computer the original VI ran too slowly to measure. After about 15 minutes It had done more than half of the rows, but was getting slower.

The rewritten version ran in 47ms.

Link to comment

I fixed it. This does what you're asking for very fast (a few milliseconds):

Download File:post-5877-1224986912.vi

The attached VI is saved in 8.5.1. Just in case you cannot load that version, here's a picture of the code:

post-5877-1224987004.png?width=400

QUOTE (jdunham @ Oct 25 2008, 06:07 PM)

you should initialize with a full array containing all zero values, and then use Replace Array Subset, which is an in-place operation, to drop the correct values at each location.
Or just let the array pre-allocation be taken care of by an auto indexing output terminal of a For Loop. :-)
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.