Thursday, 28 August 2014

track an object is optical flow

vid = videoinput('winvideo', 1);
% Configure the number of frames to log upon triggering.
set(vid,'FramesPerTrigger', 1);
set(vid,'TriggerRepeat', Inf);
optical = vision.OpticalFlow( ...
    'OutputValue', 'Horizontal and vertical components in complex form');
imsize = get(vid,'VideoResolution');
shapes = vision.ShapeInserter;
shapes.Shape = 'Lines';
shapes.BorderColor = 'white';
row = 1:5:imsize(2);
column = 1:5:imsize(1);
[Cv, Rv] = meshgrid(column,row);
Rv = Rv(:)';
Cv = Cv(:)';
hVideoIn = vision.VideoPlayer;
hVideoIn.Name  = 'Original Video';
hVideoOut = vision.VideoPlayer;
hVideoOut.Name  = 'Motion Detected Video';
% Start the Image Acquisition device.
start(vid);

% Set up for stream
nFrames = 0;
while (nFrames<100)     % Process for the first 100 frames.
    % Acquire single frame of single data type.
    rgbData = getdata(vid, 1, 'single');

    % Compute the optical flow for that particular frame.
    optFlow = step(optical,rgb2gray(rgbData));

    % Downsample optical flow field.
    optFlow_DS = optFlow(row, column);
    H = imag(optFlow_DS)*50;
    V = real(optFlow_DS)*50;

    % Draw lines on top of image
    lines = [Rv; Cv; Rv+H(:)'; Cv+V(:)'];
    rgb_Out = step(shapes, rgbData, lines);

    % Send image data to video player
    % Display original video.
    step(hVideoIn, rgbData);
    % Display video along with motion vectors.
    step(hVideoOut, rgb_Out);

    % Increment frame count
    nFrames = nFrames + 1;

track car using optical flow

hbfr = vision.BinaryFileReader( ...
        'Filename', 'viptraffic.bin');
hcr = vision.ChromaResampler(...
  'Resampling', '4:2:0 (MPEG1) to 4:4:4', ...
  'InterpolationFilter', 'Pixel replication');
hcsc1 = vision.ColorSpaceConverter('Conversion', 'YCbCr to RGB');
hcsc2 = vision.ColorSpaceConverter('Conversion', 'RGB to intensity');
idtc = vision.ImageDataTypeConverter('OutputDataType', 'single');
hof = vision.OpticalFlow( ...
    'OutputValue', 'Horizontal and vertical components in complex form', ...
    'ReferenceFrameDelay', 3);
hmean1 = vision.Mean;
hmean2 = vision.Mean('RunningMean', true);
hmedianfilt = vision.MedianFilter;
hclose = vision.MorphologicalClose('Neighborhood', strel('line',5,45));
hblob = vision.BlobAnalysis( ...
                    'CentroidOutputPort', false, ...
                    'AreaOutputPort', true, ...
                    'BoundingBoxOutputPort', true, ...
                    'OutputDataType', 'double', ...
                    'NumBlobsOutputPort',  false, ...
                    'MinimumBlobAreaSource', 'Property', ...
                    'MinimumBlobArea', 250, ...
                    'MaximumBlobAreaSource', 'Property', ...
                    'MaximumBlobArea', 3600, ...
                    'FillValues', -1, ...
                    'MaximumCount', 80);
herode = vision.MorphologicalErode('Neighborhood', strel('square',2));
hshapeins1 = vision.ShapeInserter( ...
            'BorderColor', 'Custom', ...
            'CustomBorderColor', [0 1 0]);
hshapeins2 = vision.ShapeInserter( ...
            'Shape','Lines', ...
            'BorderColor', 'Custom', ...
            'CustomBorderColor', [255 255 0]);
htextins = vision.TextInserter( ...
        'Text', '%4d', ...
        'Location',  [0 0], ...
        'Color', [1 1 1], ...
        'FontSize', 12);
