开发者

How to find optimal overlap of noisy bivalent matricies

开发者 https://www.devze.com 2023-02-15 07:03 出处:网络
I\'m dealing with an image processing problem that I\'ve simplified as follows. I have three 10x10 matrices, each with the values 1 or -1 in each cell. Each matrix has an irregular object located some

I'm dealing with an image processing problem that I've simplified as follows. I have three 10x10 matrices, each with the values 1 or -1 in each cell. Each matrix has an irregular object located somewhere, and there is some noise in the matrix. I'd like to figure out how to find the optimal alignment of the matrices that would let me line up the objects so I can get their average.

With the 1/-1 coding, I know that the product of two matrices (using element-wise multiplication, not matrix multipli开发者_StackOverflow中文版cation) will yield 1 if there is a match between two multiplied cells and -1 if there is a mismatch, thus the sum of the products yields a measure of overlap. With this, I know I can try out all possible alignments of two matrices to find that which yields the optimal overlap, but I'm not sure how to do this with 3 matrices (or more - I really have 20+ in my actual data set).

To help clarify the problem, here is some code, written in R, that sets up the sort of matricies I'm dealing with:

#set up the 3 matricies
m1 = c(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)
m1 = matrix(m1,10)

m2 = c(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)
m2 = matrix(m2,10)

m3 = c(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)
m3 = matrix(m3,10)

#show the matricies
image(m1)
image(m2)
image(m3)
#notice there's a "+" shaped object in each

#create noise
set.seed(1)
n1 = sample(c(1,-1),100,replace=T,prob=c(.95,.05))
n1 = matrix(n1,10)
n2 = sample(c(1,-1),100,replace=T,prob=c(.95,.05))
n2 = matrix(n2,10)
n3 = sample(c(1,-1),100,replace=T,prob=c(.95,.05))
n3 = matrix(n3,10)

#add noise to the matricies
mn1 = m1*n1
mn2 = m2*n2
mn3 = m3*n3

#show the noisy matricies
image(mn1)
image(mn2)
image(mn3)


Here is a program in Mathematica that does what you want (I think).

I may explain it in more detail, if you need.

(*define temp tables*)
r = m = Table[{}, {100}];
(*define noise function*)
noise := Partition[RandomVariate[BinomialDistribution[1, .05], 100], 
   10];
For[i = 1, i <= 100, i++,
 (*generate 100 10x10 matrices with the random cross and noise added*)
 w = RandomInteger[6]; h = w = RandomInteger[6];
 m[[i]] = (ArrayPad[CrossMatrix[4, 4], {{w, 6 - w}, {h, 6 - h}}] + 
     noise) /. 2 -> 1;

 (*Select connected components in each matrix and keep only the biggest*)
 id = Last@
   Commonest[
    Flatten@(mf = 
       MorphologicalComponents[m[[i]], CornerNeighbors -> False]), 2];
 d = mf /. {id -> x, x_Integer -> 0} /. {x -> 1};
 {minX, maxX, minY, maxY} =
  {Min@Thread[g[#]] /. g -> First,
     Max@Thread[g[#]] /. g -> First,
     Min@Thread[g[#]] /. g -> Last,
     Max@Thread[g[#]] /. g -> Last} &@Position[d, 1];

 (*Trim the image of the biggest component *)
 r[[i]] = d[[minX ;; maxX, minY ;; maxY]];
 ]
(*As the noise is low, the more repeated component is the image*)
MatrixPlot @@ Commonest@r  

Result:

How to find optimal overlap of noisy bivalent matricies

0

精彩评论

暂无评论...
验证码 换一张
取 消