Creating dynamic array

I am missing some commands, so if you can help me - that would be awesome.

I want to creat a dynamic array, that stores the keypressed, and how many times each one pressed

for example, let’s say the user pressed the key L

if the user pressed the L key for the first time, I want add L,0 to the array
if the user pressed the L key for the third time, I want to change the objects from L,2 to L,3

which commands do I need?

1 Like

Unfortunately, in Java it’s not so intuitive. Instead of a regular array, you can use ArrayList.

If you have specific questions, it would be great if you can post your code to show where you are stuck :slight_smile:

1 Like

You can use an IntList container for that:
final IntList keyPressCount = new IntList();

Use its method increment() to increase the count of desired index which represents your pressed key:
keyPressCount.increment('L'); // increase count to index L
keyPressCount.increment(keyCode); // increase count to last pressed key

Call method get() in order to read current count for a specific key index:
println(keyPressCount.get('L')); // read count for index L
println(keyPressCount.get(keyCode)); // read count for last key pressed

Here’s a sample sketch using IntList and its methods increment() & get():

/**
 * IntList KeyCode Count (v1.0.1)
 * GoToLoop (2021/Aug/19)
 * https://Discourse.Processing.org/t/creating-dynamic-array/31794/3
 */

final IntList keyPressCount = new IntList();

void setup() {
  size(400, 300);
  noLoop();

  keyPressCount.increment('L');
  println(keyPressCount.get('L')); // 1

  keyPressCount.increment('L');
  keyPressCount.increment('L');
  println(keyPressCount.get('L')); // 3
}

void draw() {
  background((color) random(#000000));
}

void keyPressed() {
  final int k = keyCode;

  keyPressCount.increment(k);
  println((char) k, k, keyPressCount.get(k));

  redraw();
}
2 Likes

You can also use a HashMap. HashMaps can store things in pairs, like the key being pressed and the count. Here is the syntax:

HashMap<String, Integer> letters = new HashMap<String, Integer>();

You can loop through the HashMap to check whether the key being pressed matches one of the keys in the HashMap, and if it does, use the replace() method in the HashMap to alter the count of the number of times the key was pressed. You can check this by looping through the keySet in the HashMap by using the keySet() method. If the key pressed doesn’t match any of the items in the HashMap, then you can use the put() method to add a new value and count.

1 Like

As a matter of fact, the final target is to analyze pixels of an image, and count how many pixels of each color exist in this image

I still build it in my head…

ohh, thank you. I never meant to ask someone to write the whole code. It was just an example.

The final purpose of my question is to analazye a JPG image and cound how many pixels of each color exists.

Then you should check it out my answers from this very old forum: :eyeglasses:
forum.Processing.org/two/discussion/14393/getting-the-dominant-color-of-an-image.html

2 Likes

I found it. Thanks.

80% of the code I don’t understand but I got it.

next step is to check for similar colors, means - if you find a color that is similar to another exist color (for example 100 distance) - refer it as it already existed (As I work on photograph and… Unique colors found: 141642 from: 663000

)

A color pixel in Processing is a 32-bit integer (24-bit if we disregard the alpha component):

Maybe you should consider converting from 24-bit to just 8-bit.
This way you’d have 256 colors instead of 16,777,216!
Red & green components would be converted from 8-bit (255 max) to a 3-bit (7 max) value.
While the blue component would be converted from 8-bit (255 max) to a 2-bit (3 max) value.

Check it out the solutions from the link below:

2 Likes

No, I don’t want to do that.

I want to use less colors, but I don’t want the only to use limited colors.

There are many tools and libraries focused on counting groups of colors in an image. In general, counting many colors as one color is called “color quantization”:

Related past discussion:

If you want to group colors using a strategy that maintains visual qualities of the original color space, perhaps start with Median Cut – we had a past conversation pointing to the Java implementation of Median Cut available from ImageJ. javax also has a ColorQuantizer.

OpenCV also has some quantization built-in via k-means clustering. There is a good discussion of approaches using OpenCV here:

I’m guessing that is what you mean – rather than using a stock palette, you want to adaptively choose a small number of colors that best approximate the appearance of your original image. Is that right?

2 Likes

Thanks for your reply.

I want to reduce the number of the colors, in order to change it style.

if I need to reduce from 110K to 50K, or any change below 80K will spoil the image? I don’t know yet.
Maybe I can reduce it to 30K or even to 12K and it still look similar to the original ? I don’t know yet

Will have to run some tests, to find the right number