0% found this document useful (0 votes)
24 views

Assignment No.1: Que1) Create A Program For Image Read & Show

The document contains 16 assignments completed by Pratik Ajay Nipane, a student with Roll No. 228 in B.voc II class. Each assignment involves writing an OpenCV Python program to perform image processing/computer vision tasks such as reading and displaying an image, saving an image, converting an image to grayscale, drawing lines and rectangles on an image, video recording and saving, adding borders to an image, finding contours in an image, and performing histogram equalization. For each assignment, the program code and output are provided.

Uploaded by

pratik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Assignment No.1: Que1) Create A Program For Image Read & Show

The document contains 16 assignments completed by Pratik Ajay Nipane, a student with Roll No. 228 in B.voc II class. Each assignment involves writing an OpenCV Python program to perform image processing/computer vision tasks such as reading and displaying an image, saving an image, converting an image to grayscale, drawing lines and rectangles on an image, video recording and saving, adding borders to an image, finding contours in an image, and performing histogram equalization. For each assignment, the program code and output are provided.

Uploaded by

pratik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Name : Pratik Ajay Nipane.

Roll No : 228
Class : B.voc II

ASSIGNMENT NO.1
Que1)Create a program for image read & show.
Program :

import cv2
img = cv2.imread("P:\car_img.jpg")
img1 = cv2.resize(img,(600,400))
cv2.imshow('first_img',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

1
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.2
Que2)Create a program for image read & write (save).

Program :

import cv2 as cv
#read image
img=cv.imread("P:\car_img.jpg",0)
#save image
result=cv.imwrite("P:\car_img.jpg",img)
if result==True:
print("File saved successfully")
else:
print("Error in saving file")
cv.waitKey(0)
cv.destroyAllWindows()

Output :

2
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.3
Que3) Create simple image convert to gray scale.
Program :

import cv2
img=cv2.imread("P:\car_img.jpg",0)
img1 = cv2.resize(img,(700,400))
cv2.imshow('first image',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

3
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.4
Que4)Create a program for drawing function used five parameter list.
Program :

import cv2
img=cv2.imread("P:\car_img.jpg",1)
img1 = cv2.resize(img,(600,400))
img=cv2.line(img1,(0,0),(400,600),(154,92,454),10)
cv2.imshow('first image',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

4
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO .5
5)Create drawing function draw the rectangle parameter.
Program :

import cv2
img=cv2.imread("P:\car_img.jpg",1)
img1 = cv2.resize(img,(600,400))
img1=cv2.rectangle(img1,(400,200),(200,100),(154,92,454),8)
cv2.imshow('second imgae',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

5
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO .6
6)Create basic screen recording using open cv.
Program :
import cv2

cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)

print("check===",cap.isOpened())

fourcc=cv2.VideoWriter_fourcc(*"XVID")

output=cv2.VideoWriter("file\output.avi",fourcc,20.0,(640,480),0)

while(cap.isOpened()):

ret,frame=cap.read()

if ret==True:

gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

#frame=cv2.flip(gray,0)

output.write(gray)

cv2.imshow("grayframe",gray)

cv2.imshow("colorframe",frame)

if cv2.waitKey(1)&0xFF==ord('q'):

break

else:

break

cap.release()

output.release()

cv2.destroyAllWindows()

Output:

6
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

7
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.7
7)Making border of an image using open cv.
Program :

import cv2
image=cv2.imread("P:\car_img.jpg",1)
cv2.resize(image,(1000,1000))
image_bordered=cv2.copyMakeBorder(src=image,top=30,bottom=30,left=30,right=30,borde
rType=cv2.BORDER_CONSTANT)
image_bordered=cv2.resize(image_bordered,(900,600))
cv2.imshow('Bordered image',image_bordered)
cv2.waitKey(0)

Output :

8
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.8
8)Create read write & save video using open cv.

Program :
import cv2
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
print("check===",cap.isOpened())
fourcc=cv2.VideoWriter_fourcc(*"XVID")
output=cv2.VideoWriter("frame\output.avi",fourcc,20.0,(640,480),0)
while(cap.isOpened()):
ret,frame=cap.read()
if ret==True:
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#frame=cv2.flip(gray,0)
output.write(gray)
cv2.imshow("grayframe",gray)
cv2.imshow("colorframe",frame)
if cv2.waitKey(1)&0xFF==ord('q'):
break
else:
break
cap.release()
output.release()
cv2.destroyAllWindows()

9
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

Output :

10
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.9
9)Find & draw counters in specific image.
Program :

import cv2 as cv
#load an image
image=cv.imread('circle.png')
LUV= cv.cvtColor(image,cv.COLOR_BGR2LUV)
edges=cv.Canny(LUV,10,100)
contours,hierarchy=cv.findContours(edges,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_N
ONE)
cv.drawContours(image,contours,-1,(0,255,0),3)
cv.drawContours(image,contours,-1,(0,255,0),3)
cv.imshow('Contours',image)
cv.waitKey(0)

Output :

11
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

ASSIGNMENT NO.10

10) Create histogram equalization.


Program :
import numpy as np
import cv2
from matplotlib import pyplot as plt

img = np.zeros((200,200), np.uint8)


cv2.rectangle(img, (0, 100), (200, 200), (255), -1)
cv2.rectangle(img, (0, 50), (50, 100), (127), -1)
#It accept parameters like ([img],[channel],mask,[histsize],range[0-255]).
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.plot(hist)
plt.show()
cv2.imshow("res",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

img = cv2.imread("fea.jpeg")
img = cv2.resize(img,(500,650))
b, g, r = cv2.split(img)
cv2.imshow("img", img)
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)

plt.hist(b.ravel(), 256, [0, 256])


plt.hist(g.ravel(), 256, [0, 256])

12
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

plt.hist(r.ravel(), 256, [0, 256])


plt.title("ColorFull Image")
plt.show()

#cal
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.title("ColorFull Image")
plt.plot(hist)
plt.show()

img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([img_gray], [0], None, [256], [0, 256])
plt.plot(hist)
plt.title("Gray Image")
plt.show()

equ = cv2.equalizeHist(img_gray)
res = np.hstack((img_gray,equ)) #stacking images side-by-side
cv2.imshow("equ",res)
hist1 = cv2.calcHist([equ], [0], None, [256], [0, 256])
plt.plot(hist1)
plt.title("Equalization")
plt.show()

clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))


cl1 = clahe.apply(img_gray)
cv2.imshow('clahe',cl1)
hist2 = cv2.calcHist([cl1], [0], None, [256], [0, 256])

13
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

plt.plot(hist2)
plt.title("CLAHE")
plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

14
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

15
Name : Pratik Ajay Nipane. Roll No : 228
Class : B.voc II

16

You might also like