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.
Top Comments