Algorithm 如何寻找含噪二价矩阵的最优重叠

Algorithm 如何寻找含噪二价矩阵的最优重叠,algorithm,image-processing,random,matrix,alignment,Algorithm,Image Processing,Random,Matrix,Alignment,我正在处理一个图像处理问题,我已经简化如下。我有三个10x10矩阵,每个单元格中的值为1或-1。每个矩阵的某处都有一个不规则的对象,并且矩阵中存在一些噪声。我想弄清楚如何找到矩阵的最佳对齐方式,让我排列对象,从而得到它们的平均值 通过1/-1编码,我知道两个矩阵的乘积(使用元素相乘,而不是矩阵相乘)将在两个相乘的单元之间匹配时产生1,如果不匹配时产生-1,因此乘积之和产生重叠的度量。有了这一点,我知道我可以尝试两个矩阵的所有可能对齐,以找到哪一个可以产生最佳重叠,但我不确定如何使用3个矩阵(或者

我正在处理一个图像处理问题,我已经简化如下。我有三个10x10矩阵,每个单元格中的值为1或-1。每个矩阵的某处都有一个不规则的对象,并且矩阵中存在一些噪声。我想弄清楚如何找到矩阵的最佳对齐方式,让我排列对象,从而得到它们的平均值

通过1/-1编码,我知道两个矩阵的乘积(使用元素相乘,而不是矩阵相乘)将在两个相乘的单元之间匹配时产生1,如果不匹配时产生-1,因此乘积之和产生重叠的度量。有了这一点,我知道我可以尝试两个矩阵的所有可能对齐,以找到哪一个可以产生最佳重叠,但我不确定如何使用3个矩阵(或者更多——我的实际数据集中确实有20多个)

为了帮助澄清这个问题,下面是一些用R编写的代码,用于设置我正在处理的矩阵类型:

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

这是Mathematica中的一个程序,它可以满足您的需要(我想)

如果你需要的话,我可以详细解释一下

(*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  
(*定义临时表*)
r=m=Table[{},{100}];
(*定义噪声函数*)
噪声:=分区[随机变量[二项分布[1,05],100],
10];
对于[i=1,i=1;
(*在每个矩阵中选择连接的组件,并仅保留最大的*)
id=最后一个@
普通的[
展平@(mf=
形态学成分[m[[i]],n->False]),2];
d=mf/{id->x,x_整数->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];
(*修剪最大组件的图像*)
r[[i]]=d[[minX;;maxX,minY;;maxY]];
]
(*由于噪声较低,重复次数较多的组件是图像*)
MatrixPlot@Commonest@r  
结果:


这里有一个Mathematica中的程序,它可以满足您的需要(我想)

如果你需要的话,我可以详细解释一下

(*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  
(*定义临时表*)
r=m=Table[{},{100}];
(*定义噪声函数*)
噪声:=分区[随机变量[二项分布[1,05],100],
10];
对于[i=1,i=1;
(*在每个矩阵中选择连接的组件,并仅保留最大的*)
id=最后一个@
普通的[
展平@(mf=
形态学成分[m[[i]],n->False]),2];
d=mf/{id->x,x_整数->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];
(*修剪最大组件的图像*)
r[[i]]=d[[minX;;maxX,minY;;maxY]];
]
(*由于噪声较低,重复次数较多的组件是图像*)
MatrixPlot@Commonest@r  
结果: