We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Pages: 1 2 
copy 2d array (Read 2538 times)
copy 2d array
Apr 9th, 2009, 4:26am
 
I have the images from a webcam sorted on hue but within those hue's I want to sort it on brightness aswell.
In pass 3 the contents 2d-array get's filled with pixels, like below where [14] is the hue value and this one saves 3 colors.

Quote:
contents[0][0] = colorvalue
~
contents[14][0] = colorvalue
contents[14][1] = colorvalue
contents[14][2] = colorvalue
etc


I created a Pass 3.5 where a simulair array get's created only it doesn't hold any values yet. So I thought it meight be better to copy the contents array but I tried some ways but I can't figure out how to copy a 2d-array. can someone help me. (tips of what is the best way are also welcome).

Code:
int w = 320, h = 240;

PImage out = new PImage(w,h);

import processing.video.*;
Capture cam;

int[][] contents = new int[256][];//256 levels for RGB/hue/brightness/etc
int[][] countsB = new int[256][];//256 levels for saved HUE from first step

int[] counts = new int[256];//256 levels for RGB/hue/brightness/etc
int[] indexlist = new int[76800];//int[] indexlist = new int[data.pixels.length];

void setup() {
size(640,480);
cam = new Capture(this, 320, 240);//++++++++++
}


void draw() {
if (cam.available() == true) {
int tmr = millis();
cam.read();
cam.loadPixels();
image(cam,0,0); //set(0, 0, cam);

//Pass 1
for (int i=0; i<cam.pixels.length; i++) {
indexlist[i] = int(hue(cam.pixels[i]));//target ////saves hue to indexlist[i]
counts[indexlist[i]]++;//increment the count table
}

//allocate arrays to hold sorted data
//possibly faster to pre-allocate [256][data.length] but would require much more RAM
//[256][65536] == 16 million int's, or a 4096x4096 image
//Pass 2
for (int i=0; i<256; i++) {
contents[i] = new int[counts[i]];
}
//Fills contents from data
//Pass 3
for (int i=0; i<cam.pixels.length; i++) {
contents[ indexlist[i] ][ contents[indexlist[i]].length - counts[indexlist[i]] ] = cam.pixels[i];
counts[indexlist[i]]--;//back down the count table
}


//allocate array to hold brightnes for each hue
//Pass 3.5
for (int i=0; i<contents.length; i++) {//outer array size 256
countsB[i] = new int[contents[i].length];//
}


//Cycle through the 256 indecies in order and list them out
//Pass 4
int idx = 0;//Location to place into output
for (int i=0; i<contents.length; i++) {//outer array size 256
for (int d=0; d<contents[i].length; d++) {//76800
out.pixels[idx] = contents[i][d];
idx++;
}
}

println("milliseconds: " + (millis() - tmr));

}

out.updatePixels();
image(out,w,0);
if ((frameCount % 15) == 0) {
saveFrame("screen#####.png");
}
}
Re: copy 2d array
Reply #1 - Apr 9th, 2009, 8:40am
 
I've been working on this after the first version of it.
Didn't think I could let it go unfinished, did ya?   Cheesy

I used recursion to perform the sort on each sub array.

Code:

int w = 1024, h = 1024;//1 Mega-pixel image. Still pretty quick...


PImage data;// = new PImage(w,h);
PImage out;// = new PImage(w,h);


void setup() {

size(800,600);

data = createImage(w, h, ARGB);
out = createImage(w, h, ARGB);


noLoop();
}


void mouseClicked() {

int tmr = millis();


for (int i=0; i<data.pixels.length; i++) {
//Note: on my machine, when searching by numeric values (recursion depth is higher),
// ARGB takes about 10 times longer than RGB, at least at 1024x1024
//data.pixels[i] = (((int)random(0xffff))<<16) | ((int)random(0xffff));//Fills data with random ARGB

data.pixels[i] = ((int)random(0xffffff)) | 0xff000000;//Fills data with random RGB values
}


out.loadPixels();
out.pixels = sortArray(data.pixels, 0);
out.updatePixels();

println("Time (ms): " + (millis() - tmr));


redraw();
}



