Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In image processing, why is it recommended to loop over Y first and then X second?

I've read in a few places that when processing the pixels of an image, it is recommended that you loop over Y and then X pixels because it is more likely to be memory efficient. Why is this so?

like image 960
Eric R. Avatar asked Oct 07 '13 01:10

Eric R.


1 Answers

A1 A2 A3 A4 
B1 B2 B3 B4 
C1 C2 C3 C4 
D1 D2 D3 D4 

Supposing that's your image (and some sort of coordinates to it's points), it is stored in memory in a string, just like:

A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4.

Let's simulate those options:

For X and then by Y:

A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3 A4 B4 C4 D4 

Now check on the string, how messy those access would be (just like random reads, right?)

Nor, For Y and then by X

A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4

See? Straight forward read!

That's why.

like image 132
Marcelo Myara Avatar answered Dec 02 '22 00:12

Marcelo Myara