Analyzing objects in an image
Another main function of image processing is the analysis of image content (binary or other). When analyzing an image, usually we search for the presence of edges, corners, or circles inside it. Having this information at hand, we are in the position to detect shapes and locate specific objects in our images, or enhance selected parts of the image. This has a lot to do with the subject of ROI selection that we have discussed so far in this chapter. Let's start our image analysis techniques' overview with the most popular method, which is edge detection.
Detecting edges in an image
Edge detection is a process that typically transforms a grayscale image to a binary one, denoting all the pixels belonging to lines of different orientations with instances of 1. The edge detection process is widely used and has been tackled using a variety of techniques. MATLAB has an inherent function called edge
, which has incorporated most of the popular methods in an easily usable form.
To demonstrate the process, we will use an image with many lines, so that the usefulness of each algorithm is demonstrated. The one chosen is holiday_image2.bmp
. To get a better idea of all the different methods supported by edge
, you can type help edge in the command line. These methods are Sobel
, Prewitt
, Roberts
, Laplacian of Gaussian
(LoG
), zero-cross
and Canny
. Let's use them all for our images and display the results. In order for the edge detection to perform faster, we will first resize our image by a scale of 0.5:
>> img = imread('holiday_image2.bmp'); >> img = imresize(img,0.5); >> BW1 = edge(img,'sobel'); >> BW2 = edge(img,'prewitt'); >> BW3 = edge(img,'roberts'); >> BW4 = edge(img,'log'); >> BW5 = edge(img,'zerocross'); >> BW6 = edge(img,'canny'); >> subplot(3,3,2),imshow(img),title('Original Image') >> subplot(3,3,4),imshow(BW1),title('Sobel result') >> subplot(3,3,5),imshow(BW2),title('Prewitt result') >> subplot(3,3,6),imshow(BW3),title('Roberts result') >> subplot(3,3,7),imshow(BW4),title('LoG result') >> subplot(3,3,8),imshow(BW5),title('Zerocross result') >> subplot(3,3,9),imshow(BW6),title('Canny result')
As you can see, the Canny edge detection method provides a much denser result in this case. Its main advantage seems to be the detection of edges in regions with low brightness values. The Sobel
, Prewitt
and Roberts
methods appear to be the weakest, producing sparse results, detecting fewer lines than the other three methods.
The process of edge detection can be used in several ways. The two most popular applications are those of object segmentation in an image and of image enhancement. Especially in object segmentation, it is usual for edge detection to be combined with other methods such as corner detection.
Detecting corners in an image
Corner detection is another useful tool used for object segmentation, as well as in image registration algorithms (matching points in one image with corresponding points in another version of the image that has been transformed, or that has been taken at a different point of time). A general definition of a corner in the image processing domain is the intersection of two edges. As such, it is very closely connected to edge detection. MATLAB offers corner detection through the corner
function, which in turn, supports two different algorithms; Harris corner detection method and Shi &Tomasi's minimum eigenvalue method.
Let's use the same image as before to demonstrate their usage. This time, as corner
returns the coordinates of the detected corners, we will use plot
to visualize them. In order for them to be projected on the original image, we will also use the command hold on
before we call plot
. Finally, we will use red circles for the corners detected by the Harris method and blue asterisks for the ones detected by the minimum eigenvalue method. To better demonstrate the results, we will crop a part of the image. Let's get to work:
>> img = imread('holiday_image2.bmp'); >> img = imresize(img,0.5); >> img = imcrop(img); >> C1 = corner(img); >> C2 = corner(img, 'MinimumEigenvalue'); >> figure, imshow(img) % Display original image >> hold on % Hold on the figure >> plot(C1(:,1), C1(:,2), 'ro'); % Overlay the corners from Harris >> plot(C2(:,1), C2(:,2), 'b*'); % Overlay the corners from Shi
The results show that the Shi-Tomasi method produces more results using the default settings and that most of the results coincide. However, they are not completely identical, so we must be careful when choosing which technique to use.
Detecting circles in an image
The last of the popular image processing methods we will visit, which is widely used in everyday tasks, is circle detection. Circles are a very descriptive feature of many objects that often need to be detected in an image. To name a few, circle detection can be used to localize eyes, stars, balls, coins, tires, lights, and so on. MATLAB's inherent function for circle detection is function imfindcircles
. It can be used with several possible inputs, with the only ones necessary being the input image and the radius (or range of radii) in pixels of the circle we want to detect. Its output may be only the centers of the detected circles, or it can also contain the radii of the respective circles and the power of each circle.
We'll demonstrate its usage, using a rather funny example. The cat depicted in the photograph we will use, has a funny little piece of black fur under its nose, which looks like a human moustache. Let's try to ignore it for a while and attempt to automatically detect the cat's eyes, using imfindcircles
. We will use the range of radii from 20
to 50
pixels. The result will be visualized using the MATLAB command viscircles
, which is designed for such use:
>> img = imread('cat.jpg'); >> img = rgb2gray(img); >> imshow(img)
Here is our cat. Funny little guy, right?
Let's try to locate its eyes. As we said, we will use a range of radii for our algorithm, spanning from 20
to 50
pixels (if we want to be more accurate, we can use some of the tools MATLAB provides for measuring distances in an image, for example, imdistline
):
>> [centers,radii,metric] =imfindcircles(c,[20 50])
Now, we can observe our results produced from the last line of code which got displayed on screen, since we didn't use our semi-colon operator. A close look at the results reveals that our call to the function located three circles instead of two:
Such counterintuitive results are very common when using an automated technique for object detection. This is natural, of course, since the algorithm only looks at the circularity of region, without the use of any additional knowledge to assist in refining the results. Therefore, refining the results in such tasks is usually a human's job.
Taking a closer look, we see that two of the results have almost identical radii (28.6 to 28.7 pixels). These also have a significantly higher circularity metric than the third one, so they are definitely more likely to be the cat's eyes. One last clue that could confirm our choice is the positions of the centers of the detected circles. Unfortunately, the positions in this example do not greatly vary (similar rows and close columns), so we cannot say for sure. An assumption that can be made, though, is that the first two circles detected are the eyes and the third is a smaller symmetric region between them (its center's column coordinate is near the midpoint of the two other centers), above the cat's nose. Let's see if we are right. We will visualize the two first circles using viscircles
and then we will place a blue asterisk on the center of the third circle:
>> viscircles(centers(1:2,:), radii(1:2,:)); >> hold on; >> plot(centers(3,1), centers(3,2),'b*');
Spot on! The circle detector indeed located the two eyes, as well as a highly symmetrical area on the top of the cat's nose. Our rationale in the post-processing phase was correct and we chose the proper centers of the cat's eyes. Not bad for a procedure based on an automatic tool, right? We will get to put this method to the test again later in the book.
Pop quiz – object analysis pros and cons
Q1. MATLAB provides several functions that implement complex object analysis tasks. Can you answer whether the following properties are true?
- The most dense and detailed edge detection result is achieved using function
edge
with theSobel
method. - The
corner
function provided by MATLAB provides two different methods for performing corner detection. - The circle detection performed by
imfindcircles
can work either with one radius, or with a range of radii.