You are on page 1of 21

A-01

RAJ RAVI UPRETI

Experiment 1:

AIM:
Perform basic operation, arithmetic operation and logical operations on digital image.

Code1:
subplot(3,3,1);
img = imread("Lenna_(test_image).png");
imshow(img)
title('original Image')

subplot(3,3,2);
bw = im2bw(img);
imshow(bw);
title('Black and White Image');

subplot(3,3,3);
g = rgb2gray(img);
imshow(g);
title('Gray Scale Image');

subplot(3,3,4);
I = img + 70;
imshow(I);
title('Brightness');

OUTPUT1:
A-01
RAJ RAVI UPRETI

Code2:
subplot(2,2,1);
A = imread("naruto.jpg")
imshow(A)
title("Image 1")

subplot(2,2,2);
B = imread("sasuke.jpg");
imshow(B);
title('Image 2');

subplot(2,2,3);
C = imresize(A,[400 400]);
D = imresize(B, [400 400]);
A-01
RAJ RAVI UPRETI

imgadd = imadd(C,D);
imshow(imgadd);
title('Image Addition');

subplot(2,2,4);
C = imresize(A,[400 400]);
D = imresize(B,[400 400]);
imgsub = imsubtract(C,D);
imshow(imgsub);
title("Image Subtraction");

OUTPUT2:

Code3:

subplot(2,2,1);
a = [0 1 0 ; 1 0 1 ; 0 1 0];
A-01
RAJ RAVI UPRETI

imshow(a);
title('IMAGE');
subplot(2,2,2);
b = imcomplement(a);
imshow(b);
title('Compliment');

subplot(2,2,3);
c = a & b;
imshow(c);
title('AND OPERATION');

subplot(2,2,4);
d = a | b;
imshow(d);
title('OR OPERATION');

OUTPUT3:
A-01
RAJ RAVI UPRETI

Experiment 2:

AIM:
Histogram and Histogram equalization.

Code:
img1 = imread("lena_dark.png");
subplot(2,2,1);
imshow(img1);
title("Original Image");

subplot(2,2,2);
imhist(img1);
title("Histogram");
A-01
RAJ RAVI UPRETI

subplot(2,2,3);
img2 = histeq(img1);
imshow(img2);
title("Histogram Eq img");

subplot(2,2,4);
imhist(img2);
title("Histogram Eq graph");

OUTPUT:
A-01
RAJ RAVI UPRETI

Experiment 3:

AIM:
Perform edge detection on digital image using sobel, prewitt, robert and laplacian operator.
Code:
a = imread('cameraman.png');

subplot(3,3,1);
imshow(a);
title('Orignal Image');

sobelvr = edge(a,'sobel','both');
subplot(3,3,2);
imshow(sobelvr,[]);
title('sobel - all edges');

subplot(3,3,3);
cannyedg=edge(a,'canny');
imshow(cannyedg,[]);
title('Canny image');

robertsedg = edge(a, 'roberts');


subplot(3,3,4);
imshow(robertsedg , []);
title('roberts - alledges');
A-01
RAJ RAVI UPRETI

sedg = edge(a,'prewitt');
subplot(3,3,5);
imshow(sedg, []);
title('prewitt - alledges');

f = fspecial('laplacian');
lapedg = imfilter(a, f, 'symmetric');
subplot(3,3,6);
imshow(lapedg, []);
title('laplacien - filter');

OUTPUT:
A-01
RAJ RAVI UPRETI

Experiment 4:

AIM:
Perform low pass filter, high pass filter and median filter.

Code:
clc;
clear all;
close all;

subplot(3,3,1);
a=imread("lena1.jpg");
imshow(a);
title('original Image');

subplot(3,3,2);
j=imnoise(a,'salt & pepper',0.2);
imshow(j);
title('Noise Image');

subplot(3,3,3);
k=medfilt2(j,[3 3]);
imshow(k);
title('Output of 3x3 Median Filter')
A-01
RAJ RAVI UPRETI

subplot(3,3,4)
k1=medfilt2(j,[7 7]);
imshow(k1);
title('Out put of 7x7 Median filter')

subplot(3,3,5);
avgfilt=[1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
];
avgfiltmask = avgfilt/sum(avgfilt);
convimage=conv2(double(a),double(avgfiltmask));
imshow(convimage,[]);
title('Avg filter output')

subplot(3,3,6)
h=[1 -2 1;-1 3 -1;1 -2 1];
hpt3=conv2(double(a),double(h));
imshow(hpt3/100);
title('O/p of HPF')

subplot(3,3,7)
A-01
RAJ RAVI UPRETI

h1=[-1 -1 -1;-1 9 -1;-1 -1 -1];


hpt32=conv2(double(a),double(h1));
imshow(hpt32/100);
title('O/p of sharpening');

OUTPUT:

Experiment 5:

AIM:

To perform Morphological Operation- Erosion and dilation.

Code:

clc;
close all;
clear all;
org_img = imread('Square_combinationmark_black-
232f684716f1474ebd37744e2e386748.jpg');
A-01
RAJ RAVI UPRETI

bw_d = im2bw(rgb2gray(org_img));
se = strel('square', 18);
dilate_img = imdilate(bw_d, se);

erode_img = imerode(dilate_img, se);

subplot(2,2,1)
imshow(bw_d)
title('Dilate i/p')

subplot(2,2,2)
imshow(dilate_img)
title('Dilated Image')

subplot(2,2,3)
imshow(dilate_img)
title('Erode i/p')

subplot(2,2,4)
imshow(erode_img)
title('Eroded Image')

OUTPUT:
A-01
RAJ RAVI UPRETI

Experiment 6:

AIM:

Perform and observe various types of noise in image using matlab.

Code:
clc
close all
mygrayimg = imread('lena1.jpg');
mygrayimg = imresize(mygrayimg,[256 256], 'bilinear');
subplot(2,3,1);
imshow(mygrayimg);
title('Original Image');

salt = imnoise(mygrayimg, 'salt & pepper', 0.2);


subplot(2,3,2);
imshow(salt);
title("Salt and Pepper image");

gau = imnoise(mygrayimg,"gaussian",0,0.01);
subplot(2,3,3);
imshow(gau);
title("Gaussian image");

subplot(2,3,4);
[x y]= meshgrid(1:256,1:256);
mysinusoidalnoise =15*sin(2*pi/14*x+2*pi/14*y);
mynoiseimg1 = double(mygrayimg)+ mysinusoidalnoise;
imshow(mynoiseimg1,[]);
title("Genrated Sinusoidal Noise");

OUTPUT:
A-01
RAJ RAVI UPRETI

Experiment 7:

Code:

clc;

close all;

clear all;

a=imread('grey-flower-dawn-oconnor.jpg');

a=rgb2gray(a);

subplot(3,3,1);

imshow(a);title('Original image');

level=0.3;

subplot(3,3,2);

segimage1=im2bw(a, level);

imshow(segimage1); title('Simple thresholding at 0.3');

subplot(3,3,3);

imshow(a>153); title('Simple thresholding at 0.6');


A-01
RAJ RAVI UPRETI

b=imread('blurimage.jpg');

subplot(3,3,4);

imshow(b); title('Badly illuminated image');

level= graythresh(b);

subplot(3,3,5);

segimage=im2bw(b, level);

imshow(segimage); title('Otsu-Segmentation');

b=imread('blurimage.jpg');

b=rgb2gray(b);

avgfilt=ones(13,13);

adaptfiltmask= avgfilt/sum(avgfilt);

im= imfilter(b,adaptfiltmask, 'replicate');

im1 = medfilt2(b, [20 20]);

thresh = im+18;

adaptthreshimg = b-thresh;

subplot(3,3,6);

imshow(adaptthershimg > 0); title('Adpative threshold = 18');

thresh1 = im1 + 2;

adaptthreshimg = b-thresh1;

subplot(3,3,7);

imshow(adaptthreshing > 0); title('Adpative threshold = 2');

Output:
A-01
RAJ RAVI UPRETI

Experiment 8:

Code:

clc;

close all;

clear all;

a=imread('Lena_(test_image).png');

a=rgb2gray(a);

figure;

subplot(3,3,1);

imshow(a);

title('original Image');

[numr numc] =size(a);

nc=2;

ci=kmeans(double(a(:)),nc);

B=zeros(size(ci));
A-01
RAJ RAVI UPRETI

B(ci==2) =255;

C= im2bw(B);

subplot(3,3,2);

S=reshape(C,numr,numc);

imshow(S);

title("With Two clusters");

nc =3;

ci = kmeans(double(a(:)),nc);

B=zeros(size(ci));

B(ci==3)=255;

C=im2bw(B);

subplot(3,3,3)

S=reshape(C,numr,numc);

imshow(S);

title("With three clusters")

Output:

Experiment 9&10:

Code:
import cv2
import face_recognition
import os
A-01
RAJ RAVI UPRETI

import numpy as np
from datetime import datetime

path = 'D:\work\endance\images'
images = []
classNames = []
mylist = os.listdir(path)
for cl in mylist:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
classNames.append(os.path.splitext(cl)[0])

def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encoded_face = face_recognition.face_encodings(img)[0]
encodeList.append(encoded_face)
return encodeList
encoded_face_train = findEncodings(images)

def markAttendance(name):
with open('Attendance.csv','r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now = datetime.now()
time = now.strftime('%I:%M:%S:%p')
date = now.strftime('%d-%B-%Y')
f.writelines(f'\n{name}, {time}, {date}')

cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faces_in_frame = face_recognition.face_locations(imgS)
encoded_faces = face_recognition.face_encodings(imgS, faces_in_frame)
for encode_face, faceloc in zip(encoded_faces,faces_in_frame):
matches = face_recognition.compare_faces(encoded_face_train, encode_face)
faceDist = face_recognition.face_distance(encoded_face_train, encode_face)
matchIndex = np.argmin(faceDist)
print(matchIndex)
if matches[matchIndex]:
name = classNames[matchIndex].upper().lower()
y1,x2,y2,x1 = faceloc
y1, x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.rectangle(img, (x1,y2-35),(x2,y2), (0,255,0), cv2.FILLED)
A-01
RAJ RAVI UPRETI

cv2.putText(img,name, (x1+6,y2-5),
cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
markAttendance(name)
cv2.imshow('webcam', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

Output:
A-01
RAJ RAVI UPRETI
A-01
RAJ RAVI UPRETI

You might also like