如何找到噪声二价矩阵的最佳重叠

发布于 2024-10-20 16:35:04 字数 1841 浏览 2 评论 0原文

我正在处理一个图像处理问题,我已将其简化如下。我有三个 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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2024-10-27 16:35:04

这是 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  

结果:

在此处输入图像描述

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:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文