int[] sortArray(int[] tosort, int depth) {


int[][] contents = new int[256][];//256 levels for RGB/hue/brightness/etc

int[] counts = new int[256];//256 levels for RGB/hue/brightness/etc
int[] indexlist = new int[tosort.length];


int[] sorted = new int[tosort.length];



//Pass 1
int tx = 0;
for (int i=0; i<tosort.length; i++) {


/*
switch (depth) {//This sort sequence orders them by numeric value
case 0:
indexlist[i] = int(alpha(tosort[i]));//alpha
break;
case 1:
indexlist[i] = int(red(tosort[i]));//red
break;
case 2:
indexlist[i] = int(green(tosort[i]));//green
break;
case 3:
indexlist[i] = int(blue(tosort[i]));//blue
break;
default:
indexlist[i] = int(brightness(tosort[i]));//target
}
*/

switch (depth) {//This sort sequence orders them by hue then brightness
case 0:
indexlist[i] = int(hue(tosort[i]));//alpha
break;
case 1:
indexlist[i] = int(brightness(tosort[i]));//red
break;
default:
indexlist[i] = int(brightness(tosort[i]));//target
}


counts[indexlist[i]]++;//increment the count table
}

//Pass 2
for (int i=0; i<256; i++) {
contents[i] = new int[counts[i]];
}

//Pass 3
for (int i=0; i<tosort.length; i++) {
contents[ indexlist[i] ][ contents[indexlist[i]].length - counts[indexlist[i]] ] = tosort[i];
counts[indexlist[i]]--;//back down the count table
}




if ( (depth >= 0) && (depth < 4) ) {//=======================recursion call===============
for (int i=0; i<256; i++) {



if (contents[i].length > 0) {


switch (depth) {//Sort by hue then brightness
case 0:
contents[i] = sortArray(contents[i], 1);
break;
case 1:
//stop here
//contents[i] = sortArray(contents[i], 2);
break;
default:
//indexlist[i] = int(brightness(tosort[i]));//target
}

/*
switch (depth) {//Sort by numeric value
case 0:
contents[i] = sortArray(contents[i], 1);
break;
case 1:
contents[i] = sortArray(contents[i], 2);
break;
case 2:
contents[i] = sortArray(contents[i], 3);
break;
case 3:
//don't do it anymore
//contents[i] = sortArray(contents[i], -1);
break;
default:
//do nothing
}
*/




}

}
}






//Pass 4
int idx = 0;//Location to place into output
for (int i=0; i<contents.length; i++) {//outer array size

for (int d=0; d<contents[i].length; d++) {
sorted[idx] = contents[i][d];
idx++;
}

}

return sorted;

}


void draw() {
background(0,128,128);
image(out,0,0,600,600);
}
Re: copy 2d array
Reply #2 - Apr 11th, 2009, 3:48am
 
thx, I was hoping for you Cheesy

I will later try to understand how your code works, I don't have time for that yet.
For the purpose of learning, how can I copy the 2d-array?
Re: copy 2d array
Reply #3 - Apr 11th, 2009, 5:53am
 
clankill3r wrote on Apr 11th, 2009, 3:48am:
For the purpose of learning, how can I copy the 2d-array

Code:
void setup()
{
// Declare
int[][] numbers = new int[10][];
// Initialize
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = new int[int(1 + random(10))];
for (int j = 0; j < numbers[i].length; j++)
{
numbers[i][j] = i * 100 + j;
}
}
// Show result
DumpArray(numbers);

// Declare copy
int[][] copy = new int[numbers.length][];
// Do the copy
for (int i = 0; i < numbers.length; i++)
{
copy[i] = Arrays.copyOf(numbers[i], numbers[i].length);
}

// Alter the original, to show it copied more than references...
for (int i = 0; i < numbers.length; i++)
{
for (int j = 0; j < numbers[i].length; j++)
{
numbers[i][j] = 0;
}
}

