title: labeling an image with piece of a puzzle
body: <p>I have a picture on which are the different pieces of a puzzle. i would like to write a program with matlab that allows me to get the different pieces of the puzzle each on a picture. so far i just manage to get the different pieces in binary picture and not in color picture. does anyone have an idea?</p>
  <p>My code:</p>
  <pre><code>close all;
  clc
  A = imread('pieces.jpg');
  figure, imshow(A);
  title('Original Image');
  level = graythresh(A);
  %D = im2gray(A);
  B = ~im2bw(A, 0.672);
  figure, imshow(B);
  C = imfill(B, 'holes');
  figure, imshow(C);
  label = bwlabel(C);
  max(max(label))
  for j = 1:max(max(label))
  [row,col] = find(label==j);
  len=max(row)-min(row)+2;
  breadth=max(col)-min(col)+2;
  target=uint8(zeros([len breadth]));
  sy = min(col)-1;
  sx = min(row)-1;
  for i=1:size(row,1)
  x=row(i,1) - sx;
  y=col(i,1) - sy;
  target(x,y) = A(row(i,1),col(i,1));
  end
  figure,imshow(target);
  end```
  
  
   
  </code></pre>