0
votes

I have a martrix A representing a 640x480 image and another matrix index_matrix which contains some indices as shown:

[1  639;
212 210;
213 208;
214 209;
215 211;
216 210;
217 211];

The first col is corresponds to the row index in martix A and the second col corresponds to the col index in matrix A, so each row in the index_matrix represents a the index of a pixel in martix A, e.g the first row in the above example of index_matrix represents the index of the pixel located in row_1 col_639. So I want to connect only the points of martix A found in the index_matrix, where each point of those points will be connected to the nearest two other points in the index_matrix. This is like building a tree or a graph between these points. Such that the edges of this graph connecting two points should be drawn on pixels having values greater than zero, so if a pixel of value equal to zero was in the edge path, this edge connecting the two points will not be constructed and the two points will not be connected. So as shown in this fig if pixels A, B and C's indices where in the index_matrix and A and B will be connected, B and C will be connected, while A and C will not be connected because when constructing the path of the edge will path through a zero valued pixel "circled". So I'm asking if anyone could please advise how can I do this?

1
Do you want the red line in your example "interrupted" by the zero pixel, or not drawn at all?Floris
@Floris just to clarify that these values are the pixels values. I'm not sure what do you mean by "interrupted" but yes if during constructing the edge path a zero pixel is found this edge will not be drawnTak
@Floris I'm wondering if you have any suggestions please?Tak

1 Answers

1
votes

Use the plot command to draw a line between two points on your image. What you can do is store the original image (say, I) and keep drawing on another image (say, C).

Suppose we start at the second point (212,210). Then we need two lines, (212,210) - (1,639) and (212,210) - (213,208). Let's look at the first line.

You can either compute the discrete points on the line segment between the two points or use the plot function to draw it on a blank image say M = zeros(size(I));. Now, M (for mask) will only have white pixels where the line would be drawn.

You can then make it a logical mask (M == 255) and check if there are any zeros in the original image I which correspond to true pixels in M.

UPDATE: Since the method of using a mask every time is a bit lame, and using the equation of a line and discretizing is not the best way to draw a line, I would recommend using the bresenham function to obtain a list of points on the line segment. You can easily check the values and decide to keep the line or not without messing with masks.