zatoichi66 Posted October 26, 2008 Report Share Posted October 26, 2008 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! Quote Link to comment
jdunham Posted October 27, 2008 Report Share Posted October 27, 2008 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. Quote Link to comment
Aristos Queue Posted October 27, 2008 Report Share Posted October 27, 2008 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: 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. :-) Quote Link to comment
jdunham Posted October 28, 2008 Report Share Posted October 28, 2008 QUOTE (Aristos Queue @ Oct 25 2008, 07:12 PM) I fixed it. This does what you're asking for very fast (a few milliseconds): AQ's solution is certainly faster and better, but he sort of pulls a rabbit out of a hat, luckily finding another function available which does the rearrangement internally. If the OP is curious about how to work with large data sets, and how to get decent performance, then this version shows how him or her how the original code could have been written to run in a reasonable amount of time. Jason http://lavag.org/old_files/monthly_10_2008/post-1764-1225093225.png' target="_blank"> Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.