3

I have also read:

Convert a 2D array index into a 1D index

I am trying to debug my program and I would need to understand how could we convert a 2d coordinate into a 1d coordinate.

If I have an IJ coordinate, for example: (3284,1352) and we want to access an unidimensional array which is (3492,2188), how could we achieve that?

Until now I have tried:

1) I thought that you offset for each row and inside each row for each column as:

i x j = 3284 * 1352 = 4.439.968

If we have that the coordinate (3284,1352) graphically corresponds to the segment which has a gray level intensity of 1:

enter image description here

And we try to access the unidimensional array which has for each pixel its gray value, in pixel: 4.439.968 we have:

enter image description here

A gray intensity of 14

Which is quite strange because of:

-> Segment number 14 is not being displayed by ITKSnap:

enter image description here

-> Our segment has a gray level of 1, so then we should after manually calculating which index to access, find that gray level:

enter image description here

2) The second way I have tried is to calculate it as:

column clicked * number of total rows + row clicked;

j * xLength + i;

In our example is:

1352 * 3492 + 3284 = 4.724.468

If we try to find that pixel in the data:

enter image description here

We find that the gray level is 0, which corresponds to the background.

What is wrong with the conversion?

Could you help me please?

Yone
  • 2,064
  • 5
  • 25
  • 56
  • I think it should be: `row clicked * number of total columns + column clicked;` as it usually goes through columns then rows (it goes left to right then top to bottom). – ibrahim mahrir Jun 24 '18 at 11:25

1 Answers1

3

I think you need something like this: index = (y * maxColumns) + x
(looks like your j=x and i=y)

I draw something to visulize it: 2d to 1d array

timlg07
  • 547
  • 6
  • 20
  • 1
    I don't think that `Math.round` call is necessary. It all just integer arithmitics, right? – ibrahim mahrir Jun 24 '18 at 12:30
  • Yes, you are right I just copied it out from a project where I was calculating the total columns with `Math.sqrt`. (And I recently had problems with trying to access an array with a float value, because there is no int in JS, so I now put a `Math.round` everywhere ;D ) – timlg07 Jun 24 '18 at 12:40