Find the end of a curved line in a binary image - algorithm
I'm looking for an algorithm that will detect the end of a curved line. I'm going to convert a binary image into a point cloud as coordinates, and I need to find the end of the line so I can start another algorithm.
I was thinking of taking the average of vectors for the N nearest '1' pixels to each point, and saying that the pixel with the longest vector must be an endpoint, because if a point is in the middle of a line then the average of the vectors will cancel out. However, I figure this must be a problem that is well known in image processing so I thought I'd throw it up here to see if anybody knows a 'proper' algorithm.
If the line will only ever be one or perhaps two pixels thick, you can use the approach suggested by Malcolm McLean in a comment.
Otherwise, one way to do this is to compute, for each red pixel, the red pixel in the same component that is furthest away, as well as how far away that furthest pixel is. (In graph theory terms, the distance between these two pixels is the eccentricity of each pixel.) Pixels near the end of a long line will have the greatest eccentricities, because the shortest path between them and points at the other end of the line is long. (Notice that, whatever the maximum eccentricity turns out to be, there will be at least two pixels having it, since the distance from a to b is the same as the distance from b to a.)
If you have n red pixels, all eccentricities (and corresponding furthest pixels) can be computed in O(n^2) time: for each pixel in turn, start a BFS at that pixel, and take the deepest node you find as its furthest pixel (there may be several; any will do). Each BFS runs in O(n) time, because there are only a constant number of edges (4 or 8, depending on how you model pixel connectivity) incident on any pixel.
For robustness you might consider taking the top 10 or 50 (etc.) pixel pairs and checking that they form 2 well-separated, well-defined clusters. You could then take the average position within each cluster as your 2 endpoints.
If you apply thinning to the line, so your line is just one pixel thick, You can leverage morphologyEX and use MORPH_HITMISS in OpenCV. Essentially you create a template (kernel or filter) for every possible corner (there are 8 possible) and convolve by each one. The result of each convolution will be 1 in the place where the kernel matches and 0 otherwise. So you can do the same manually if you feell that you can do a better job in c.
here is an example. It takes as input_image any image of zeros and ones where the lines are one pixel thick.
import numpy as np
import cv2
import matplotlib.pylab as plt
def find_endoflines(input_image, show=0):
kernel_0 = np.array((
[-1, -1, -1],
[-1, 1, -1],
[-1, 1, -1]), dtype="int")
kernel_1 = np.array((
[-1, -1, -1],
[-1, 1, -1],
[1,-1, -1]), dtype="int")
kernel_2 = np.array((
[-1, -1, -1],
[1, 1, -1],
[-1,-1, -1]), dtype="int")
kernel_3 = np.array((
[1, -1, -1],
[-1, 1, -1],
[-1,-1, -1]), dtype="int")
kernel_4 = np.array((
[-1, 1, -1],
[-1, 1, -1],
[-1,-1, -1]), dtype="int")
kernel_5 = np.array((
[-1, -1, 1],
[-1, 1, -1],
[-1,-1, -1]), dtype="int")
kernel_6 = np.array((
[-1, -1, -1],
[-1, 1, 1],
[-1,-1, -1]), dtype="int")
kernel_7 = np.array((
[-1, -1, -1],
[-1, 1, -1],
[-1,-1, 1]), dtype="int")
kernel = np.array((kernel_0,kernel_1,kernel_2,kernel_3,kernel_4,kernel_5,kernel_6, kernel_7))
output_image = np.zeros(input_image.shape)
for i in np.arange(8):
out = cv2.morphologyEx(input_image, cv2.MORPH_HITMISS, kernel[i,:,:])
output_image = output_image + out
return output_image
if show == 1:
show_image = np.reshape(np.repeat(input_image, 3, axis=1),(input_image.shape[0],input_image.shape[1],3))*255
show_image[:,:,1] = show_image[:,:,1] - output_image *255
show_image[:,:,2] = show_image[:,:,2] - output_image *255
plt.imshow(show_image)
Related
Minimize the difference of the distance between points on the line
My problem is as follows: Given n points on a line segment and a threshold k, pick the points on the line so that would minimize the average difference of the distance between each consecutive point and the threshold. For example: If we were given an array of points n = [0, 2, 5, 6, 8, 9], k = 3 Output: [0, 2, 6, 9] Explanation: when we choose this path, the difference from the threshold in each interval is [1, 1, 0] which gets an average of .66 difference. If I chose [0, 2, 5, 8, 9], the differences would be [1, 0, 0, 2], which averages to .75. I understand enough dynamic programming to consider several solutions including memorization and depth-first search, but I was hoping someone could offer a specific algorithm with the best efficiency.
Clustering N points based on their distances
I am given n points on a 2D grid. I am asked to cluster the points, where points with distance <=k for some constant k are grouped together. Note that all pairs of points within a cluster must adhere to the distance rule. The way I thought about approaching this problem is, for each point, find its neighbors, where neighbors are defined by points that are within the distance k. We can assume each point and their initial neighbors form an initial cluster. Then for each point, we compare its initial cluster with all of its neighbors clusters. If all of the neighbors clusters are the same as the current points cluster, then they're joined. If at least 1 of the neighbors clusters, is not the same, then all points involved and their neighbors must be disjoint. e.g., say we have points [0, -0.5] [0, 0] [0, 0.5] [0, 1.5] and k = 1 For point [0, -0.5] we have neighbors [0, 0] and [0, 0.5]. For point [0, 0] we have neighbors [0, 0.5] and [0, -0.5]. For point [0, 0.5] we have neighbors [0, 0], [0, -0.5], [0, 1.5]. For point [0, 1.5] we have neighbors [0, 0], [0, 1.5]. So in this case, we will have 4 clusters, defined by an individual point. What is the most efficient way to implement this algorithm?
Matrix of Linear Transformation
Find a matrix for the Linear Transformation T: R2 → R3, defined by T (x, y) = (13x - 9y, -x - 2y, -11x - 6y) with respect to the basis B = {(2, 3), (-3, -4)} and C = {(-1, 2, 2), (-4, 1, 3), (1, -1, -1)} for R2 & R3 respectively. Here, the process should be to find the transformation for the vectors of B and express those as a linear combination of C and those vectors will form the matrix for linear transformation. Is my approach correct or do I need to change something?
I will show you how to do in python with sympy module import sympy # Assuming that B(x, y) = (2,3)*x + (-3, -4)*y it can be expressed as a left multiplication by B = sympy.Matrix( [[2, -3], [3, -4]]) # Then you apply T as a left multiplication by T = sympy.Matrix( [[13, -9], [-1, -2], [-11, -6]]) #And finally to get the representation on the basis C you multiply of the result # by the inverse of C C = sympy.Matrix( [[-1, -4, 1], [2, 1, -1], [2, 3, -1]]) combined = C.inv() * T * B The combined transformation matrix yelds [[-57, 77], [-16, 23], [-122, 166]])
Introduction to algorithms 22.1-7 the contradiction in answer?
I am self-studying Introduction to algorithms, and am confused by the answer to 22.1-7. Consider a directed graph with no self loop. I think it can be like picture3, so the incidence matrix B should be picture4 and the BT should be picture5 so The product of BBT should be picture6 i can understand the value in the diagonal means the the number of edge connect with the point but in BBT[1][4] the value is 1 (0*1+(-1)*(-1)+(-1)*0) i confused and don’t understand what wrong with it
To begin with, I think it's excellent that you actually tried to verify it with an example. Unfortunately, you don't specify your edge numbers, so it's unclear what your incidence matrix refers to. Suppose we use the following numbering: Then the incidence matrix is import numpy as np b = np.array([[-1, -1, 0], [1, 0, 1], [0, 1, -1]]) >>> b array([[-1, -1, 0], [ 1, 0, 1], [ 0, 1, -1]] And the product is >>> np.dot(b, b.T) array([[ 2, -1, -1], [-1, 2, -1], [-1, -1, 2]]) which does not seem to be what you got, but actually makes a lot of sense.
A fast implementation in Mathematica for Position2D
I'm looking for a fast implementation for the following, I'll call it Position2D for lack of a better term: Position2D[ matrix, sub_matrix ] which finds the locations of sub_matrix inside matrix and returns the upper left and lower right row/column of a match. For example, this: Position2D[{ {0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} }, { {2, 3}, {3, 4} }] should return this: { {{1, 3}, {2, 4}}, {{2, 2}, {3, 3}}, {{3, 1}, {4, 2}} } It should be fast enough to work quickly on 3000x2000 matrices with 100x100 sub-matrices. For simplicity, it is enough to only consider integer matrices.
Algorithm The following code is based on an efficient custom position function to find positions of (possibly overlapping) integer sequences in a large integer list. The main idea is that we can first try to eficiently find the positions where the first row of the sub-matrix is in the Flatten-ed large matrix, and then filter those, extracting full sub-matrices and comparing to the sub-matrix of interest. This will be efficient for most cases except very pathological ones (those, for which this procedure would generate a huge number of potential position candidates, while the true number of entries of the sub-matrix would be much smaller. But such cases seem rather unlikely generally, and then, further improvements to this simple scheme can be made). For large matrices, the proposed solution will be about 15-25 times faster than the solution of #Szabolcs when a compiled version of sequence positions function is used, and 3-5 times faster for the top-level implementation of sequence positions - finding function. The actual speedup depends on matrix sizes, it is more for larger matrices. The code and benchmarks are below. Code A generally efficient function for finding positions of a sub-list (sequence) These helper functions are due to Norbert Pozar and taken from this Mathgroup thread. They are used to efficiently find starting positions of an integer sequence in a larger list (see the mentioned post for details). Clear[seqPos]; fdz[v_] := Rest#DeleteDuplicates#Prepend[v, 0]; seqPos[list_List, seq_List] := Fold[ fdz[#1 (1 - Unitize[list[[#1]] - #2])] + 1 &, fdz[Range[Length[list] - Length[seq] + 1] * (1 - Unitize[list[[;; -Length[seq]]] - seq[[1]]])] + 1, Rest#seq ] - Length[seq]; Example of use: In[71]:= seqPos[{1,2,3,2,3,2,3,4},{2,3,2}] Out[71]= {2,4} A faster position-finding function for integers However fast seqPos might be, it is still the major bottleneck in my solution. Here is a compiled-to-C version of this, which gives another 5x performance boost to my code: seqposC = Compile[{{list, _Integer, 1}, {seq, _Integer, 1}}, Module[{i = 1, j = 1, res = Table[0, {Length[list]}], ctr = 0}, For[i = 1, i <= Length[list], i++, If[list[[i]] == seq[[1]], While[j < Length[seq] && i + j <= Length[list] && list[[i + j]] == seq[[j + 1]], j++ ]; If[j == Length[seq], res[[++ctr]] = i]; j = 1; ] ]; Take[res, ctr] ], CompilationTarget -> "C", RuntimeOptions -> "Speed"] Example of use: In[72]:= seqposC[{1, 2, 3, 2, 3, 2, 3, 4}, {2, 3, 2}] Out[72]= {2, 4} The benchmarks below have been redone with this function (also the code for main function is slightly modified ) Main function This is the main function. It finds positions of the first row in a matrix, and then filters them, extracting the sub-matrices at these positions and testing against the full sub-matrix of interest: Clear[Position2D]; Position2D[m_, what_,seqposF_:Automatic] := Module[{posFlat, pos2D,sp = If[seqposF === Automatic,seqposC,seqposF]}, With[{dm = Dimensions[m], dwr = Reverse#Dimensions[what]}, posFlat = sp[Flatten#m, First#what]; pos2D = Pick[Transpose[#], Total[Clip[Reverse#dm - # - dwr + 2, {0, 1}]],2] &# {Mod[posFlat, #, 1], IntegerPart[posFlat/#] + 1} &#Last[dm]; Transpose[{#, Transpose[Transpose[#] + dwr - 1]}] &# Select[pos2D, m[[Last## ;; Last## + Last#dwr - 1, First## ;; First## + First#dwr - 1]] == what & ] ] ]; For integer lists, the faster compiled subsequence position-finding function seqposC can be used (this is a default). For generic lists, one can supply e.g. seqPos, as a third argument. How it works We will use a simple example to dissect the code and explain its inner workings. This defines our test matrix and sub-matrix: m = {{0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}}; what = {{2, 3}, {3, 4}}; This computes the dimensions of the above (it is more convenient to work with reversed dimensions for a sub-matrix): In[78]:= dm=Dimensions[m] dwr=Reverse#Dimensions[what] Out[78]= {3,4} Out[79]= {2,2} This finds a list of starting positions of the first row ({2,3} here) in the Flattened main matrix. These positions are at the same time "flat" candidate positions of the top left corner of the sub-matrix: In[77]:= posFlat = seqPos[Flatten#m, First#what] Out[77]= {3, 6, 9} This will reconstruct the 2D "candidate" positions of the top left corner of a sub-matrix in a full matrix, using the dimensions of the main matrix: In[83]:= posInterm = Transpose#{Mod[posFlat,#,1],IntegerPart[posFlat/#]+1}&#Last[dm] Out[83]= {{3,1},{2,2},{1,3}} We can then try using Select to filter them out, extracting the full sub-matrix and comparing to what, but we'll run into a problem here: In[84]:= Select[posInterm, m[[Last##;;Last##+Last#dwr-1,First##;;First##+First#dwr-1]]==what&] During evaluation of In[84]:= Part::take: Cannot take positions 3 through 4 in {{0,1,2,3},{1,2,3,4},{2,3,4,5}}. >> Out[84]= {{3,1},{2,2}} Apart from the error message, the result is correct. The error message itself is due to the fact that for the last position ({1,3}) in the list, the bottom right corner of the sub-matrix will be outside the main matrix. We could of course use Quiet to simply ignore the error messages, but that's a bad style. So, we will first filter those cases out, and this is what the line Pick[Transpose[#], Total[Clip[Reverse#dm - # - dwr + 2, {0, 1}]], 2] &# is for. Specifically, consider In[90]:= Reverse#dm - # - dwr + 2 &#{Mod[posFlat, #, 1],IntegerPart[posFlat/#] + 1} &#Last[dm] Out[90]= {{1,2,3},{2,1,0}} The coordinates of the top left corners should stay within a difference of dimensions of matrix and a sub-matrix. The above sub-lists were made of x and y coordiantes of top - left corners. I added 2 to make all valid results strictly positive. We have to pick only coordiantes at those positions in Transpose#{Mod[posFlat, #, 1], IntegerPart[posFlat/#] + 1} &#Last[dm] ( which is posInterm), at which both sub-lists above have strictly positive numbers. I used Total[Clip[...,{0,1}]] to recast it into picking only at those positions at which this second list has 2 (Clip converts all positive integers to 1, and Total sums numbers in 2 sublists. The only way to get 2 is when numbers in both sublists are positive). So, we have: In[92]:= pos2D=Pick[Transpose[#],Total[Clip[Reverse#dm-#-dwr+2,{0,1}]],2]&# {Mod[posFlat,#,1],IntegerPart[posFlat/#]+1}&#Last[dm] Out[92]= {{3,1},{2,2}} After the list of 2D positions has been filtered, so that no structurally invalid positions are present, we can use Select to extract the full sub-matrices and test against the sub-matrix of interest: In[93]:= finalPos = Select[pos2D,m[[Last##;;Last##+Last#dwr-1,First##;;First##+First#dwr-1]]==what&] Out[93]= {{3,1},{2,2}} In this case, both positions are genuine. The final thing to do is to reconstruct the positions of the bottom - right corners of the submatrix and add them to the top-left corner positions. This is done by this line: In[94]:= Transpose[{#,Transpose[Transpose[#]+dwr-1]}]&#finalPos Out[94]= {{{3,1},{4,2}},{{2,2},{3,3}}} I could have used Map, but for a large list of positions, the above code would be more efficient. Example and benchmarks The original example: In[216]:= Position2D[{{0,1,2,3},{1,2,3,4},{2,3,4,5},{3,4,5,6}},{{2,3},{3,4}}] Out[216]= {{{3,1},{4,2}},{{2,2},{3,3}},{{1,3},{2,4}}} Note that my index conventions are reversed w.r.t. #Szabolcs' solution. Benchmarks for large matrices and sub-matrices Here is a power test: nmat = 1000; (* generate a large random matrix and a sub-matrix *) largeTestMat = RandomInteger[100, {2000, 3000}]; what = RandomInteger[10, {100, 100}]; (* generate upper left random positions where to insert the submatrix *) rposx = RandomInteger[{1,Last#Dimensions[largeTestMat] - Last#Dimensions[what] + 1}, nmat]; rposy = RandomInteger[{1,First#Dimensions[largeTestMat] - First#Dimensions[what] + 1},nmat]; (* insert the submatrix nmat times *) With[{dwr = Reverse#Dimensions[what]}, Do[largeTestMat[[Last#p ;; Last#p + Last#dwr - 1, First#p ;; First#p + First#dwr - 1]] = what, {p,Transpose[{rposx, rposy}]}]] Now, we test: In[358]:= (ps1 = position2D[largeTestMat,what])//Short//Timing Out[358]= {1.39,{{{1,2461},{100,2560}},<<151>>,{{1900,42},{1999,141}}}} In[359]:= (ps2 = Position2D[largeTestMat,what])//Short//Timing Out[359]= {0.062,{{{2461,1},{2560,100}},<<151>>,{{42,1900},{141,1999}}}} (the actual number of sub-matrices is smaller than the number we try to generate, since many of them overlap and "destroy" the previously inserted ones - this is so because the sub-matrix size is a sizable fraction of the matrix size in our benchmark). To compare, we should reverse the x-y indices in one of the solutions (level 3), and sort both lists, since positions may have been obtained in different order: In[360]:= Sort#ps1===Sort[Reverse[ps2,{3}]] Out[360]= True I do not exclude a possibility that further optimizations are possible.
This is my implementation: position2D[m_, k_] := Module[{di, dj, extractSubmatrix, pos}, {di, dj} = Dimensions[k] - 1; extractSubmatrix[{i_, j_}] := m[[i ;; i + di, j ;; j + dj]]; pos = Position[ListCorrelate[k, m], ListCorrelate[k, k][[1, 1]]]; pos = Select[pos, extractSubmatrix[#] == k &]; {#, # + {di, dj}} & /# pos ] It uses ListCorrelate to get a list of potential positions, then filters those that actually match. It's probably faster on packed real matrices.
As per Leonid's suggestion here's my solution. I know it isn't very efficient (it's about 600 times slower than Leonid's when I timed it) but it's very short, rememberable, and a nice illustration of a rarely used function, PartitionMap. It's from the Developer package, so it needs a Needs["Developer`"] call first. Given that, Position2D can be defined as: Position2D[m_, k_] := Position[PartitionMap[k == # &, m, Dimensions[k], {1, 1}], True] This only gives the upper-left coordinates. I feel the lower-right coordinates are really redundant, since the dimensions of the sub-matrix are known, but if the need arises one can add those to the output by prepending {#, Dimensions[k] + # - {1, 1}} & /# to the above definition.
How about something like Position2D[bigMat_?MatrixQ, smallMat_?MatrixQ] := Module[{pos, sdim = Dimensions[smallMat] - 1}, pos = Position[bigMat, smallMat[[1, 1]]]; Quiet[Select[pos, (MatchQ[ bigMat[[Sequence##Thread[Span[#, # + sdim]]]], smallMat] &)], Part::take]] which will return the top left-hand positions of the submatrices. Example: Position2D[{{0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 5, 5, 6}}, {{2, 3}, {3, _}}] (* Returns: {{1, 3}, {2, 2}, {3, 1}} *) And to search a 1000x1000 matrix, it takes about 2 seconds on my old machine SeedRandom[1] big = RandomInteger[{0, 10}, {1000, 1000}]; Position2D[big, {{1, 1, _}, {1, 1, 1}}] // Timing (* {1.88012, {{155, 91}, {295, 709}, {685, 661}, {818, 568}, {924, 45}, {981, 613}}} *)