Tak
Tak

Reputation: 3616

Connect certain pixels in certain row with certain pixels in the next row in an image Matlab

I have a martrix A representing a 480x640 image and another matrix index_matrix of size 480xN which contains some indices as shown:

row_index col_index(es)
1          210
2          210
3          [179,210]
4          [182,210]
5          206
6          206
.
.
.
480

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_210, the third row represents the index of two pixels located in row_3 col_179 and row_3 col_210. So I want to connect only the pixels of martix A with indexes in the index_matrix such that I'll move row by row in the index_matrix and will connect the pixels of the current row with pixels in the the row below and so on. For example working on the above example: in row_1 in index_matrix the col_index is 210 and the next row row_2 in index_matrix the col_index is 210 so I want to connect pixel(1,210) with pixel(2,210), then for the next row row_3 in index_matrix the col_indexes are 179 and 210 so I want to connect pixel (2,210) with both (3,179) and (3,210) but pixel (1,210) will not be connected to them because its not the row directly above it, and so on. So the main idea is to connect certain pixels in every row with the certain pixels in the next row. So I'm asking if anyone could please advise.

Upvotes: 0

Views: 158

Answers (1)

Buck Thorn
Buck Thorn

Reputation: 5073

If I understood your question correctly, the following will get you started. It displays a blank image with a black rectangular strip on the upper left (to help with orientation), then overlays connecting green lines with connectivities defined in A starting from row 1 at top.

% example white image with black strip to help with orientation
nr = 480;
nc = 640; 
clp = 40; 
arr = [ones(nr,nc-clp) [zeros(nr/2,clp);ones(nr/2,clp)]] *255;

h=figure;
imshow(arr) 

% A for this example using random column positions
A = [[1:nr]' floor((randn(nr,1)-0.5)*50)+300];        

hold on
plot(A(1,2),1,'ro','MarkerFaceColor','r','MarkerSize',5)  % <-- just to show starting row  

for ii=1:size(A,1)-1
    for jj=1:nnz(A(ii,:))-1  
        for kk = 1:nnz(A(ii+1,:))-1
             line([A(ii,1+jj) A(ii+1,1+kk)],[A(ii,1) A(ii+1,1)],'color','g','linewidth',1)
        end
    end
end

For your particular case with a cell array containing the connectivities in retVal, the following should work:

imshow(depth)

for ii=1:length(retVal)-1
    for jj=1:nnz(retVal{ii})  
        for kk = 1:nnz(retVal{ii+1})
             line([retVal{ii}(jj) retVal{ii+1}(kk)],[ii ii+1],'color','g','linewidth',1)
        end
    end
end

Here's the output:

enter image description here

Upvotes: 1

Related Questions