// Show result
println("");
DumpArray(copy);
DumpArray(numbers);
}

void DumpArray(int[][] arr)
{
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
print(arr[i][j] + " ");
}
println("");
}
}
Re: copy 2d array
Reply #4 - Apr 12th, 2009, 12:44pm
 
thx Philo!

hey noahbudy,

I have this under pass 1 to print how many times each hue value is there only if I look in my output.txt file it starts with 76800 and after that it's full with zero's. How does that become?

Code:
    for (int i =0; i<256; i++){
output.println(counts[i]);
}


You need this outside setup:
PrintWriter output;

this inside setup:
 output = createWriter("output.txt");

and this at bottem or something

void keyPressed() { // Press a key to save the data
 output.flush(); // Write the remaining data
 output.close(); // Finish the file
 exit(); // Stop the program
}
Re: copy 2d array
Reply #5 - Apr 12th, 2009, 3:41pm
 
Hmmm... Could you copy an example code that does this?

Using my last post and your printWriter code, I get lots of different numbers in my text file.

PS. what kind of image are you using as the source? Lots of colors? One color?
Re: copy 2d array
Reply #6 - Apr 13th, 2009, 3:56am
 
This is a small part of the output, I saw that it shows numbers after a while .. only it's still incorrect.
The first 0 to 255 meight be correct cause there are high numbers present, but for the second the values are way to low cause the total value should be 76800 (320x240).

Quote:
0 2162
1 4
2 6
3 15
4 13
5 5
6 17
7 30
8 30
9 40
10 40
11 61
12 131
13 177
14 417
15 503
16 631
17 521
18 712
19 857
20 888
21 1315
22 1414
23 1754
24 2113
25 2648
26 3421
27 4351
28 4487
29 3888
30 2915
31 2652
32 2106
33 2113
34 2345
35 2024
36 1720
37 1636
38 1551
39 2019
40 2029
41 2557
42 7917
43 2835
44 1517
45 1289
46 633
47 511
48 389
49 337
50 152
51 194
52 78
53 112
54 67
55 31
56 62
57 22
58 20
59 22
60 18
61 13
62 11
63 190
64 2
65 4
66 8
67 14
68 2
69 3
70 25
71 1
72 2
73 1
74 5
75 4
76 4
77 2
78 2
79 3
80 3
81 2
82 0
83 1
84 0
85 359
86 1
87 1
88 0
89 1
90 1
91 1
92 0
93 2
94 0
95 2
96 0
97 1
98 0
99 24
100 0
101 0
102 0
103 0
104 0
105 0
106 62
107 0
108 0
109 0
110 0
111 0
112 0
113 19
114 0
115 0
116 1
117 0
118 0
119 0
120 0
121 0
122 0
123 0
124 0
125 0
126 0
127 172
128 0
129 0
130 0
131 0
132 0
133 0
134 0
135 0
136 1
137 0
138 3
139 0
140 0
141 27
142 0
143 0
144 1
145 0
146 0
147 0
148 91
149 0
150 0
151 0
152 4
153 0
154 0
155 41
156 0
157 1
158 0
159 7
160 0
161 3
162 1
163 0
164 0
165 0
166 0
167 0
168 0
169 0
170 655
171 0
172 0
173 0
174 0
175 0
176 0
177 1
178 3
179 0
180 10
181 0
182 2
183 0
184 40
185 0
186 0
187 3
188 0
189 0
190 0
191 56
192 0
193 0
194 0
195 1
196 0
197 0
198 12
199 0
200 0
201 3
202 0
203 0
204 0
205 0
206 0
207 0
208 0
209 0
210 0
211 0
212 179
213 0
214 0
215 0
216 0
217 0
218 0
219 0
220 0
221 0
222 0
223 0
224 0
225 0
226 7
227 0
228 0
229 0
230 0
231 0
232 1
233 116
234 0
235 1
236 0
237 0
238 3
239 0
240 32
241 0
242 2
243 1
244 0
245 0
246 1
247 3
248 3
249 4
250 2
251 3
252 2
253 1
254 0
255 0
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 1
27 0
28 1
29 0
30 2
31 0
32 1
33 1
34 1
35 0
36 2
37 1
38 1
39 1
40 1
41 0
42 0
43 0
44 0
45 2
46 0
47 0
48 0
49 0
50 0
51 0
52 0
53 1
54 0
55 0
56 0
57 0
58 0
59 1
60 0
61 0
62 0
63 0
64 0
65 0
66 0
67 0
68 0
69 0
70 0
71 0
72 0
73 0
74 0
75 0
76 0
77 0
78 0
79 0
80 0
81 0
82 0
83 0
84 0
85 0
86 0
87 0
88 0
89 0
90 0
91 0
92 0
93 0
94 0
95 0
96 0
97 0
98 0
99 0
100 0
101 0
102 0
103 0
104 1
105 0
106 0
107 0
108 0
109 0
110 0
111 0
112 0
113 0
114 0
115 0
116 0
117 0
118 0
119 0
120 0
121 0
122 0
123 0
124 0
125 0
126 0
127 0
128 0
129 0
130 0
131 0
132 0
133 0
134 0
135 0
136 0
137 0
138 0
139 0
140 0
141 0
142 0
143 0
144 0
145 0
146 0
147 0
148 0
149 0
150 0
151 0
152 0
153 0
154 0
155 0
156 0
157 0
158 0
159 0
160 0
161 0
162 0
163 0
164 1
165 0
166 0
167 0
168 0
169 0
170 0
171 0
172 0
173 0
174 0
175 0
176 0
177 0
178 0
179 0
180 0
181 0
182 0
183 0
184 0
185 0
186 0
187 0
188 0
189 0
190 0
191 0
192 0
193 0
194 0
195 0
196 0
197 0
198 0
199 0
200 0
201 0
202 0
203 0
204 0
205 0
206 0
207 0
208 1523
209 77
210 110
211 426
212 7
213 0
214 0
215 0
216 0
217 0
218 0
219 0
220 0
221 0
222 0
223 0
224 0
225 0
226 0
227 0
228 0
229 0
230 0
231 0
232 0
233 0
234 0
235 0
236 0
237 0
238 0
239 0
240 0
241 0
242 0
243 0
244 0
245 0
246 0
247 0
248 0
249 0
250 0
251 0
252 0
253 0
254 0
255 0
Re: copy 2d array
Reply #7 - Apr 13th, 2009, 4:09am
 
