element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Projects
  • Learn
  • Learning Center
  • STEM Academy
  • STEM Projects
  • More
  • Cancel
STEM Projects
Blog Virtual Eye System -- Post #6 -- Object detection (Color & Edge) and Tracking.
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: nikhil22
  • Date Created: 9 Sep 2015 8:59 PM Date Created
  • Views 494 views
  • Likes 3 likes
  • Comments 2 comments
  • object_detection
  • teachers_pet
  • virtual_eye_system
  • matlab_raspberry_pi
  • raspberrypi
  • teachers pet robotics design challenge
  • picam
Related
Recommended

Virtual Eye System -- Post #6 -- Object detection (Color & Edge) and Tracking.

nikhil22
nikhil22
9 Sep 2015

Hi folks!!

In the previous blog post, we've seen the basics of getting started with Pi cam like taking snapshots and videos among other things like changing various parameters of the Pi cam. In order to understand this post, please have a glance at my previous post.

In this post, I'll explain the concept of object detection by color and edge detection and then we'll look into tracking them.

 

Object detection and tracking (by color detection):

 

I would first like to explain an easier approach to object detection.....i.e. color detection. Color detection is the most effective means of object detection when you don't have a noisy background in the image and have an object whose color is well differentiated from the background. Let us first create handles for both raspberry pi and Pi cam.

 

clear rpi                                          % Clears variable rpi

clear cam

rpi = raspi();                                      

cam = cameraboard(rpi, 'Resolution', '640 x 480'); %Creates an object to point to the Pi cam with given resolution

img=snapshot(cam);

r = img(:,:,1);

g = img(:,:,2);

b = img(:,:,3);

Now that object cam is defined, you would be glad to hear that Matlab offers a quite short and understandable approach to color detection. Whenever you take a snapshot, Matlab stores the values of RGB components of the image in a matrix when can be manipulated accordingly. So only we use command imgsc function to view an image taken by cam. It stands for image scale and presents the image in view-able format.

 

img=snapshot(cam);

% EXTRACT THE RGB COMPONENTS FROM THE MATRIX img;

r = img(:,:,1);

g = img(:,:,2);

b = img(:,:,3);

Let us take an example of tracking a blue object. Set a threshold as a cutoff of blue's intensity.

 

readBlue= b - r/2 - g/2;

% THRESHOLD THE IMAGE

thresh = 40;

readThresh = readBlue > thresh; 

Now, we need to find the center of the object to track it.

 

%% Find center

 

[x, y] = find(readThresh);         %% Find all the coordinates of positive id of the tracking process.

if ~isempty(x) && ~isempty(y)

    xm = round(mean(x));          %% Find mean of all such coordinates and round them off.

    ym = round(mean(y));

  

    %% Create a red dot as a means of tracking it

 

    xx = max(1, xm-5):min(xm+5, size(readThresh, 1));

    yy = max(1, ym-5):min(ym+5, size(readThresh, 2));

    bwbw = zeros(size(readThresh), 'uint8');

    bwbw(xx, yy) = 255;

 

%% Create output image i.e. a red dot pointing center of the object

 

    img(:,:,1) = uint8(r + bwbw);

    img(:,:,2) = uint8(g - bwbw);

    img(:,:,3) = uint8(b - bwbw);

end

There you have a code to detect and track the color of your choice. Matlab has made color detection even simpler by providing this bit of code as function called trackball which accepts two arguments i.e. image and threshold. It can be called as follows..but it has predefined tracking setup of tracking green color and needs a modification if you want to track any other color.

 

[img, bw] = trackball(img, thresh);

 

Object detection by Edge detection:

 

Again, thanks to Matlab for making the process of edge detection very understandable and simple. After taking a snapshot, let us convert the color image to an equivalent gray scale image with normalized values (0.0 for black, 1.0 for white).

 

gray = (0.2989 * double(img(:,:,1)) + 0.5870 * double(img(:,:,2)) + 0.1140 * double(img(:,:,3)))/255;

We are using a Sobel filter for edge detection which accepts 2 arguments, the image and threshold value. Pass the normalized image and a threshold value.

 

edgeIm = sobel(gray, 0.7);

im3 = repmat(edgeIm, [1 1 3]); %replicate and tile the image

image(im3);

Let us see how to integrate servo with the tracking mechanism to make a Pan n tilt module for wider angle of vision of tracking in the next blog post.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 10 years ago +1
    Have you considered doing spectral analysis to assess plant health? You can do some analysis with the RGB images, but adding a NIR image would be very useful. DAB
  • nikhil22
    nikhil22 over 10 years ago in reply to DAB

    That a really nice idea.....I knew that NIR image helps when dealing with edge detection but didn't consider it initially. I will surely try it out.....
    Thanks for the idea....
    Nikhil.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 10 years ago

    Have you considered doing spectral analysis to assess plant health?

     

    You can do some analysis with the RGB images, but adding a NIR image would be very useful.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube