Houghpeaks function in Hough transform ( matlab code ) - hough-transform

Look at the houghpeaks function for Hough transform in matlab toolbox:
peaks = houghpeaks(H, numpeaks)
peaks = houghpeaks(..., param1, val1,param2, val2)
paramter:
'NHoodSize' : Two-element vector of positive odd integers: [M N].
Question: NHoodSize must be odd? why?

Wouldn't that be because otherwise you're not landing on a cell boundary?
1 2 3
4 5 6
7 8 9
vs.
1 2
3 4
In NHOOD calculations an element in the middle is being evaluated and somehow being influenced by the surrounding values.

Related

How to get max sum of k 2x1 or 1x2 tiles in Nx3 matrix

I have a problem where I have a N x 3 matrix with int values. I need to tile it with K 2x1 or 1x2 tiles so that they do not overlap and that I get the maximum sum with the use of dynamic programming.
What would the best way be to solve such a problem?
Example 5 x 3 matrix, K = 5:
2 6 2
6 5 6
2 6 2
1 1 1
1 1 1
Good tiles: (6,2), (6,2), (6,2), (6,5), (2,1)
Result = 38
And an example with an edge case:
2 x 3 Matrix, K = 2
0 4 1
3 4 1
Good tiles: (4,1), (4,3)
Result = 12
Let's define the state of a row as the cells that are covered by some of the K bricks. You have 8 combinations (2^3) from 000 (everything is not covered) to 111 (everything is covered) (you can use binary to encode the state for efficiency).
The dynamic programming matrix will be a[row][tiles][state]. Where row is the row we are processing, going top to bottom, tiles is how many tiles you placed already, state is the state as we defined above and the value is the current maximum sum.
To fill it we go top to bottom. We simplify things by only allowing a vertical tile to be placed on the current and the row above (not below). You can iterate through tile placement combinations between the rows (some are mutually exclusive). You have 3 vertical options and 2 horizontal options on the current row (5 options, for a total of 12 combinations, if I've done the math right). Also iterate through the possible values of 'titles'. For each combination look for all possible combination that allow it's placement on the previous row (so that the vertical tiles don't overlap) take the maximum and update the dynamic matrix. Some combinations are very strict (3 vertical tiles require 000 in the row above), while some are very relaxed (1 horizontal tile allows for every posibility). Do this on paper a few times to see how it works.
As an optimization note that you only need to know the values from the previous row, as the ones above that don't factor into so you can keep only the previous row and current row.
Algorithm should look something like this
For i from 0 to N
for tiles from 0 to K
for each combination
if tiles - combination.tiles < 0: continue
m = -1
for each state compatible with combination.previous_row
m = max(m, a[i-1][tiles - combination.tiles][state])
if m > 0
a[i][tiles][combination.state] = max(a[i][tiles][combination.state], m)
The solution is the maximum between the states on last row with tiles=K.
Complexity will be N*K* 12 combinations * 2^3 states so O(N*K). Memory can be O(K) with the trick I've mentioned above.

Get X and Y positions of a Pixel given the HEIGHT , WIDTH and index of a pixel in FLATTENED array representing the image

Imagine you have this image
[[1 2 3] [4 5 6] [7 8 90]]
You flatten it into this format -
[1 2 3 4 5 6 7 8 90]
Now you are given the index of Pixel 90 to be 8.
How can you find that pixel 90 is in Row 3 and column 3?
OpenCL, similarly to other programming languages like C, C++, Java and so on, uses zero based indexing. So in this terms you are looking for Row 2 and Column 2.
Now to calculate which row that is we need to divide index position 8 by number of columns:
8 / 3 = 2
So in zero based indexing that is a second row.
Now to calculate which column that is we use modulo operator:
8 % 3 = 2
In the 2D case, a point (x,y) in a rectangle with the dimensions (sx,sy) can be represented in 1D space by a linear index n as follows:
n = x+y*sx
Converting the 1D index n back to (x,y) works as follows:
x = n%sx
y = n/sx
For the 3D case, a point (x,y,z) in the a box with dimensions (sx,sy,sz) can be represented in 1D as
n = x+(y+z*sy)*sx
and converted back to (x,y,z) like this:
z = n/(sx*sy);
temp = n%(sx*sy);
y = temp/sx;
x = temp%sx;
Note that "/" here means integer division (always rounds down the result) and "%" is the modulo operator.

How to find maximum xor in a sub matrix?

eg given 3 x 3 matrix
1 2 3
4 5 6
7 8 9
has max xor value = 15
sub matrix
2
5
8
that is column matrix having index 1
For 1D array trie is a possible approach but cant think of any approach for 2D array
A straight-forward solution is
Enumerate all possible row ranges (upper bound and lower bound)
For each range, vertically sum up the rows in the range to one row.
Say we are working on the example in the question, and we are trying to find the maximum xor in a sub matrix that starts from Row2 and ends at Row3.
Then we can vertically sum up 4 5 6 and 7 8 9 to
4 xor 7 5 xor 8 6 xor 9 = 3 13 15
use the algorithm for 1D array. In the example, we can find the maximum xor sum of 7 8 9 is 15, which represents the sub matrix
6
9
This would be a O(n^3log(max value)) solution.
You can use https://en.wikipedia.org/wiki/Fenwick_tree for fast xor calculation and updates. Segment tree is another structure that can be used. It has similar characteristics but uses more memory. Both can be used for 1D array case as well.
One way to speed up a brute force try-all-possibilities approach is to find a way of working out the result of an arbitrary sub-matrix without xorring together all its elements. You can do this if you prepare a table where the entry at T(x, y) is the xor of all elements (i,j) where i <= x and j <= y, as you can then calculate any sub-matrix by xorring together four elements.
Now if you want the answer for e.g. the submatrix M(3-10, 5-23) you should find that it is T(10, 23) ^ T(2, 23) ^ T(10, 4) ^ T(2, 4)
The first term covers all of the elements you want, but includes those with i and j values too low. The second term removes those with i values too low. The second term removes those with j values too low, but also does a double removal of those with both i and j values too low. The final term corrects for this double removal.
You can also think of this as an application of the principle of inclusion-exclusion (https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle) or draw yourself a picture showing four overlapping rectangles co-operating to draw a submatrix.

Inserteing the elements from several matrices to form a Large matrix in MATLAB [duplicate]

This question already has answers here:
Form a large matrix from n numbers of small matrices
(2 answers)
Closed 8 years ago.
I am a new to MATLAB. I have generated n numbers of smaller matrices of (3 x 1 ) by using a FOR loop. All the matrices are having random values .Now I want to concatenate all the values to form a LARGE matrix 'M'Please check out my codes below .
n= input('please input the number of criterias \n');
for k=1:1:n
fprintf('Please input the %d X %d decision matrix for no %d Criteria \n', n,n,k);
m=input('');
S=sum(m);
for i=1:1:n
for j=1:1:n
m(i,j)= m(i,j)/S(j);
end
end
rS=sum(m,2);
pk=rS/n;
fprintf('the prioritized matrix for no %d criteria ) is ::\n',k);
disp(pk);
end`
and the Command window shows the O/p like this
please input the number of criterias
3
Please input the 3 X 3 decision matrix for no 1 Criteria
[1 2 3 ; 4 5 6; 7 8 9]
the prioritized matrix for no 1 criteria ) is ::
0.1278
0.3333
0.5389
Please input the 3 X 3 decision matrix for no 2 Criteria
[4 5 6; 3 7 9; 8 1 4]
the prioritized matrix for no 2 criteria ) is ::
0.3224
0.4040
0.2736
Please input the 3 X 3 decision matrix for no 3 Criteria
[1 5 4 ; 2 7 0; 3 6 7]
the prioritized matrix for no 3 criteria ) is ::
0.2694
0.2407
0.4899
Now I want to append the values obtained from all the smaller resultant matrices (prioritized matrices) in order to form a LARGE MATRIX 'M'. 'M' shall look like this
M = [ .1278 .3224 .2644 ;
.3333 .4040 .2407 ;
.5839 .2736 .4899 ]
Now Please guide me how could i do this in an efficient way ? NOTE : 'M' is not always a 3X3 matrix , Its a huge order dimension (arround 40X40) in my real project and moreover Its not always fixed and It depends upon the USER INPUT i.e 'n' . I am extremely sorry for the previous Formatting mistakes.
Hard to see what is going on with your loops, but this example should help. Matrix concatenation is done with commas (to add columns) or semi-colons (to add rows). So if you have three row matrices of size 1x3 that look like:
m1=[.1278 .3224 .2644]
m2=[.3338 .4040 .2407]
m3=[.5839 .2736 .4899]
you can make a 3x3 matrix M concatenating your small matrices with semi-colons:
M=[m1;m2;m3]
that looks like this:
M =
0.12780 0.32240 0.26440
0.33380 0.40400 0.24070
0.58390 0.27360 0.48990

Transform Matrix A to B through swapping elements

Well, I got this homework where i must find the minimum number of swaps to convert some matrix A to other matrix B given, the constraints are very limited ("may not exceed 10 elements on the matrix and the matrix will also be N=M"), that means that it will be always a 1x1 matrix and a 2x2 matrix, (which is trivial), the problem is at the 3x3 matrix.
I already tried to backtrack the elements by seeking the manhattan distance between two elements on the matrix that are separated, multiply by two and substract - 1, e.g.
The rules of swapping are: You may swap elements that are adjacent, we define adjacent when they share the same row or the same column.
1 3 2
6 5 4
7 8 9
target:
1 2 3
4 5 6
7 8 9
The manhattan distance between {1,3} is 1, so 2*1 - 1 = 1, 1 swap needed.
for {6,4} is 2, so 2*2 - 1 = 3, 3 swaps needed, then, the final answer is 4 swaps needed.
However, my program is getting rejected by the automatic corrector, any ideas on how to solve this problem?

Resources