I changed the code a litle zo it doesn't print the zero's.

Code:
    for (int i =0; i<256; i++){
if(counts[i] != 0){
output.println(i+" "+counts[i]);
}
}
output.println();


I get more values now which is odd to me.
Only the total count is still to low.

Here is a zip with all code files (I splitted the program in 3 files).
I can also past the code if you prefer.

1. Download Link:      Click here to download file
http://rapidshare.com/files/220774074/_18b_sort_hb.zip.html

Quote:
28 12
29 38
30 39
31 50
32 85
33 70
34 29
35 24
36 19
37 17
38 12
39 7
40 4
41 4
42 9
43 6
44 9
45 10
46 5
47 7
48 3
49 3
50 8
51 9
52 10
53 2
54 8
55 10
56 1
57 6
58 5
59 3
60 10
61 4
62 4
63 1
64 3
65 6
66 8
67 6
68 6
69 7
70 3
71 2
72 2
73 6
74 4
75 7
76 7
77 6
78 3
79 5
80 8
81 5
82 8
83 5
85 2
86 5
88 4
89 1
90 2
91 4
92 7
93 3
94 8
95 1
96 5
97 4
98 7
99 1
100 3
101 4
102 4
103 7
104 1
105 4
106 2
107 5
108 7
109 5
110 6
111 13
112 7
113 5
114 6
115 3
116 6
117 7
118 4
119 6
120 2
121 4
122 4
123 6
124 6
125 6
126 11
127 4
128 10
129 5
130 6
131 10
132 15
133 8
134 7
135 5
136 10
137 7
138 7
139 9
140 8
141 4
142 11
143 9
144 10
145 10
146 12
147 10
148 12
149 12
150 8
151 9
152 10
153 3
154 5
155 9
156 7
157 13
158 10
159 12
160 10
161 9
162 9
163 9
164 6
165 17
166 11
167 7
168 13
169 10
170 15
171 18
172 16
173 16
174 8
175 11
176 9
177 12
178 17
179 11
180 12
181 9
182 8
183 12
184 11
185 15
186 13
187 13
188 12
189 16
190 12
191 11
192 10
193 21
194 8
195 14
196 16
197 8
198 14
199 12
200 13
201 12
202 10
203 20
204 12
205 11
206 22
207 11
208 268
209 75
210 48
211 300
212 5
213 2
215 1
218 1


