This part is not a project, but some of the stuffs I really enjoy doing and coding.

RGB Extraction: Image extraction can be done with two methods, directly from the RGB color model or by converting to the HSV model.

In order to extract only the yellow pixels of the flower in the RGB color model, we will have to consider an approximate value of Red, Green, and Blue color. Then threshold the 3 values.

However, in the HSV model we only need to threshold the intensity value to extract the flower part. Hue and Saturation can be neglected.

```clc;clear;
x=imread('flower.jpg');
red=x(:,:,1);
y=1*(red<195);
green=x(:,:,2);
y=1*(green<167);
y2=0.5*(green>167);
blue=x(:,:,3);
y=1*(blue<62);

%----------------
% Convert to HSV
%----------------
hope=rgb2hsv(x);

hue=hope(:,:,1);
h=0.5*(hue<0.1);
h2=1*(hue>0.1);
sat=hope(:,:,2);
h=0.5*(sat<0.90);
h2=1*(sat>0.90);
In=hope(:,:,3);
h=0.5*(In>0.50);
yy=x.*y.*y2;
hopeless=(h.*h2.*x);
subplot(1,3,1);imshow(x);title('Original Image');
subplot(1,3,2);imshow(yy);title('Extracting with RGB');
subplot(1,3,3);imshow(hopeless);title('Extracting with HSV');```

Labeling Objects:

There are more than 3 objects in this image and we want to label each object with connected pixels greater than 200 pixels. 1) We need to create a structure element of 3×3 in size to perform erosion and dilate later.

2) Create a set of blank matrices with the same size as the input image. [In this case I created 5 blank matrices called label.]

3) From the input image, we need to find the first pixel position with value ‘1’ and start dilation from that point. This will extract the first object.

4) Check if the object contains at least 200 pixels or not. Then replace the value from ‘1’ to ‘2’ as in labeling our object number and store this data in the first blank matrix.

5) In the original image, set all values that are already labeled to ‘0’. Then find the next starting point for our next object.

6) Repeat the iteration until the original image is replaced with ‘0’ value or until it is empty.

```clc;clear;clc;
x=imread('chickbone.tif');
y=x>0.78*255;

y_compare = y;

se=[1,1,1;1,1,1;1,1,1];
% we can also use se=ones(3,3);

[A B]=find(y==1);
label=zeros(size(x,1),size(x,2),5); %create 5 blank matrices
n=2;
count=0;
j=1;
subplot(2,3,1);imshow(y);title('Original Image');

while(~isempty(y_compare))

%-------------------------------
% initializes the starting point
%-------------------------------
blank=zeros(size(x,1),size(x,2));

blank(A(j),B(j))=1;
se=double(se);
yy = imdilate(blank,se)&y_compare;

while(~isequal(yy,blank))

blank=yy;
se=logical(se);
yy=imdilate(blank,se)&y_compare;
count++;

end
[M N] = find((yy)==1);
area=numel(N);
y_compare(M,N)=0;
label=zeros(size(x,1),size(x,2));

if area>200
%-----------------
% label the pixel
%-----------------
label(M,N)=(yy(M,N)==1)*n;
subplot(2,3,n);imshow(label==n);
title(['Object label ==',num2str(n)]);

n++;
endif
count=0;
[A B]=find(y_compare==1);
end```

Filters:

Noise is one of the main problem when dealing with images. Using the right filtering method to cancel out noise can help achieve a sharper image output.

Filtering can be categorized in two domains:

1) Spatial Domain

2) Frequency Domain In a spatial domain, a mask, usually 3×3 or 5×5, is created and applied to the first pixel in the original image until the last pixel. Because in the spatial domain, the center value of a mask is essential, we need to implement padding algorithm around the border of the input image. Example of Spatial Filtering with mean and averaging filter: The code below compares the compilation time of two filtering methods, by composing your own function and by using the existing function in Octave library.

In the diy filter output, I used

0   1    0

1   -4   1

0   1    0

(mean filter) as the filtering mask.

```function filter_image= filterfunc(Img,filter)
x = imread(Img);
top =floor(size(filter,1)/2);
left = floor(size(filter,2)/2);
xpad= impad(x,top,left,'zeros');

[M,N]=size(x);
y=size(x);

r=top;

tic();
for m=r+1:M+r
for n=r+1:N+r
window_shift=xpad(m+(-r:r),n+(-r:r)).*filter;

y(m-r,n-r)=sum(window_shift(:),1);
endfor
endfor
toc()
%window_shift
tic();
if(r==1)
f=fspecial('average',3);
elseif (r==2)
f=fspecial('average',5);
endif

y2=imfilter(x,f);
toc()
z=y2.-y;

imshow(y/255);title('diy filter');
figure;imshow(y2);title('Octave filter');

endfunction```

Filtering in Frequency Domain:

Definition

• Techniques are based on modifying the spectral transform of an image
• Transform the image to its frequency representation
• Perform image processing
• Compute inverse transform back to the spatial domain
• High frequencies correspond to pixel values that change rapidly across the image (e.g. text, texture, leaves, etc.)
• Strong low frequency components correspond to large scale features in the image (e.g. a single, homogenous object that dominates the image)

Technique Gaussian filtering in frequency domain:
[using fspecial function, cut off frequency= 100]

```clc; clear;
x=imread('cman.bmp');
x=double(x);
[M N]=size(x);
fast_x=fft2(x);
shift_x=fftshift(fast_x);
d0=100;
gauss=fspecial('gaussian',size(x,1),d0);
filt=shift_x.*gauss;
img=(ifftshift(filt));
img_gauss=abs(ifft2(img));
min_ig=min(img_gauss(:));
max_ig=max(img_gauss(:));
gg=(real(img_gauss).-min_ig)./(max_ig-min_ig);
imshow(x/255);figure;
imshow(gg);title(['Gaussian Lowpass Filter Do= ',num2str(d0)]);```

Ideal Lowpass filtering in frequency domain:
[using meshgrid, cut off frequency= 67%  of maximum distance]:

```clc; clear;

x=imread('cman.bmp');
x=double(x);
[M, N]=size(x);
r=(M-1)/2;
c=(N-1)/2;

[u,v]= meshgrid(-r:r,-c:c);
dist=sqrt((u-r)^2+(v-c)^2);

d0=0.67*max(dist(:));

LP=(dist<=d0);
img=fftshift(fft2(x));
lowp=ones(size(img,1),size(img,2));
lowp=img.*LP;
Ideal_LP=abs(ifft2(ifftshift((lowp)/255)));
imshow(Ideal_LP);title(['Ideal Lowpass filter D0= ',num2str(d0)]);```