IPMV

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

IPMV

PROJECT
AIM:-TO DETECT RED COLOR FROM AN IMAGE USING
MATLAB SOFTWARE

SOFTWARE REQUIRED:-MATLAB2019a

THEORY:-

Digital Image Representation:-


An image may be defined as a two-dimensional function fxy
(;), where x and y are spatial (plane) coordinates, and the
amplitude of f at any pair of coordinates is called the intensity
of the image at that point. The term gray level is used often to
refer to the intensity of monochrome images. Color images
are formed by a combination of individual images. For
example, in the RGB color system a color image consists of
three individual monochrome images, referred to as the red
(R), green (G), and blue (B) primary (or component) images.
For this reason, many of the techniques developed for
monochrome images can be ex- tended to color images by
processing the three component images individually. An
image may be continuous with respect to the x- and y-
coordinates, and also in amplitude. Converting such an image
to digital form requires that the coordinates, as well as the
amplitude, be digitized. Digitizing the coordinate values is
called sampling; digitizing the amplitude values is called
quantization. Thus, when x, y, and the amplitude values of f
are all finite, discrete quantities, we call the image a digital
image.
Image Types:-

The toolbox supports four types of images:


o Gray-scale images
o Binary images
o Indexed images
o RGB images
Most monochrome image processing operations are carried
out using binary or gray-scale images.

Gray-scale Images:-
.A gray-scale image is a data matrix whose values represent
shades of gray. When the elements of a gray-scale image are
of class uint8 or uint16, they have integer values in the range
[0, 255] or [0, 65535], respectively. If the image is of class
double or single, the values are floating-point numbers.
Values of double and single gray-scale images normally are
scaled in the range [0, 1], although other ranges can be used
Binary Images:-
Binary images have a very specific meaning in MATLAB. A
binary image is a logical array of 0s and 1s. Thus, an array of
0s and 1s whose values are of data class, say, uint8, is not
considered a binary image in MATLAB. A numeric array is
converted to binary using function logical. Thus, if A is a
numeric array consisting of 0s and 1s, we create a logical
array B using the statement
B = logical(A)
If A contains elements other than 0s and 1s, the logical
function converts all nonzero quantities to logical 1s and all
entries with value 0 to logical 0s. Using relational and logical
operators (see Section 2.10.2) also results in logical arrays.
To test if an array is of class logical we use the islogical
function:
islogical(C)
If C is a logical array, this function returns a 1. Otherwise it
returns a 0. Logical arrays can be converted to numeric arrays
using the class conversion functions.
Indexed Images:-
An indexed image consists of a data matrix, X, and a
colormap matrix, map. map is an m-by-3 array of class double
containing floating-point values in the range [0, 1]. Each row
of map specifies the red, green, and blue components of a
single color. An indexed image uses “direct mapping” of pixel
values to colormap values. The color of each image pixel is
determined by using the corresponding value of X as an index
into map. Values of X therefore must be integers. The value 1
points to the first row in map, the value 2 points to the second
row, and so on.
RGB (Truecolor) Images:-
An RGB image, sometimes referred to as a truecolor image, is
stored as an m-by-n-by-3 data array that defines red, green,
and blue color components for each individual pixel. RGB
images do not use a palette. The color of each pixel is
determined by the combination of the red, green, and blue
intensities stored in each color plane at the pixel’s location.
Graphics file formats store RGB images as 24-bit images,
where the red, green, and blue components are 8 bits each.
This yields a potential of 16 million colors. The precision with
which a real-life image can be replicated has led to the
nickname “truecolor image.”

SIMULATION OF IMAGE:-

Task 1 - Simulate Model on Host Computer

In this task, you will simulate color detection model and


observe results for the images from workspace.
1. Open the Color Detection on Images Model
2. Load and display a sample color image into the
MATLAB® workspace by executing the following command
in the MATLAB command window:
Data=imread(‘office_3.jpg”);
imshow(RGB_image)

3. In the model, double-click on the Reference Color block to


open the block mask.

4. In the block mask, set the Constant value field to the


vector [ 58, 68, 117 ] to detect the color purple in the image.
The vector value represents the red, green, and blue
components of the color for detection (e.g. [R, G, B] ).

5. In the model, click the Run button on the toolbar to run the
simulation

6. (Optionally) You can modify the value of the Reference


Color block and run the simulation again to isolate different
color regions from the image.

Task 2 - Run Model on Device

In this task, you will create a new model to use the Video
Display block and deploy the model algorithm to the device
1. Open a new Simulink model and copy the contents of the
first model into the new model.

2. In the new model, remove the MATLAB Function


image_display block.
From the Simulink Library Browser, add the Android Video
Display block to the model. A pre-configured model is
included for your convenience.

4. Connect your configured Android device to your host


computer. If you have not previously configured your device,
run androidhwsetup from the MATLAB command prompt to
configure your Android device first.
5. In your Simulink model, click Simulation > Model
Configuration Parameters to open Configuration
Parameters dialog.

6. Select the Hardware Implementation pane and set


the Hardware board to the Android Device you are using.

7. In your Simulink model, click the Deploy to


Hardware button on the toolbar.

Task 3 - Run Model on Device with Real-time Video:-


In this task, you will update the previous model to use frames
captured from your Android device camera allowing for color
detection in real-time images.

1. Using the model created in Task 2, remove the Constant


blocks labelled R_inp, G_inp, and B_inp.

2. From the Simulink Library Browser, add the


Android Camera block to the model.

3. Connect the R, G, and B ports of the Camera block to the