Re: copy 2d array
Reply #8 - Apr 13th, 2009, 4:40am
 
as for your colorsort code,
your switch case 1 is the same as default,
If I comment:
   case 1:
     indexlist[i] = int(brightness(tosort[i]));//red
     break;
then everything still works, so those lines are unnessacery right?

Code:
  //Pass 1
int tx = 0;
for (int i=0; i<tosort.length; i++) {

switch (depth) {//This sort sequence orders them by hue then brightness
case 0:
indexlist[i] = int(hue(tosort[i]));//alpha
break;
//works fine without
case 1:
indexlist[i] = int(brightness(tosort[i]));//red
break;
default:
indexlist[i] = int(brightness(tosort[i]));//target
}

counts[indexlist[i]]++;//increment the count table
}


And for pass 3, case 1, only line in it is break, so case 1 can go aswell?

       case 1:
         //stop here
         //contents[i] = sortArray(contents[i], 2);
         break;
Re: copy 2d array
Reply #9 - Apr 13th, 2009, 9:23am
 
Keep in mind that only the output of the first call to sort (of each frame) will add up to the total number of pixels.

Any call after that will be sorting a subset of the first.

I threw the first run of index/count pairs into a spreadsheet and they add up to 76800.

It seems to work... unless I am not understanding the trouble.

If it still works after commenting parts out...  Cool

I admit, I haven't done a thorough testing on the program. The main reason I use Processing is to see if I can get something to work. Optimization usually comes along much later.   Wink
Re: copy 2d array
Reply #10 - Apr 13th, 2009, 9:28am
 
Quote:
Keep in mind that only the output of the first call to sort (of each frame) will add up to the total number of pixels.


what do you mean?

here another part of output. Something goes really wrong and that doesnt make sense.

Quote:
41 1
78 1
96 1
214 1
245 1
250 1

69 1
134 1
197 1
215 1

32 1
235 1
250 1
251 2
252 1

30 3
44 1
46 1
149 1
191 1
196 1
216 1
254 1

27 3
28 2
42 1
44 1
49 2
85 1
246 1

Re: copy 2d array
Reply #11 - Apr 13th, 2009, 9:47am
 
When the sort function writes to the printwriter, it writes whatever is in counts[]. The values in counts[] depends on the data it is working on at the moment.

When the switch case is 0 (sort by hue), the values in counts[] will add up to the total number of pixels in the image.

When sorting by brightness, we are sorting each block of pixels that have the same hue.

If I am missing the point, smack me upside the head. Shocked
Re: copy 2d array
Reply #12 - Apr 13th, 2009, 1:29pm
 
I edited my post above.

Quote:
the values in counts[] will add up to the total number of pixels in the image.


this is the problem for some reason it doesnt?
Re: copy 2d array
Reply #13 - Apr 13th, 2009, 4:14pm
 
It will only add up once per frame. In your file, each group separated by a blank line is a separate call to the sort routine.
So only the first group will add up to 76800.

After the original sort by hue, the totals of counts[] == the number of pixels.

Unless your image is one color, each call should be only a part of the original.
Re: copy 2d array
Reply #14 - Apr 13th, 2009, 11:39pm
 
so your saying the code does run fine?
Pages: 1 2