Quick Array Population - arrays

I have a application that will capture the screen and I want to write the captured information to an array, this takes AGES as the array ends up being +2million values. I am iterating and adding the values to the array, is there any way quicker (eg binary operations)? Should it be this slow? Why is it?

Assuming your GetPixel'ing the screen pixel by pixel, its the GetPixel call that's slow (it interrogates the display driver) not the (pre-dimensioned) array assignment.
You can instead use the getdibits() api which will copy the DC's colour info into a buffer in a single call.
Here is a C++ example, but the methodology & call sequence is the same as for VB.

Figured out why it was so slow, it was because I was using ReDim on every iteration of the loop - thanks for the help anyways
Martin

Related

How can I store images in an array in octave?

I have an read image got with the imread function.
Now, I need to create a random number of images with noise, appling the noise function.
The main problem is: The amount of images will be random. so I tried to create a cell array and store the images in each position (array(1)=img1, array(2)=img2, and so on). But using it, the array(1) and so on doesn't let me work with my image.
So how can I really put all of them in a array and use them normaly?
Tank you!
MATLAB and Octave have pretty much the same language.
Please try the following:
for i=1:length(images)
array_of_images{i}=images(i);
end
I think it should work something similar or exactly this

Simple Multi-Blob Detection of a Binary Image?

If there is a given 2d array of an image, where threshold has been done and now is in binary information.
Is there any particular way to process this image to that I get multiple blob's coordinates on the image?
I can't use openCV because this process needs to run simultaneously on 10+ simulated robots on a custom simulator in C.
I need the blobs xy coordinates, but first I need to find those multiple blobs first.
Simplest criteria of pixel group size should be enough. But I don't have any clue how to start the coding.
PS: Single blob should be no problem. Problem is multiple blobs.
Just a head start ?
Have a look at QuickBlob which is a small, standalone C library that sounds perfectly suited for your needs.
QuickBlob comes with a small command-line tool (csv-blobs) that outputs the position and size of each blob found within the input image:
./csv-blobs white image.png
X,Y,size,color
28.37,10.90,41,white
51.64,10.36,42,white
...
Here's an example (output image is produced thanks to the show-blobs.py tiny Python utility that comes with QuickBlob):
You can go through the binary image labeling the connected parts with an algorithm like the following:
Create a 2D array of ints, labelArray, that will hold the labels of the connected regions and initiate it to all zeros.
Iterate over each binary pixel, p, row by row
A. If p is true and the corresponding value for this position in the labelArray is 0 (unlabeled), assign it to a new label and do a breadth-first search that will add all surrounding binary pixels that are also true to that same label.
The only issue now is if you have multiple blobs that are touching each other. Because you know the size of the blobs, you should be able to figure out how many blobs are in a given connected region. This is the tricky part. You can try doing a k-means clustering at this point. You can also try other methods like using binary dilation.
I know that I am very late to the party, but I am just adding this for the benefipeople who are researching this problem.
Here is a nice description that might fit your needs.
http://www.mcs.csueastbay.edu/~grewe/CS6825/Mat/BinaryImageProcessing/BlobDetection.htm

Fastest method to check if an array is empty

I want to know the fastest way to check if an array is empty in VB.NET. The array is already initialized so I can't use any of the checks that look at that. This is the current code below:
If Not (cubes(threadnumber)(i).objects.GetLength(0) = 0) Then
cubes(threadnumber)(i).objects = New Double() {}
ReDim cubes(threadnumber)(i).objects(-1)
End If
I've done some testing and I know that using .GetUpperBound is a little faster, but I'm not sure if this will work because I think .GetUpperBound returns a 0 if the array length is 1.
Any/all methods to speed this up (even fractionally) will be tremendously helpful. This program takes ages to compleate and the first line of the above code is a big portion of the time, it's called 136 million times.
Also if anyone knows how to speed up For...Next loops that'd be great too!
Length is about 2x faster than GetLength on my system (calling Length 136M times takes 0.650 seconds, while calling GetLength (0) takes 1.480 seconds).
I also do not understand why you ReDim your array, you've already created a new one.
I believe this will be the fastest code if cubes is a multi-dimensional array:
If cubes(threadnumber)(i).objects.Length > 0 Then
cubes(threadnumber)(i).objects = New Double() {}
End If
If cubes is not a multi-dimensional array (like List for instance), you should take the cubes(threadnumber) code out of the loop.
Update
Length is 6x faster than GetLength when running in Release mode without the debugger, in which case Length takes 0.181s and GetLength 1.175s on my system. This is likely because the JIT will inline the call to Length, but not the call to GetLength.
This is the test code I used.
if myarray is nothing then...
or
if myarray isnot nothing then...
GetLength is the fastest way I know of to see if an array has elements in it. I don't think you will speed up this piece of code.
However, the code that is calling this 136 million times could probably be optimised.
Looking at your code:
If Not (cubes(threadnumber)(i).objects.GetLength(0) = 0) Then
cubes(threadnumber)(i).objects = New Double() {}
ReDim cubes(threadnumber)(i).objects(-1)
End If
I'm guessing that the reason you are testing if it has elements is so you can redim the array to free up memory. A better way to free up memory might be to clear the cubes object instead and allow the arrays to fall out of scope.
After some intensive testing and analyzing I've found what appears to be the quickest method (so far at least). Making this small change has sped up my program 500-600%.
When there is an item added to the object arrays I also add the index of the second dimension of the cubes to a list IF the index of the second dimension is not already in the list. Any other suggest would be welcome though.

redrawing an array in C

I just started with C, but I had some knowledge of PHP, so I decided to do some 'more complicated' stuff, as for a beginner :)
I used two nested loops to print an 50x50 array. It isn't very slow, but I included a movement with arrow keys to it to move one symbol, X (player) around the array. Every time a move is made, whole array needs to be refreshed, which I did by:
system("cls");
for(x=0;x<50;x++)
{
for(y=0;y<50;y++)
{
printf("%c",table[x][y]);
}
printf("\n");
}
Which is very sloppy solution and whole array 'blinks' while it refreshes after every move.
Is there any more efficient way of doing that in C?
You would probably have to use some sort of shell graphics library like ncurses to move stuff around your array without it blinking when you redraw it. There's not really a simple way to avoid that when you're just using printf to display your grid as output.
I assume you're using Windows (because of the cls).
Maybe ANSI.SYS escape sequences are the simplest way without a library.
You can probably avoid flickering if you move the cursor and overwrite the display contents without clearing the old contents.
There's an example on "Reading and Writing Blocks of Characters and Attributes" with the Win32 Console:
http://msdn.microsoft.com/en-us/library/ms685032%28v=vs.85%29.aspx
Edit: explained the link.

Do arrays in AS2 have to be reset to the beginning when re-used?

I am working with an older and undocumented set of ActionScript (AS2) and I have found that an array, when looping through it the second time, does not give the proper results. It has been a while since I used ActionScript - does the array need to be reset before the second time through another for loop?
For instance PHP has reset() which returns the array's pointer back to the first item in the array.
There's is no such thing as pointers in ActionScript.
You can target each and every item in an Array by simply targeting it with myArray[index], and no pointer needs to be reset to be able to re-read it.
If your two loops produce different results, I would suggest looking into code that could change anything in it between the two loops or in the first one.
Maybe you could post it here ?

Resources