Code covered by the BSD License

# "April, 2011 Image Processing with MATLAB" webinar demo files

### Takuya Otani (view profile)

MATLAB demo files used in the image processing webinar held on April, 2011 in Japan.

detectRedCore_script.m
```%% Detecting red core
% This demo shows attempts to detect, and isolate only red cores from the
% picture.
% This demo includes following image processing techniques
% - Extracting of color of interest
% - Adjusting of histogram for analysis in grayscale
% - Applying threshold to create binary image
% - Morphological operation on grayscale image
% - Morphological operation on binary image
% - Distance Transform
% - Watershed Transform
% - Property analysis using regionprops
%
%
% Copyright 2013 The MathWorks, Inc.
% Takuya Otani takuya.otani@mathworks.co.jp
%

%% House keeping
clear all; % clear workspace
close all; % close all figure windows.
clc;       % clear command window

figure, imshow(Iorg, 'InitialMagnification', 70);
text(size(Iorg, 2), size(Iorg,1)+15, ...
'摜񋟁F@Ɨs@lw ]ȊwZ^[', ...
'FontSize', 8, ...
'HorizontalAlignment', 'right');
text(size(Iorg, 2), size(Iorg,1)+35, ...
'Image courtesy of RIKEN Brain Science Institute', ...
'FontSize', 8, ...
'HorizontalAlignment', 'right');

%% Show only red color
Ired = Iorg(:,:,1);   % Data is stored in 3 dimentional array. (Row,Col,R/G/B)
imshow(Ired, [], 'InitialMagnification', 70); title('Red only Intensity');

% Let's try using some functions to help adjust histogram for better
% grayscale viewing.
figure;
subplot(3,2,1), imshow(Ired, []), title('ORIGINAL'),
subplot(3,2,2), imhist(Ired);
subplot(3,2,3), imshow(histeq(Ired), []), title('Histogram Equalization');
subplot(3,2,4), imhist(histeq(Ired));

%% cut threshold.
% Try >>thresh_tool(Ired) to explore the image
Ired_bw_try = Ired > 50;
figure, imshow(Ired_bw_try, 'InitialMagnification', 70); title('binary Image');

%% Morphological operation to clean up the blob
% (Previous attempts did not look too good, so let's try clear up the blob
% before making it a binary image.
se = strel('square',7);
Ired_close = imclose(Ired, se);
imshow(Ired_close, [], 'InitialMagnification', 70); title('Closed result');

%% Thresholding the image to turn to binary
Ired_bw = Ired_close > 50;
imshow(Ired_bw, 'InitialMagnification', 70); title('binary Image');

%% The attempt looks good.
% Let's compare the result against previous results
h = figure;
subplot(2,2,1), imshow(Ired, []), title('original grayscale');
subplot(2,2,2), imshow(Ired_bw_try), title('binary image from original grayscale');
subplot(2,2,3), imshow(Ired_close, []), title('Closed grayscale');
subplot(2,2,4), imshow(Ired_bw), title('binary image from closed grayscale');

%% Fill open holes
Ired_bw = imfill(Ired_bw, 'holes');
figure, imshow(Ired_bw, 'InitialMagnification', 70); title('binary Image with holes filled');

%% area open to eliminate small objects
Ired_bw = bwareaopen(Ired_bw, 200);
imshow(Ired_bw, 'InitialMagnification', 70); title('binary Image w/ area open to 200 pixels or above');

%% First, calculate distance transform
Ired_dist = -bwdist(~Ired_bw);
imshow(Ired_dist, [], 'InitialMagnification', 70); title('Distance transform');

%% Force background as basin
Ired_dist(~Ired_bw) = -Inf;
Ired_lim = imhmin(Ired_dist, 2); %limit over segmentation
imshow(Ired_lim, [], 'InitialMagnification', 70); title('Background forced to basin');

%% Use Watershed to segment blobs
Ired_seg = watershed(Ired_lim);
imshow(label2rgb(Ired_seg,'jet'), 'InitialMagnification', 70);

%% use region prop to draw rectangles, and counts
figure, imshow(Iorg, 'InitialMagnification', 70);
hold on;
status = regionprops(Ired_seg,'Centroid','BoundingBox');

for x = 1:length(status)
xy = status(x).Centroid;
z = status(x).BoundingBox;
rectangle('Position',z, 'EdgeColor', 'g');
text(xy(1),xy(2),'*', 'Color', 'b');
end
numblobs = num2str(size(status,1) - 1);
text(255,500, ['Red cores found: ', numblobs], 'FontSize', 14, 'color', 'red');
hold off;
```