Was having trouble with drawing a list of lines. Do indices play a role? If They do, what would their ordering be in the indices buffer?
I could not find a reliable example online.
Yes, they definitely play a role. Say I wish to draw the following 5 lines:
0---1
|\ |
| \ |
| \|
2---3
For GL_LINES each pair of indices specifies the first and the last point. The content of the indices buffer would be:
0 1 1 3 3 2 2 0 0 3
For GL_LINE_STRIP I specify the vertices in order I want to join with the lines. The content of the indices buffer would be:
0 1 3 2 0 3
Related
Hi how can I draw something like this:
https://pastebin.com/6TWZwPzq
from this input:
1. 3
2. 4
3. 1
4. 2
5. 4
where 1 is a first column ,2 is a second.. etc
where
Case 1:
perform a spread process.
Case 2:
Store into as many string as there are deep, then append into it. Check stringsteam from sstream package
I'm trying to perform OpenCV on a flow chart to extract the structure (image 1). All images are computer generated. I can extract the blocks fine, and remove them from the image so we are just left with the arrows (image 3).
The issue is I'm unsure how to extract the connections. I.e. when I apply HoughLinesP there is large number of lines generated for each arrow (image 2). Does anyone know a method of extracting the lines such that I only get one 'line' for each arrow extracted?
Since the image has no noise, using Hough transform is not optimal.
I would binarize the image so that I get all non-white pixels from the image.
Then, using a matched filter, I would find vertical and horizontal line segments.
Arrows in any of the four directions can be found in a similar fashion.
Filter for a line of 2 pixels thickness can be something like:
0 0 0 0 0 0
0 0 0 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
I have a tricky question about how to manipulate some data. Suppose I have the following structure of data:
_n group attr value
1 1 height 3
2 1 weight 12
3 1 length 9
4 2 weight 15
5 3 height 4
I want to have all groups have height, weight, and length. If there is initially not a value, I want to have a missing value be put in. Thus the end result would look like this:
_n group attr value
1 1 height 3
2 1 weight 12
3 1 length 9
4 2 height .
5 2 weight 15
6 2 length .
7 3 height 4
8 3 weight .
9 3 length .
I don't know how to do this, but perhaps it would involve reshape?
Another thing I thought about would be to use egen to sum by group. We could figure out that group 1 has 3 members, group 2 has 1 member, and group 3 has 1 member. Then we could perform functions on groups 2 and 3 to get them up to par. But this could get complicated.
This is (1) very easy and (2) usually the wrong way to go.
(1) fillin is dedicated to this task. Your example leaves ambiguous whether attr is a numeric variable with value labels or a string variable, but this works either way:
. clear
. input group str6 attr value
group attr value
1. 1 "height" 3
2. 1 "weight" 12
3. 1 "length" 9
4. 2 "weight" 15
5. 3 "height" 4
6. end
. fillin group attr
. list, sepby(group)
+----------------------------------+
| group attr value _fillin |
|----------------------------------|
1. | 1 height 3 0 |
2. | 1 length 9 0 |
3. | 1 weight 12 0 |
|----------------------------------|
4. | 2 height . 1 |
5. | 2 length . 1 |
6. | 2 weight 15 0 |
|----------------------------------|
7. | 3 height 4 0 |
8. | 3 length . 1 |
9. | 3 weight . 1 |
+----------------------------------+
(2) However, what use is this structure? How are you going to relate height, length and weight? Usually, you would be better off with this (assuming same data as sandbox):
reshape wide value, i(group) j(attr) string
renpfix value
list, sepby(group)
Here we end with variables group, height, length and weight. If you still want the long structure, that is now achievable with reshape long.
Notes:
For more on fillin, see the help, the manual entry and this expository note.
renpfix in this case zaps prefixes of variable names. The tacit third argument is an empty string, so the prefix value is replaced with an empty string, namely removed. In recent versions of Stata (12 up), that is now easy with rename.
Presumably these data are just a toy example, but I'd be amazed if the wide structure were not more useful for your real data too. If there's a reason for the long structure, you did not tell us about it. (If you really have panel data, that's a different story, but check out tsfill.)
Given a large image composed of smaller images stored as a matrix. I need to find out a boundary dividing the large image into two parts(not necessarily equal but preferably nearly equal) without cutting past a smaller image.
Each small image is represented by a single integer in the larger image matrix.
Ex:
1 1 2 2 2
1 1 2 2 2
3 3 3 4 4
3 3 3 4 4
is the large image matrix composed of 4 small images.
I need to find one such boundary to separate it into two smaller images such that their sizes don't differ by a very large amount.
This is my solution:
1. Start from considering the 1st row.
2. Using binary search find the start of a boundary. In above example it will be like
1 1 | 2 2 2
1 1 2 2 2
3 3 3 4 4
3 3 3 4 4
3.Proceed down until the dividing line doesn't intersect an image. If end of large image is reached then stop.
1 1 | 2 2 2
1 1 | 2 2 2
3 3 3 4 4
3 3 3 4 4
4.Again do step 1,2,3 considering the remaining rows and make horizontal line from old line to new division line.
1 1 | 2 2 2
1 1 | 2 2 2
--
3 3 3 4 4
3 3 3 4 4
1 1 | 2 2 2
1 1 | 2 2 2
-----
3 3 3 | 4 4
3 3 3 | 4 4
End of large image...Stop.
Of-course if no vertical line can be found in step 2. We can look for a horizontal line first in a similar way like in the case of:
1 1 1 1 1
1 1 1 1 1
--
3 3 3 2 2
3 3 3 2 2
and then proceed.
How can I improve on this solution?
Are there better solutions and will my algorithm fail anytime?
I will be coding in C++. A heuristic/ greedy solution will be nice as well.
If the image is somehow big enough to make sense then you could get local differences to guide your boundaries selection.
Here is an example implemented in MATLAB for simplicity but you will get the picture:
suppose we create an image similar to the one you defined:
img = [ ones(20,20), 2*ones(20,30); ones(10,20), 2*ones(10,30); 3*ones(20,30), 4*ones(20,20)]
This command creates an image 50x50, having a 20x30 sub-image 1, a 30x30 sub-image 2, a 30x20 sub-image 3 and a 20x20 sub-image 4, as depicted graphically bellow:
Ideally you would like to get the boundaries between these "trays" representing the values 1 to 4. One way to do so is to shift the image one pixel left/right and one pixel top/bottom and subtract it with the original. This will produce another image with values only in the boundary positions.
See for example in MATLAB:
mask=((img-shift(img,1) + img-shift(img',1)')~=0);
This will create a mask by adding the difference of the right-shifted image and the original with the difference of the bottom-shifted image and the original, and, finally, by comparing the result with zero (zero values will be all pixel values except in boundaries). Function shift just shifts values of a matrix right or left. There is no need to put the code here since I just want to show the concept.
So you will end-up with the following mask image:
This mask has been cropped one pixel at the right and bottom since the previous subtractions produces a border that is not needed.
In this image, true values (white pixels) are on the last pixel of the previous image, i.e. image 1 ends at the 1st boundary and image 2 begins at the next pixel, so image 1 is bounded by x=20 and y=30, and so on for the other sub-images.
I was thinking of a fast method to look for a submatrix m in a bigger mtrix M.
I also need to identify partial matches.
Couple of approaches I could think of are :
Optimize the normal bruteforce to process only incremental rows and columns.
May be extend Rabin-karp algorithm to 2-d but not sure how to handle partial matches with it.
I believe this is quite frequently encountered problem in image processing and would appreciate if someone could pour in their inputs or point me to resources/papers on this topic.
EDIT:
Smaller example:
Bigger matrix:
1 2 3 4 5
4 5 6 7 8
9 7 6 5 2
Smaller Matrix:
7 8
5 2
Result: (row: 1 col: 3)
An example of Smaller matrix which qualifies as a partial match at (1, 3):
7 9
5 2
If More than half of pixels match, then it is taken as partial match.
Thanks.
I recommend doing an internet search on "2d pattern matching algorithms". You'll get plenty of results. I'll just link the first hit on Google, a paper that presents an algorithm for your problem.
You can also take a look at the citations at the end of the paper to get an idea of other existing algorithms.
The abstract:
An algorithm for searching for a two dimensional m x m pattern in a two dimensional n x n text is presented. It performs on the average less comparisons than the size of the text: n^2/m using m^2 extra space. Basically, it uses multiple string matching on only n/m rows of the text. It runs in at most 2n^2 time and is close to the optimal n^2 time for many patterns. It steadily extends to an alphabet-independent algorithm with a similar worst case. Experimental results are included for a practical version.
There are very fast algorithms for this if you are willing to preprocess the matrix and if you have many queries for the same matrix.
Have a look at the papers on Algebraic Databases by the Research group on Multimedia Databases (Prof. Clausen, University of Bonn). Have a look at this paper for example: http://www-mmdb.iai.uni-bonn.de/download/publications/sigir-03.pdf
The basic idea is to generalize inverted list, so they use any kind of algebraic transformation, instead of just shifts in one direction as with ordinary inverted lists.
This means that this approach works whenever the modifications you need to do to the input data can be modelled algebraically. This specifically that queries which are translated in any number of dimensions, rotated, flipped etc can all be retrieved.
The paper is mainly showing this for musical data, since this is their main research interest, but you might be able to find others, which show how to adapt this to image data as well (or you can try to adapt it yourself, if you understand the principle it's quite simple).
Edit:
This idea also works with partial matches, if you define them correctly.
There is no way to do this fast if you only ever need to match one small matrix against one big matrix. But if you need to do many small matrices against big matrices, then preprocess the big matrix.
A simple example, exact match, many 3x3 matrices against one giant matrix.
Make a new "match matrix", same size as "big matrix", For each location in big matrix compute a 3x3 hash for each x,y to x+3,y+3 in big matrix. Now you just scan the match matrix for matching hashes.
You can achieve partial matches with specialized hash functions that give the same hash to things that have the same partial matching properties. Tricky.
If you want to speed up further and have memory for it, create a hash table for the match matrix, and lookup the hashes in the hash table.
The 3x3 solution will work for any test matrix 3x3 or larger. You don't need to have a perfect hash method - you need just something that will reject the majority of bad matches, and then do a full match for potential matches in the hash table.
I think you cannot just guess where the submatrix is with some approach, but you can optimize your searching.
For example, given a matrix A MxN and a submatrix B mxn, you can do like:
SearchSubMatrix (Matrix A, Matrix B)
answer = (-1, -1)
Loop1:
for i = 0 ... (M-m-1)
|
| for j = 0 ... (N-n-1)
| |
| | bool found = true
| |
| | if A[i][j] = B[0][0] then
| | |
| | | Loop2:
| | | for r = 0 ... (m-1)
| | | | for s = 0 ... (n-1)
| | | | | if B[r][s] != A[r+i][s+j] then
| | | | | | found = false
| | | | | | break Loop2
| |
| | if found then
| | | answer = (i, j)
| | | break Loop1
|
return answer
Doing this, you will reduce your search in the reason of the size of the submatrix.
Matrix Submatrix Worst Case:
1 2 3 4 2 4 [1][2][3] 4
4 3 2 1 3 2 [4][3][2] 1
1 3 2 4 [1][3]{2 4}
4 1 3 2 4 1 {3 2}
(M-m+1)(N-n+1) = (4-2+1)(4-2+1) = 9
Although this is O(M*N), it will never look M*N times, unless your submatrix has only 1 dimension.