-1

I am having an image.i would like to convert this image into graph.Pixels are the nodes and edges represents 8-neighbourhood. i tried

I=imread('cameraman.tif');
[r c]=size(I); 
%r = 32; c = 32;
%# Get the matrix size
diagVec1 = repmat([ones(c-1,1); 0],r,1);  %# Make the first diagonal vector
%#   (for horizontal connections)
diagVec1 = diagVec1(1:end-1);             %# Remove the last value
diagVec2 = [0; diagVec1(1:(c*(r-1)))];    %# Make the second diagonal vector
%#   (for anti-diagonal connections)
diagVec3 = ones(c*(r-1),1);               %# Make the third diagonal vector
%#   (for vertical connections)
diagVec4 = diagVec2(2:end-1);             %# Make the fourth diagonal vector
rc = r*c;
adj = spdiags([diagVec1;0],1,rc,rc);
adj = adj + spdiags([diagVec2; zeros(c-1,1)], c-1, rc,rc);
adj = adj + spdiags([diagVec3; zeros(c,1)], c, rc,rc);
adj = adj + spdiags([diagVec4; zeros(c+1,1)], c+1, rc,rc);
adj = adj + adj.';

%# plot adjacency matrix
subplot(121), spy(adj)

%# plot connected points on grid
[X Y] = meshgrid(1:c,1:r);
X = X(:); Y = Y(:);
[xx yy] = gplot(adj, [X Y]);
subplot(122), plot(xx, yy, 'ks-', 'MarkerFaceColor','r')
axis([0 r+1 0 c+1])
%# add labels
X = reshape(X',[],1) + 0.1; Y = reshape(Y',[],1) + 0.1;
text(X, Y(end:-1:1), cellstr(num2str((1:r*c)')) )

i tried this code.But out of memory problem;works fine for 32*32 image.its not a duplicate question

user1234
  • 437
  • 3
  • 6
  • 14
  • 2
    This is **too broad** and probably opinion-based. You basically want to represent an image as a graph. There are at least three ways you can represent graphs --> Adjacency List, Adjacency Matrix, Edge Lists. Each representation has advantages and disadvantages: https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs – rayryeng Jun 01 '15 at 16:47
  • 1
    Look here: http://stackoverflow.com/questions/3277541/construct-adjacency-matrix-in-matlab - Google search brought me there. Both 4 connected and 8 connected neighbourhoods are represented. Do a bit of searching and make a bit of effort before posting a question. It actually helps reduce clutter. – rayryeng Jun 01 '15 at 17:05
  • Read the post. What you want is actually independent of image contents. Think of an image as a 2D rectangular grid of nodes and you want to connect edges between the nodes. However, if you want it to depend on image contents, what may be different is that the adjacency matrix won't be 0s and 1s, but it may represent intensity differences. I'll leave that for you to figure it out. – rayryeng Jun 01 '15 at 17:08
  • Replace `[X Y] = size(I)` with `[M N] = size(I)`.... – rayryeng Jun 01 '15 at 17:26
  • Please update your post with this new code and error. It's very hard (and very annoying) to read it in a comments block. I actually don't know what the error is... only the line where it's happening. – rayryeng Jun 01 '15 at 17:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79344/discussion-between-user1234-and-rayryeng). – user1234 Jun 01 '15 at 17:36
  • I've only reopened this question because I'm going to eventually vote to close it with this link: http://stackoverflow.com/questions/3277541/construct-adjacency-matrix-in-matlab – rayryeng Jun 04 '15 at 04:19
  • i had edited the code .but it is not working for cameraman.tif.problem is still there – user1234 Jun 04 '15 at 14:59
  • I'm reopening this question because now the OP has a different issue all together... but the answer by Shai should fix the problem (see below). – rayryeng Jun 04 '15 at 22:07
  • its not fixed yet.they are creating a grid and form the adjacency.i need to get an adjacency from image, cameraman – user1234 Jun 05 '15 at 09:04

1 Answers1

1

You can use sparse_adj_matrix: a utility function that computes adjacency matrices for arbitrary grid-graphs (in particular 2D 4-connect or 8-connect).
Download the function from github and type

>> doc sparse_adj_matrix

To see how it can be used.

Shai
  • 111,146
  • 38
  • 238
  • 371