Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. filename = 'C:li_walk.avi';
  2. hVidReader = vision.VideoFileReader(filename, 'ImageColorSpace', 'RGB','VideoOutputDataType', 'single');
  3. hOpticalFlow = vision.OpticalFlow('OutputValue', 'Horizontal and vertical components in complex form', 'ReferenceFrameDelay', 3);
  4. hMean1 = vision.Mean;
  5. hMean2 = vision.Mean('RunningMean', true);
  6. hMedianFilt = vision.MedianFilter;
  7. hclose = vision.MorphologicalClose('Neighborhood', strel('line',5,45));
  8. hblob = vision.BlobAnalysis('CentroidOutputPort', false, 'AreaOutputPort', true, 'BoundingBoxOutputPort', true, 'OutputDataType', 'double','MinimumBlobArea', 250, 'MaximumBlobArea', 3600, 'MaximumCount', 80);
  9. herode = vision.MorphologicalErode('Neighborhood', strel('square',2));
  10. hshapeins1 = vision.ShapeInserter('BorderColor', 'Custom', 'CustomBorderColor', [0 1 0]);
  11. hshapeins2 = vision.ShapeInserter( 'Shape','Lines', 'BorderColor', 'Custom','CustomBorderColor', [255 255 0]);
  12. htextins = vision.TextInserter('Text', '%4d', 'Location', [1 1],'Color', [1 1 1], 'FontSize', 12);
  13. sz = get(0,'ScreenSize');
  14. pos = [20 sz(4)-300 200 200];
  15. hVideo1 = vision.VideoPlayer('Name','Original Video','Position',pos);
  16. pos(1) = pos(1)+220; % move the next viewer to the right
  17. hVideo2 = vision.VideoPlayer('Name','Motion Vector','Position',pos);
  18. pos(1) = pos(1)+220;
  19. hVideo3 = vision.VideoPlayer('Name','Thresholded Video','Position',pos);
  20. pos(1) = pos(1)+220;
  21. hVideo4 = vision.VideoPlayer('Name','Results','Position',pos);
  22. % Initialize variables used in plotting motion vectors.
  23. lineRow = 22;
  24. firstTime = true;
  25. motionVecGain = 20;
  26. borderOffset = 5;
  27. decimFactorRow = 5;
  28. decimFactorCol = 5;
  29. while ~isDone(hVidReader) % Stop when end of file is reached
  30. frame = step(hVidReader); % Read input video frame
  31. grayFrame = rgb2gray(frame);
  32. ofVectors = step(hOpticalFlow, grayFrame); % Estimate optical flow
  33. % The optical flow vectors are stored as complex numbers. Compute their
  34. % magnitude squared which will later be used for thresholding.
  35. y1 = ofVectors .* conj(ofVectors);
  36. % Compute the velocity threshold from the matrix of complex velocities.
  37. vel_th = 0.5 * step(hMean2, step(hMean1, y1));
  38. % Threshold the image and then filter it to remove speckle noise.
  39. segmentedObjects = step(hMedianFilt, y1 >= vel_th);
  40. % Thin-out the parts of the road and fill holes in the blobs.
  41. segmentedObjects = step(hclose, step(herode, segmentedObjects));
  42. % Estimate the area and bounding box of the blobs.
  43. [area, bbox] = step(hblob, segmentedObjects);
  44. % Select boxes inside ROI (below white line).
  45. Idx = bbox(:,1) > lineRow;
  46. % Based on blob sizes, filter out objects which can not be cars.
  47. % When the ratio between the area of the blob and the area of the
  48. % bounding box is above 0.4 (40%), classify it as a car.
  49. ratio = zeros(length(Idx), 1);
  50. ratio(Idx) = single(area(Idx,1))./single(bbox(Idx,3).*bbox(Idx,4));
  51. ratiob = ratio > 0.4;
  52. count = int32(sum(ratiob)); % Number of cars
  53. bbox(~ratiob, :) = int32(-1);
  54. % Draw bounding boxes around the tracked cars.
  55. y2 = step(hshapeins1, frame, bbox);
  56. % Display the number of cars tracked and a white line showing the ROI.
  57. y2(22:23,:,:) = 1; % The white line.
  58. y2(1:15,1:30,:) = 0; % Background for displaying count
  59. result = step(htextins, y2, count);
  60. % Generate coordinates for plotting motion vectors.
  61. if firstTime
  62. [R C] = size(ofVectors); % Height and width in pixels
  63. RV = borderOffset:decimFactorRow:(R-borderOffset);
  64. CV = borderOffset:decimFactorCol:(C-borderOffset);
  65. [Y X] = meshgrid(CV,RV);
  66. firstTime = false;
  67. sumu=0;
  68. sumv=0;
  69. end
  70.  
  71. grayFrame = rgb2gray(frame);
  72. [ra ca na] = size(grayFrame);
  73. ofVectors = step(hOpticalFlow, grayFrame); % Estimate optical flow
  74.  
  75. ua = real(ofVectors);
  76. ia = ofVectors - ua;
  77. va = ia/complex(0,1);
  78.  
  79.  
  80. sumu=ua+sumu;
  81. sumv=va+sumv;
  82. [xa ya]=meshgrid(1:1:ca,ra:-1:1);
  83.  
  84.  
  85. % Calculate and draw the motion vectors.
  86. tmp = ofVectors(RV,CV) .* motionVecGain;
  87. lines = [Y(:), X(:), Y(:) + real(tmp(:)), X(:) + imag(tmp(:))];
  88. motionVectors = step(hshapeins2, frame, lines);
  89. % Display the results
  90. step(hVideo1, frame); % Original video
  91. step(hVideo2, motionVectors); % Video with motion vectors
  92. step(hVideo3, segmentedObjects); % Thresholded video
  93. step(hVideo4, result); % Video with bounding boxes
  94.  
  95. quiver(xa,ya,sumu,sumv)
  96. end
  97. release(hVidReader);
  98.  
  99. ua = real(ofVectors);
  100. ia = ofVectors - ua;
  101. va = ia/complex(0,1);
  102.  
  103. ua = real(ofVectors);
  104.  
  105. ia = ofVectors - ua;
  106.  
  107. va = ia/complex(0,1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement