What's a good way of pattern recognition with existing template? - algorithm

Suppose that I have a 6 by 6 matrix/image A with a pattern in it, i.e.
0 0 0 0 0 0
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
And suppose that I have a template of Pattern that I want to recognize,
i.e. a 5 by 5 matrix B
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
Suppose we can apply matrix algebra, element wise calculation.
What's the quickest and efficient way to recognize the existence of pattern indicated by matrix B in matrix A?(Notice the "angle" of rotation in A was generate treated as unknown.)

Related

Find 4-neighbors using J

I'm trying to find the 4-neighbors of all 1's in a matrix of 0's and 1's using the J programming language. I have a method worked out, but am trying to find a method that is more compact.
To illustrate, let's say I have the matrix M—
] M=. 4 4$0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 0
and I want to generate—
0 0 1 0
0 1 0 1
0 0 1 0
0 0 0 0
I've sorted something close (which I owe to this little gem: https://www.reddit.com/r/cellular_automata/comments/9kw21u/i_made_a_34byte_implementation_of_conways_game_of/)—
] +/+/(|:i:1*(2 2)$1 0 0 1)&|.M
0 0 1 0
0 1 2 1
0 0 1 0
0 0 0 0
which is fine because I'll be weighting the initial 1's anyway (and the actual numbers aren't really that important for my application anyway). But I feel like this could be more compact and I've just hit a wall. And the compactness of the expression actually is important to my application.
Building on #Eelvex comment solution, if you are willing to make the verb dyadic it becomes pretty simple. The left argument can be the rotation matrix and then the result is composed with +./ which is a logical or and can be weighted however you want.
] M0=. 4 4$0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
0 0 0 0
] m =.2,\5$0,i:1
0 _1
_1 0
0 1
1 0
m +./#:|. M0
0 0 1 0
0 1 0 1
0 0 1 0
0 0 0 0
There is still an issue with the edges (which wrap) around, but that also occurs with your original solution, so I am hoping that you are not concerned with that.
] M1=. 4 4$1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
m +./#:|. M1
0 1 0 1
1 0 0 0
0 0 0 0
1 0 0 0
If you did want to clean that up, you can use the slightly longer m +./#:(|.!.0), which fills the rotation with 0's.
] M2=. 4 4$ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
m +./#:(|.!.0) M2
0 0 0 0
0 0 0 0
0 0 0 1
0 0 1 0
m +./#:(|.!.0) M1
0 1 0 0
1 0 0 0
0 0 0 0
0 0 0 0

Grundy Number For a Matrix [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How to calculate the Grundy number for states of a 4*4 matrix. A valid move consists of transforming the 1s into 0s of a submatrix having all 1s.
Example:
1010
0011
0000
0000
Grundy Number = 2
I checked for smaller cases and calculated the Grundy number for that, but couldn't extend it for any binary 4*4 matrix. So, please help me to calculate this.
Note: Can convert 1 to 0 only in submatrix.
The Grundy number is calculated recursively through the reachable positions:
Start with the final position (all zeros) which is a loss (0).
0 0 0 0
0 0 0 0 = 0
0 0 0 0
0 0 0 0
Proceed to add ones to the matrix to get the values for the other configurations. Some examples with exactly one 1.
1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 = 0 0 0 0 = 0 0 1 0 = 0 0 0 1 = 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
For two 1s we have to distinguish if the 1s are adjacent and can be removed in one move or not.
1 0 1 0 1 0 0 0 1 0 0 0* 0 0 1 0
0 0 0 0 = 0 0 1 0 = 0 0 0 1 = 0 0 0 1 = 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 = 0 0 1 1 = 2
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
The same for three and more 1s.
1 0 1 0*
0 0 0 1 = 1
0 0 0 0
0 0 0 0
1 0 1 0* 1 0 0 0* 0 0 1 0*
0 0 1 0 = 0 0 1 1 = 0 0 1 1 = 3
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Finally we can evaluate the given matrix. Reachable positions from the example are marked with a star *. So we can easily see that the number we are looking for is mex(0, 1, 3) = 2.
1 0 1 0
0 0 1 1 = 2
0 0 0 0
0 0 0 0
A pseudo program could look as simple as this (the grundy function has to support scalar state and arrays or vectors of states for this to work):
grundy(0) = 0
grundy(state) = mex(grundy(reachableStates(state)))

Can`t plot matrix in Gnuplot

I have a matrix of 1s and 0s saved in file. It looks like this:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 1 1 0 0 0
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
I am trying to plot in gnuplot using command:
plot 'data.rtf' matrix with image
but when I do that I get an error:
warning: matrix contains missing or undefined values
Matrix does not represent a grid
I think I should get an image where 0 is white space and 1 is black space. I am new to gnuplot so i have no idea what might be wrong nor if i am using correct way to do it. I will be grateful for any help. Thanks.
Your file is an rtf (rich text format) file which is a markup language format, which gnuplot will not understand. You will need to create the file in a text editor (not a word processor) in order to be able to use it.
The file that you provided looks like:
{\rtf1\ansi\ansicpg1250\cocoartf1404\cocoasubrtf340
{\fonttbl\f0\fnil\fcharset0 Menlo-Regular;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\f0\fs22 \cf0 \CocoaLigature0 0 0 0 0 0 0 0 0 0 0\
0 0 0 0 0 0 0 0 0 0\
0 0 0 0 0 0 0 0 0 1\
0 0 0 0 0 0 0 0 0 0\
0 0 0 0 0 0 0 1 1 0\
0 0 0 0 0 1 1 0 0 0\
1 1 1 1 1 0 0 0 0 0\
0 0 0 0 0 0 0 0 0 0\
0 0 0 0 0 0 0 0 0 0\
0 0 0 0 0 0 0 0 0 0
Notice that it starts with a bunch of markup text. Gnuplot is designed to work with text files and not formatted text or binary files (with some limited exceptions).
Creating a text file containing your designed matrix will work just fine.
Color plots are surface-like plots, thus you have to use splot not plot
set pm3d map
set palette gray
splot 'test.txt' matrix w image

C/C++ wavelet library that return also the NxN wavelet matrix

I am looking for a C++ library for Discrete Wavelet Transform (DWT) which can also return
the NxN DWT matrix of the transform.
There was a similar question opened here
Looking for a good C/C++ wavelet library for signal processing
but I am looking for something more specific as you can see.
It would be more helpful if the library is under some non-GNU license that lets me use it in proprietary software (LGPL, MPL, BSD etc.)
Thanks in advance
The reason why this matrix is never computed is that it is very inefficient to compute the DWT using it. The FWT approach is much faster.
For a signal of length 16 and a 3-level haar transform, I found that this matrix in matlab
>> h=[1 1];
>> g=[1 -1];
>> m1=[[ones(1,8) zeros(1,8); ...
zeros(1,8) ones(1,8); ...
1 1 1 1 -1 -1 -1 -1 zeros(1,8); ...
zeros(1,8) 1 1 1 1 -1 -1 -1 -1]/sqrt(8); ...
[1 1 -1 -1 zeros(1,12); ...
zeros(1,4) 1 1 -1 -1 zeros(1,8); ...
zeros(1,8) 1 1 -1 -1 zeros(1,4); ...
zeros(1,12) 1 1 -1 -1]/sqrt(4); ...
[g zeros(1,14); ...
zeros(1,2) g zeros(1,12); ...
zeros(1,4) g zeros(1,10); ...
zeros(1,6) g zeros(1,8); ...
zeros(1,8) g zeros(1,6); ...
zeros(1,10) g zeros(1,4); ...
zeros(1,12) g zeros(1,2); ...
zeros(1,14) g]/sqrt(2)]
m1 =
A A A A A A A A 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 A A A A A A A A
A A A A -A -A -A -A 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 A A A A -A -A -A -A
B B -B -B 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 B B -B -B 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 B B -B -B 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 B B -B -B
C -C 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 C -C 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 C -C 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 C -C 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 C -C 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 C -C 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 C -C 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 C -C
where A=1/sqrt(8), B=1/sqrt(4) and C=1/sqrt(2).
corresponds to the FWT. That shows you how you build your matrix from the filters. You start with the bottom half of the matrix --a matrix of zeroes, putting filter g 2 steps further every row. then make the filter twice as wide and repeat, only now shift 4 steps at a time. repeat this until you are at the highest level of decomposition, the finally put the approximation filter in at the same width (here, 8).
just as a check
>> signal=1:16; % ramp
>> [h g]=daubcqf(2); % Haar coefficients from the Rice wavelet toolbox
>> fwt(h,signal,3) % fwt code by Jeffrey Kantor
>> m1*signal' % should produce the same vector
Hope that helps you writing it in C++. It is not difficult (a bit of bookkeeping) but as said, noone uses it because efficient algorithms do not need it.

how to convert these 7-segment decoder to boolean expression

how to convert these 7-segment decoder to boolean expression??
BCD 7-Segment decoder
A B C D a b c d e f g
0 0 0 0 0 0 0 0 0 0 1
0 0 0 1 1 0 0 1 1 1 1
0 0 1 0 0 0 1 0 0 1 0
0 0 1 1 0 0 0 0 1 1 0
0 1 0 0 1 0 0 1 1 0 0
0 1 0 1 0 1 0 0 1 0 0
0 1 1 0 0 1 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 1 0 0
I suggest you use a karnough map.
You'll need to use one for each result column, so 7 4x4 tables.
There are even a few karough map generators on the web that you can use.

Resources