3

我目前正在尝试为高斯马尔可夫随机场创建一个精度矩阵。假设我在 6x6 的空间网格中有随机变量。然后我将有一个 36x36 的精度矩阵。

现在假设我有一个 3x3 的邻居,那么我的精度矩阵将是

Q= nnbs[1] -1      0        0    0     0    -1.......0
   -1      nnbs[2] -1       0    0     0     0 ......0
   0       -1      nnbs[3]  -1   0     0     0 ......0
   ...................................................
   ...................................................

等等。谁能建议我如何编码这个精度矩阵。我的意思是如果我将窗口大小/邻域大小更改为 5x5,那么我将有一个新的精度矩阵。我该如何编码?其中 nnbs 是该元素的邻居数

rows=20;
columns=20;

%Random initialization
data=zeros(1000,3);
index=1;
value=-1;

%3x3 neighborhood
%For each element the neighbors are accessible within 1 hop so neighbors=1
neighbors=1;


for i=1:rows
    for j=1:columns

        for k=1:neighbors
            %same row right
            if j+k <= columns
                data(index,1) = (i-1)*columns+j;
                data(index,2) = ((i-1)*columns) + (j+k);
                data(index,3) = value;
                index=index+1;
            end

            %same row left
            if j-k >= 1;
                data(index,1) = (i-1)*columns+j;
                data(index,2) = ((i-1)*columns) + (j-k);
                data(index,3) = value;
                index=index+1;
            end
        end


        %row below -> bottom left right
        for k=i+1:i+neighbors
            if k <= rows
                %bottom
                data(index,1) = (i-1)*columns+j;
                data(index,2) = (k-1)*columns + j;
                data(index,3) = value;
                index=index+1;

                for l=1:neighbors
                    %right
                    if j+l <= columns
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns) + (j+1);
                        data(index,3) = value;
                        index=index+1;
                    end

                    %left
                    if j-l >= 1;
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns)+(j-1);
                        data(index,3) = value;
                        index=index+1;
                    end
                end

            end


        end




        %row above top left right
        for k=i-1:i-neighbors
            if k >= 1
                %top
                data(index,1) = (i-1)*columns+j;
                data(index,2) = ((k-1)*columns) +j;
                data(index,3) = value;
                index=index+1;

                for l=1:neighbors
                    %right
                    if j+l <= columns
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns) + (j+1);
                        data(index,3) = value;
                        index=index+1;
                    end

                    %left
                    if j-k >= 1;
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns) + (j-1);
                        data(index,3) = value;
                        index=index+1;
                    end
                end
            end
        end  
    end
end

%Get the values for the diagonal elements(which is equal to the number of
%neighbors or absolute sum of the nondiagonal elements of the corresponding
%row)

diagonal_values = zeros(rows*columns,3);
for i=1:rows*columns
    pointer=find(data(:,1) == i);
    diag_value=abs(sum(data(pointer,3)));
    diagonal_values(i,1) = i;
    diagonal_values(i,2) = i;
    diagonal_values(i,3) = diag_value;
end

data(index:index+rows*columns-1,:)=diagonal_values(:,:);


Q = sparse(data(:,1), data(:,2), data(:,3), rows*columns, rows*columns);    

我尝试过这样的事情,但我认为这不是最有效的方法。我认为应该有更好的方法。

4

1 回答 1

1

A bit too late but it may be useful for someone else : Your precision matrix is a linear combination of kronecker product of symmetric Toeplitz matrix : to each neighbour type corresponds a kronecker product of 2 Toeplitz matrix. More info about toeplitz Matrix

Example : you want a precision matrix only with the horizontal link for each pixel

Writing I_n the identity matrix of size n and H_{n,p} the Symmetric Toeplitz matrix of dimension [n n] filled with 0 everywhere excepts on the p-th diagonals

H_{4,2} =

      0  1  0  0
      1  0  1  0
      0  1  0  1
      0  0  1  0

In Matlab :

H_nonSym_n_p = toeplitz(zeros(n,1), [[zeros(1,p-1) 1] zeros(1,n-p)]) ;

H_sym_n_p = H_nonSym + H_nonSym' ;

Then, if you have a [n m] image and if you want to code the horizontal neighbour of every pixel, you can express it through the kronecker product (hope the LaTeX-like code will work): Q = - I_n \otimes \H_{n,2}. And finally, to get your nnbs : something like Q = Q - diag(sum(Q,2))...

Now, if you want other links, for instance 2 horizontal and 2 vertical links : Q = - I_n \otimes \H_{n,2} - I_n \otimes \H_{n,3} - \H_{n,2} \otimes I_{n} - \H_{n,3} \otimes I_{n}. and again Q = Q - diag(sum(Q,2))

Note that the diagonal neighbours are a bit more difficult to produce but it is still represented by a kronecker product of toeplitz matrix (maybe non-symmetric this time).

于 2014-10-27T14:46:56.743 回答