2011-03-06 57 views
1

我正在处理一个图像处理问题,我简化如下。我有三个10x10矩阵,每个矩阵中的值为1或-1。每个矩阵都有一个不规则的对象,位于的某处,并且矩阵中有一些噪音。我想弄清楚如何找到矩阵的最佳对齐方式,让我排列这些对象,这样我就可以得到它们的平均值。使用1/-1编码,我知道两个矩阵的乘积(使用单元乘法而不是矩阵乘法)将在两个相乘单元格之间匹配时产生1,如果存在不匹配,因此产品的总和产生重叠的度量。有了这个,我知道我可以尝试两个矩阵的所有可能的对齐来找出哪个产生最佳重叠,但我不知道如何用3个矩阵(或更多 - 我的真实数据中有20+组)。如何找到嘈杂的二价基质的最佳重叠

为了澄清这个问题,这里是一些代码,写在R,即建立排序我处理matricies的:

#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) 

回答

1

这里是数学一个程序,你想要做什么(我认为)。

如果需要,我可以更详细地解释它。

(*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 = [email protected] 
    Commonest[ 
    [email protected](mf = 
     MorphologicalComponents[m[[i]], CornerNeighbors -> False]), 2]; 
d = mf /. {id -> x, x_Integer -> 0} /. {x -> 1}; 
{minX, maxX, minY, maxY} = 
    {[email protected][g[#]] /. g -> First, 
    [email protected][g[#]] /. g -> First, 
    [email protected][g[#]] /. g -> Last, 
    [email protected][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 @@ [email protected] 

结果:

​​