R_in, G_in, and B_in ports of the Color Detection block,
respectively. A pre-configured model is included for your
convenience.
4. In your Simulink model, click the Deploy to Hardware
button on the tool bar.

5. The camera images with the masked colors appear on your


device.

Other Things to Try:-

 Change the detection algorithm by modifying the threshold


values used in the Color Detection subsystem.
Color detection in an image:-

Steps: read an image -> convert it to grey(0-255) -> subtract


the desired color from it -> convert it to black and white
image with a threshold(acc. to the color and intensity)

o a=imread(‘d.jpg’);
o figure(1),title(‘original image’),imshow(a);
o b=rgb2gray(a);
o red = imsubtract(a(:,:,1), b)%1 for red,2 for green ,3 for
blue
o red =medfilt2(red, [4 4]); ]); % Filter out the noise by
using median filter

o red = im2bw(red,0.17); % Convert the image into binary


image with the red objects as white
o red1= bwareaopen(red,300); % removes from a binary
image all connected components (red) that have fewer
than 300 pixels, producing another binary image, red1.
o figure(3),title(‘red color’),imshow(red1);

Hardware Information of your camera:-


o hwInfo = imaqhwinfo(‘winvideo’)
o DeviceInfo
o device1 = hwInfo.DeviceInfo(1)
Live Streaming:-
o v=videoinput(‘winvideo’,1,’YUY2_320x240′); % 1 for
laptop camera and 2 for webcam , YUY2 is according to
your device info
o preview(v);

Setting colour of an video:-


o v=videoinput(‘winvideo’,1,’YUY2_320x240′); %
Acquire input video stream
o set(v,’ReturnedColorSpace’,’gray’);
o preview(v);

To click an image:-

o vid=videoinput(‘winvideo’,1,’YUY2_320x240′);
o im=getsnapshot(vid);
o imshow(im);
o im=ycbcr2rgb(im); % the image snapped is usually in
ycbcr
o imshow(im);
o imwrite(im,’myimage.jpg’);

Looping Technique:-
o vid=videoinput(‘winvideo’);
o for i= 1:5
o set(vid,’ReturnedColorSpace’,’rgb’);
o snapshot=getsnapshot(vid);
o imshow(snapshot);
o end

Simply detecting a color in live streaming:-

v= videoinput(‘winvideo’,1,’YUY2_320x240′);
set(v,’ReturnedColorSpace’,’rgb’);
set(v,’TriggerRepeat’,Inf);
figure;
set(gcf,’doublebuffer’,’on’); % Double buffering is the
process of drawing to an off-screen pixel buffer and then
blitting the buffer contents to the screen once the drawing is
complete.

start(v)
while(v.FramesAcquired<=500)%run time =500/fps
data = getsnapshot(v);
r= imsubtract(data(:,:,1),rgb2gray(data));
r = medfilt2(r, [4 4]);
red = im2bw(r,0.17);
R = sum(red(:));
subplot(1,2,1);imshow(data);
subplot(1,2,2);imshow(red);
if(R > 0) % display red if red color is being detected
disp(‘RED’);
else
disp(‘NONE’);
end
end
stop(v);

To detect a color and find its centroid(using


RegionProps):-
vid=videoinput(‘winvideo’,1,’YUY2_320x240′);
set(vid,’ReturnedColorSpace’,’rgb’);
set(vid,’TriggerRepeat’,Inf);
figure;
set(gcf,’doublebuffer’,’on’);
start(vid)
while(vid.FramesAcquired<=500)
data=getdata(vid,1);
hold on
f=imsubtract(data(:,:,3),rgb2gray(data));%for blue
f=medfilt2(f,[4 4]); %Filter out the noise by using
median filter

f=im2bw(f,0.27); % Convert the image into binary image with


the red objects as white, have to change acc. To the intensity

f=bwareaopen(f,300);
bw=bwlabel(f,8);
stats=regionprops(bw,’BoundingBox’,’Centroid’);%refer
matlab help to know more about regionprops
imshow(data)
hold on
for object=1:length(stats) % Write the corresponding
centroids
bb=stats(object).BoundingBox;
bc=stats(object).Centroid;
rectangle(‘Position’,bb,’EdgeColor’,’r’,’LineWidth’,2)
plot(bc(1),bc(2))
a=text(bc(1)+15,bc(2),strcat(‘X:’,num2str(round(bc(1))),’Y’,n
um2str(round(bc(2)))));%x and y coordinate of centroid
set(a,’fontName’,’Arial’,’FontWeight’,’bold’,’FontSize’,12,’
Color’,’yellow’);
end
% Clearing Memory
flushdata(vid);

end

stop(vid)
hold off
CODE:-

data = imread('office_3.jpg');
diff_im = imsubtract(data(:,:,1), rgb2gray(data));

%Use a median filter to filter out noise

diff_im = medfilt2(diff_im, [3 3]);


diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);

stats = regionprops(bw, 'BoundingBox', 'Centroid');

% Display the image

imshow(data)
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;

rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')

a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))),


‘Y:’ , num2str(round(bc(2)))));

set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12,


'Color', 'black');
end

hold off
CONCLUSION:-
With the help of image processing toolbox in the
MATLAB, the program had been made which can detect red,
blue, green, magenta, yellow, cyan colors. Also, the colored
object is being enclosed inside a bounded region along with
the centroid of that region.

REFERENCES:-

1.https://ch.mathworks.com/help/supportpkg/android/ref/color
-detection.html.
2. http://www.ucrjiit.com/tutorials/?p=27&i=1.
3. https://www.ijcaonline.org
4. https://www.theengineeringprojects.com
5. https://www.instructables.com

You might also like