# taking up images part 2

Posted by SUYOG PATIL on 2:39 PM in , , ,
Suppose the image at hand is the following (again it’s not a default image) and we need to extract the red region.

So, we use the same function with the main conditional statement changed as
function bw=red(im)
[m,n,t]=size(im);
bw=zeros(m,n);
for i=1:m
for j=1:n
if(im(i,j,1)>150 && im(i,j,2)<50 && im(i,j,3)<50)
%Specifying range for red bw(i,j)=1;
end
end
end
You can define more specific range as per your requirement. Now when we see the output image, it looks like

*Tip:
Removing Noise To get an idea of what range of RGB values you must define, use imtool() function (or data cursor) with the original file, move the cursor over the region which you need to extract and note down the RGB values. Expand/ Contract the range as per your requirement by trying several times. This is actually how the calibration is done.

Removing noise

As you can see, the binary image obtained above has quite a lot of noise. You need to smoothen the edges, remove the tiny dots scattered here and there so that at last you have some countable number of objects to work upon. There are several functions available in MATLAB to remove noise in an Image. Some of them are (‘bw’ is the above binary image): imclose(): Performs morphological closing operation. It fills in the gaps between two objects and smoothens the edges. The degree and type of smoothening and joining depend on the structuring element, i.e., the basic shape which is used to perform the operation. The structuring element can be a disk, a diamond, a line, etc. To create a structuring element, use strel() function.
For example,
>> se=strel('disk',5); % a disk of radius 5
>>bw=imclose(bw,se); %Perform the closing operation on original image itself
>>imshow(bw);

imfill(): Remove holes from the image Holes are background pixels surrounded by foreground (image) pixels. >>bw=imfill(bw,'holes');
>>imshow(bw);

Now you need to remove the spatters. For that you can use the function imopen(). imopen(): Morphologically open the image.
>>se=strel('disk',2);
>>bw=imopen(bw,se);
>>imshow(bw);

*
• The purpose of these functions might not be clear at fist. Hence, it is strongly recommended that you explore these functions yourself by trying out on different images and have a look at these functions in MATLAB Help section also.
Note:
• The sequence of performing these operations is very significant, as the subsequent operation is performed on the converted image and not the original image. The sequence I have adopted is NOT necessary, and you must try yourself which sequence suits your requirement.
• The size and shape of structuring element is also to be determined experimentally, so that the number of objects you get in the binary image is same as what you require. This final image obtained in this example is still not workable, and you can try yourself to get a consolidated image.
• There are many other functions available for noise removal like imerode(), imdilate(), etc. Please refer to MATLAB Help section for details.
• An object is defined as a connected region in a binary image.

The use of making a binary file: In most of problem statements of robotics based on image processing, we are required to find the centroid, area, and no. of objects of a particular color. MATLAB has in-built functions for these tasks which operate on binary images only. Hence we create binary images corresponding to different colors.
THANKS FOR READING,We will study how to access camera through Matlab next time...

|