Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given two equally sized binary matrices, compute a pattern similarity score

Say I have a two 4*4 matrices (representing binary images) and I want to compute a similarity score (from 0 to 1) of the pattern displayed on the two matrices. The number of 'on' pixels is always the same between the two matrices eg:

M1
0 1 1 1
0 0 0 1
0 0 0 0
0 0 0 0

M2
0 0 0 0
0 0 0 0
1 1 1 0
0 0 1 0

M3
1 0 0 1
0 0 0 0
0 0 0 0
1 0 0 1

M4
0 0 0 0
1 1 1 0
0 0 0 1
0 0 0 0

In this case, I want M1:M2 to give a perfect score (1) because positioning of the pattern is irrelevant. M1:M3 should give a very poor score, and M1:M4 would get a good, but imperfect, score. For now, I'm only interested in patterns in the same orientation, so checking pattern orientation is not needed.

Any help or recommendations for relevant algorithms would be much appreciated!

The final implementation of this will be written in Matlab, but I'm writing up an initial test implementation in Python, so libraries in either are fine :)

like image 633
Josha Inglis Avatar asked Apr 29 '26 23:04

Josha Inglis


1 Answers

seems like an operation on conv2 will be related , but that was just suggested. Here's a different approach, since M1 and M2 are identical they have the same principal components, so svd can be helpful as a measure of similarity, for example:

abs(sum(svd(M1)-svd(M2)))
ans =
   1.1102e-16

abs(sum(svd(M1)-svd(M4)))
ans =
   0.1189

abs(sum(svd(M3)-svd(M4)))
ans =
    0.7321
like image 104
bla Avatar answered May 01 '26 13:05

bla