0

In reference to calculating adjacency matrix from gradient of image, I found something in python. large-adjacency-matrix-from-image-in-python

I want to calculate an adjacency matrix based on 4 or 8 neighboring pixels. I also found http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3408910/

How can i do this with 4 or 8 neighbors? I want to do this in C++. I already have gradient image for use.

Community
  • 1
  • 1
Mandar
  • 131
  • 1
  • 9

1 Answers1

1

For the sake of simplicity, assume that the gradient image is a square pixel bitmap of size n x n. Assign an ordinal number to each pixel by row-major counting starting in the northwestern corner.

Define the (n^2 x n^2) adjacency matrix A = (a_ij)_i,j=1..n^2 as follows:

a_i(i-n) = 1; i > n             // northern neighbour
a_i(i+1) = 1; (i-1) mod n < n-1 // eastern neighbour
a_i(i-1) = 1; (i-1) mod n > 0   // western neighbour
a_i(i+n) = 1; i <= n^2 - n      // southern neighbour
a_ij     = 0; else

For 8 neighbours per pixel add

a_i(i-n+1) = 1; i > n and (i-n-1) mod n < n-1          // northeastern neighbour
a_i(i-n-1) = 1; i > n and (i-n-1) mod n > 0            // northwestern neighbour
a_i(i+n+1) = 1; i <= n^2 - n and (i+n-1) mod n < n-1   // southeastern neighbour
a_i(i+n-1) = 1; i <= n^2 - n and (i+n-1) mod n > 0     // southwestern neighbour

Instead of 1 you may assign the weights calculated from the gradient between adjacent pixels. Note that 0 entries would change to M, M representing a sufficiently large number ( infinite, since the respective cells are no neighbours, but that requires the implementation take special provisions ).

A will be sparse and will have a regular structure, for efficiency you should probably employ a class for sparse matrix processing. This SO question provides some suggestions.

Community
  • 1
  • 1
collapsar
  • 17,010
  • 4
  • 35
  • 61
  • everywhere, where you wrote mod , do you mean | (pipe) i.e. logic symbol for or? – Mandar Nov 23 '15 at 00:37
  • @Mandar No, `mod` means the modulo function (remainder of non-negative arg1 when divided by positive integer arg2). – collapsar Nov 23 '15 at 10:19
  • :( I can't understand ur codeblocks :( I am so sorry please elaborate. – Mandar Nov 23 '15 at 11:10
  • @Mandar These are no code blocks but (somewhat) mathematical notation to indicate which entries have to be set in the adjacency matrix. Do you follow the dimensioning of the adjacency matrix ? The main task is to map pixel coords to the index range of the adjacency matrix. In principle, any 1-on-1 mapping works. The suggested mapping scans line by line left to right and counts pixels, i.e.: `(1,1) -> 1, (1,2) -> 2, ..., (1,n) -> n, (2,1) -> n+1, ... (2, n) -> 2n, ..., (n,1) -> n*n - n + 1, (n,n) -> n*n`. Given this mapping, the answer specifies the cells of the adjacency matrix to be filled. – collapsar Nov 23 '15 at 11:49
  • Ok i have tried reading your answer again and again... But I can not make it into a code. I understand the mapping part. That I need to take n x m image to nm x nm adj matrix. But my problem is i still do not get what u have written in ur answer. what is `A = (a_ij)_i,j=1..n^2`? :( and ur somewhat mathematical notations? `a_i(i-n) = 1; i > n`? do u mean `if(i > n){a_i(i-n)}` ? – Mandar Dec 08 '15 at 14:28
  • 1
    The adj matrix is indexed by the ordinal number of individual pixels. Any kind of counting is equivalent in principle, lets scan columns line by line. Thus pixel `(1,1)` gets index `1`, `(1,m)` index `m`, `(2,1)` maps to `m+1` etc. up to `(n,m)` indexed by `n*m`. In the 'code blocks' in my answer, I just try to concisely describe the adj matrix elements that are set to 1. Example 1: Let `n=100, m=`200`; `(3,4), (3,5)` are neighboring pixels; their corresponding indices are `2*200+4 = 404, 2*200+5 = 405` ( they were counted after 2 full rows of 200 pixels each as 4th and 5th pixel ). ... – collapsar Dec 08 '15 at 14:59
  • 1
    ... Element `(404, 405)` of the adjacency matrix thus has to carry a `1`. Example 2: Let `n=100, m=`200`; `(4,4), (3,4)` are also neighbors; their corresponding indices are `3*200+4 = 604, 2*200+4 = 404`. Element `(604, 404)` of the adjacency matrix must be set `1`. Example 3: `a_i(i-m) = 1; i > m` means 'For each index `i > m` (ie. which marks a pixel not in the topmost scan line), set entry `(i, i-m)` of the adj matrix to `1` (compare this to example 2); in code: `if (i > m) { adj[i][i-m] = 1; }`. – collapsar Dec 08 '15 at 14:59
  • thanks for ur help... es ist goldwertig... ich versuche nun wieder. – Mandar Dec 08 '15 at 16:26