image thumbnail

March 2013 Japanese webinar "Introduction to Image Processing and Computer Vision with MATLAB"

by

 

25 Mar 2013 (Updated )

Script used in the Japanese webinar for image processing and computer vision held on Mar. 01, 2013.

S3_findObjectAlgorithm.m
%% Copyright 2013 The MathWorks, Inc.
% This is a script for card matching
%
% Original version created by Takuya Otani
% Senior Application Engineer, MathWorks, Japan


%% Rs[^rWfF
%   _Eʂ̒o}b`OɂAvJ[h̒o
%   ZNVs
clear all; close all; clc;

%% QƉ摜̓ǂݍ (_C̃NC[) 
ref_img = imread('cards/Queen_diamonds.png'); 
ref_img_gray = rgb2gray(ref_img);
ref_img_w = size(ref_img_gray,2);
ref_img_h = size(ref_img_gray,1);
% SURF _̒oƕ\
ref_keys = detectSURFFeatures(ref_img_gray, ...
                             'MetricThreshold', 100);
[ref_features  ref_validPts] ...
         = extractFeatures(ref_img_gray,  ref_keys);
figure; imshow(ref_img); hold on;
plot(ref_keys.selectStrongest(80)); hold off;

%% Ώۉ摜̓ǂݍ
vid_img      = imresize(imread('cards/random_cards.jpg'),0.4); 
vid_img_gray = rgb2gray(vid_img);
vid_img_w = size(vid_img_gray,2);
vid_img_h = size(vid_img_gray,1);
% SURF _̒oƕ\
vid_keys = detectSURFFeatures(vid_img_gray, ...
                               'MetricThreshold', 100);
[vid_features vid_validPts] ...
         = extractFeatures(vid_img_gray, vid_keys);
figure; imshow(vid_img, 'InitialMagnification', 'fit');
hold on; plot(vid_keys.selectStrongest(300)); hold off;

%% _̃}b`O
index_pairs = matchFeatures(ref_features, vid_features);
ref_matched_points = ref_validPts(index_pairs(:,1));
vid_matched_points = vid_validPts(index_pairs(:,2));
% }b`Oʂ̕\
figure, showMatchedFeatures(ref_img, vid_img, ...
      ref_matched_points, vid_matched_points, 'montage')
title('Showing All Matches');

%% ϊs̐ƁAΉ_̏AΉ_̕\
gte = vision.GeometricTransformEstimator; 
gte.Method = 'Random Sample Consensus (RANSAC)';
gte.Transform = 'Nonreflective similarity';
[tform inlierIdx] = step(gte, ...
                         ref_matched_points.Location, ...
                         vid_matched_points.Location);
figure, showMatchedFeatures(ref_img, vid_img, ...
               ref_matched_points(inlierIdx), ...
               vid_matched_points(inlierIdx), 'montage')
title('Inliers Only');

%% ‚J[h̕\
% First setup the original location, and width/height
x = 1; y = 1; w = ref_img_w; h = ref_img_h;
rect_coord = [x, y, 1; x+w, y, 1; x+w, y+h, 1; x, y+h, 1; ];
% peform a geometric transformation
rect_coord_tr = double(rect_coord * tform);
% Show Boundary on image
figure, imshow(vid_img,  'InitialMagnification', 50); 
h = impoly(gca, rect_coord_tr);
setColor(h,'blue');

% Now we know the region. Create a mask of this region from the polygon
mask = poly2mask(rect_coord_tr(:,1), rect_coord_tr(:,2), vid_img_h, vid_img_w);
imshow(mask,  'InitialMagnification', 50);
mask3 = repmat(mask, [1 1 3]);

% Show the result with darkened background
vid_img(~mask3) = vid_img(~mask3) .* 0.3;
imshow(vid_img , 'InitialMagnification', 50);

% _C̃NC[̌o I

%% Find Object Demo : SẴJ[h̔F
% 
% 1. SURF to detect & extract features for each card
% 2. Match the features
% 3. Move the rectangular region to matched location
% 4. Find the center location of the object.
% 5. Mark the detected object at the center.
clear all; close all; clc;

% --Define Geometric Transform Estimation Object
geometricTransformEstimator = vision.GeometricTransformEstimator; 
geometricTransformEstimator.Method = 'Random Sample Consensus (RANSAC)';
geometricTransformEstimator.Transform = 'Nonreflective similarity';

% --Load image & extract features
vid_img      = imresize(imread('cards/random_cards.jpg'), 0.3); 
vid_img_gray = rgb2gray(vid_img);
vid_img_w = size(vid_img_gray,2);
vid_img_h = size(vid_img_gray,1);

% Extract SURF Features
vid_keys      = detectSURFFeatures(vid_img_gray, 'MetricThreshold', 300);
[vid_features vid_validPts] = extractFeatures(vid_img_gray, vid_keys);

% --Check the directory for number of cards to detect and match
cards_prefix = 'cards/';
fileNames = ls([cards_prefix '*.png']);
nfiles = size(fileNames,1);

figure, himg = imshow(vid_img , 'InitialMagnification', 'fit');
mask_all = zeros(size(vid_img(1), vid_img(2)));

% --Start of loop 
for ii = 1:nfiles
  fileNameExt = [cards_prefix fileNames(ii,:)];
  [~,fileName,~] = fileparts(fileNameExt);
  
  % --Load each card
  ref_img = imresize(imread(fileNameExt), 0.5); 
  ref_img_gray = rgb2gray(ref_img);
  ref_img_w = size(ref_img_gray,2);
  ref_img_h = size(ref_img_gray,1);

  % Extract SURF Features
  ref_keys = detectSURFFeatures(ref_img_gray, 'MetricThreshold', 500);
  [ref_features  ref_validPts] = extractFeatures(ref_img_gray,  ref_keys);

  % --Match Features
  index_pairs = matchFeatures(ref_features, vid_features);
  % Matched Points from reference image and loaded image
  ref_matched_points = ref_validPts(index_pairs(:,1));
  vid_matched_points = vid_validPts(index_pairs(:,2));

  % --Estimate the transform matrix
  [tform inlierIdx] = step(geometricTransformEstimator, ...
                           ref_matched_points.Location, ...
                           vid_matched_points.Location);

  % --Create Rectangular Region Polygon
  % First setup the original location, and width/height
  x = 1; y = 1; w = ref_img_w; h = ref_img_h;
  poly_coord = [x, y, 1; x+w, y, 1; x+w, y+h, 1; x, y+h, 1; ];

  % peform a geometric transformation
  poly_coord_tr = double(poly_coord * tform);
  
  % --Now we know the region. Create a mask of this region from the polygon
  mask =  poly2mask(poly_coord_tr(:,1), poly_coord_tr(:,2), vid_img_h, vid_img_w);
  center = regionprops(mask, 'Centroid');

  % --Show the result with darkened background
  region_x = center.Centroid(1);
  region_y = center.Centroid(2);
  region_r = ref_img_h/5;

  fprintf('Processed %s\n', fileNameExt);  
  
  vid_img = insertObjectAnnotation(vid_img, 'circle', ...
                                   [region_x region_y region_r], fileName, ...
                                   'Color', 'red', ...
                                   'TextBoxOpacity', 0.8, ...
                                   'FontSize', 30);
  set(himg, 'CData', vid_img);
  drawnow;  
end

release(geometricTransformEstimator);

%% I

Contact us