sz = get(0,'ScreenSize');
pos = [20 sz(4)-300 200 200];
hVideo1 = vision.VideoPlayer('Name','Original Video','Position',pos);
pos(1) = pos(1)+220; % move the next viewer to the right
hVideo2 = vision.VideoPlayer('Name','Motion Vector','Position',pos);
pos(1) = pos(1)+220;
hVideo3 = vision.VideoPlayer('Name','Thresholded Video','Position',pos);
pos(1) = pos(1)+220;
hVideo4 = vision.VideoPlayer('Name','Results','Position',pos);

% Initialize some variables used in plotting motion vectors.
MotionVecGain = 20;
line_row =  22;
borderOffset   = 5;
decimFactorRow = 5;
decimFactorCol = 5;
firstTime = true;

Tuesday, 26 August 2014

Geometric transform on an object

 htrans2 = vision.GeometricTransformer;
 img2 = checker_board(20,10);
 tfMat=[1      0     -15*2     0      1      15*2; ...
        0.4082 0      15*2    -0.4082 1.0204 35*2; ...
        1     -0.4082 5.4082*2 0      0.4082 44.5918*2]';

 polyROI  = [50  0 50 49 99 49 99 0; ...
              0  0  0 49 49 49 49 0; ...
             50 50 50 99 99 99 99 50]' * 2;

 htrans2.BackgroundFillValue = [0.5 0.5 0.75];
 htrans2.ROIInputPort = true;
 htrans2.ROIShape = 'Polygon ROI';
 transimg2 = step(htrans2,img2,tfMat,polyROI);
 imshow(img2);
 figure;imshow(transimg2);

draw circles on an object

I = imread('coins.png');
figure, imshow(I);
position = [96 146 31;236 173 26];
label = [5 10];
RGB = insertObjectAnnotation(I, 'circle', position, label,'Color', {'cyan', 'yellow'}, 'TextColor', 'black');
figure, imshow(RGB), title('Annotated coins');

morphology operations top hat and bottom hat

 I = im2single(imread('rice.png'));
 htop = vision.MorphologicalTopHat('Neighborhood',strel('disk', 12));

% Improve contrast of output image
   hc = vision.ContrastAdjuster;  J = step(htop,I);
  J = step(hc,J);
  figure;

 subplot(1,2,1),imshow(I); title('Original image');
 subplot(1,2,2),imshow(J);
 title('Top-hat filtered image');




I = im2single(b);
 hbot = vision.MorphologicalBottomHat('Neighborhood',strel('disk', 5));
 J = step(hbot,I);
 figure;
 subplot(1,2,1),imshow(I); title('Original image');
 subplot(1,2,2),imshow(J);
 title('Bottom-hat filtered image'); 

Draw a shape over an object

I = imread('board.tif');
figure, imshow(I);
label_str = cell(3,1);
conf_val = [85.212 98.76 78.342]; 
for ii=1:3
    label_str{ii} = ['Confidence: ' num2str(conf_val(ii),'%0.2f') '%'];
end
position = [23 373 60 66;35 185 77 81;77 107 59 26];
RGB = insertObjectAnnotation(I, 'rectangle', position, label_str,'TextBoxOpacity', 0.9, 'FontSize', 18);
figure, imshow(RGB), title('Annotated chips');

morphological operations in matlab

hcsc = vision.ColorSpaceConverter;
 hcsc.Conversion = 'RGB to intensity';
 hautothresh = vision.Autothresholder;
 hdilate = vision.MorphologicalDilate('Neighborhood', ones(5,5));
 x1 = step(hcsc, a);
 x2 = step(hautothresh, x1);
 y = step(hdilate, x2);
 figure;
 subplot(3,1,1),imshow(a); title('Original image');
 subplot(3,1,2),imshow(x2); title('Thresholded Image');
 subplot(3,1,3),imshow(y); title('Dilated Image');