5

I am trying to understand this code:

d=edge(d,'canny',.6);
figure,
imshow(d,[])

ds = bwareaopen(d,40);
figure,
imshow(ds,[])

iout = d1;
BW=ds;

iout(:,:,1) = iout;
iout(:,:,2) = iout(:,:,1);
iout(:,:,3) = iout(:,:,1);
iout(:,:,2) = min(iout(:,:,2) + BW, 1.0);
iout(:,:,3) = min(iout(:,:,3) + BW, 1.0);

I understand that d is the image and canny detector is applied and 40 pixels are neglected. The image is gray scale and contour is added to the image.

Can you please explain the next lines? What principle/algorithm is used here? I am having trouble especially with the contour detection portion of the code.

gnovice
  • 125,304
  • 15
  • 256
  • 359
user1234
  • 437
  • 3
  • 6
  • 14

1 Answers1

10

Assuming that the variable d1 stores what is likely a double precision representation (values between 0 and 1) of the original grayscale intensity image that is operated on, then the last 5 lines will turn that grayscale image into a 3-D RGB image iout that looks the same as the original grayscale image except that the contours will be overlaid on the image in cyan.

Here's an example, using the image 'cameraman.tif' that is included with the MATLAB Image Processing Toolbox:

d1 = double(imread('cameraman.tif'))./255;  % Load the image, scale from 0 to 1
subplot(2, 2, 1); imshow(d1); title('d1');  % Plot the original image
d = edge(d1, 'canny', .6);                  % Perform Canny edge detection
subplot(2, 2, 2); imshow(d); title('d');    % Plot the edges
ds = bwareaopen(d, 40);                     % Remove small edge objects
subplot(2, 2, 3); imshow(ds); title('ds');  % Plot the remaining edges
iout = d1;
BW = ds;
iout(:, :, 1) = iout;                           % Initialize red color plane
iout(:, :, 2) = iout(:, :, 1);                  % Initialize green color plane
iout(:, :, 3) = iout(:, :, 1);                  % Initialize blue color plane
iout(:, :, 2) = min(iout(:, :, 2) + BW, 1.0);   % Add edges to green color plane
iout(:, :, 3) = min(iout(:, :, 3) + BW, 1.0);   % Add edges to blue color plane
subplot(2, 2, 4); imshow(iout); title('iout');  % Plot the resulting image

And here is the figure the above code creates:

enter image description here

How it works...

The creation of the image iout has nothing to do with the edge detection algorithm. It's simply an easy way to display the edges found in the previous steps. A 2-D grayscale intensity image can't display color, so if you want to add colored contour lines to the image you have to first convert it to a format that will let you show color: either an indexed image (which is a little harder to deal with, in my experience) or a 3-D RGB image (the third dimension represents the red, green, and blue color components of each pixel).

Replicating the grayscale image 3 times in the third dimension gives us a 3-D RGB image that initially still contains gray colors (equal amounts of red, green, and blue per pixel). However, by modifying certain pixels of each color plane we can add color to the image. By adding the logical edge mask BW (ones where edges are and zeroes elsewhere) to the green and blue color planes, those pixels where the contours were found will appear cyan. The call to the function min ensures that the result of adding the images never causes a pixel color value to exceed the value 1.0, which is the maximum value an element should have for a double-precision 3-D RGB image.

It should also be noted that the code for creating the 3-D RGB image can be simplified to the following:

iout = d1;
iout(:, :, 2) = min(d1+ds, 1.0);
iout(:, :, 3) = min(d1+ds, 1.0);
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • thanks a lot.wat principle is involved in this?why to convert to rgb? is it a better method of edge detection or something like that? – user1234 May 02 '11 at 04:31
  • wat's the use of taking min value?wat's its purpose? – user1234 May 02 '11 at 04:32
  • 1
    @user689593: I added more detail to my answer to address your questions. – gnovice May 02 '11 at 14:09
  • @gnovice I have a slightly different problem. See, here we plotted `edges` detected by the canny edge detector on top of the image to which those edges belong. Thus if say the `image` is of size `300x300`, the variable `d`, that contains detected edges is also a `logical` array of `[300x300]`. What I am doing is I am finding contours of the same `[300x300]` image. Now the resultant array is of size `[2x7477]`. How do I plot these contours back to the `image` from which these were extracted? (You can see the `[300x300]` image I'm talking about [here] (http://imgur.com/C54VGIl)) – Nancy Feb 12 '16 at 05:11