OpenCV in Python
Table of Contents
import cv2
img = cv2.imread('./data_files/lena_color_512.tif', 1)
cv2.imshow('Lena',img)
cv2.waitKey(0)
cv2.destroyWindow('Lena')
import matplotlib.pyplot as plt
%matplotlib inline
# Program to load a color image in gray scale and to display using matplotlib
img = cv2.imread('./data_files/lena_color_512.tif', 0)
plt.imshow(img,cmap = 'gray')
plt.title('Lena')
plt.xticks([])
plt.yticks([])
plt.show()
import numpy as np
image = np.zeros((200,200,3), np.uint8)
cv2.line(image,(0,199),(199,0),(0,0,255),2)
cv2.rectangle(image,(20,20),(60,60),(255,0,0),1)
cv2.circle(image,(80,80),10,(0,255,0),-1)
cv2.ellipse(image,(99,99),(40,20),0,0,360,(128,128,128),-1)
points = np.array([[100,5],[125,30],[175,20],[185,10]], np.int32)
points = points.reshape((-1,1,2))
cv2.polylines(image,[points],True,(255,255,0))
cv2.putText(image,'Test',(80,180),cv2.FONT_HERSHEY_DUPLEX,1, (255,0,255))
cv2.imshow('Shapes',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def empty(z):
pass
# Create a black background
image = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('Palette')
# create trackbars for colors and associate those with Pallete
cv2.createTrackbar('B','Palette',0,255,empty)
cv2.createTrackbar('G','Palette',0,255,empty)
cv2.createTrackbar('R','Palette',0,255,empty)
while(True):
cv2.imshow('Palette',image)
if cv2.waitKey(1) == 27:
break
# fetch the color value
blue = cv2.getTrackbarPos('B','Palette')
green = cv2.getTrackbarPos('G','Palette')
red = cv2.getTrackbarPos('R','Palette')
image[:] = [blue,green,red]
cv2.destroyWindow('Pallete')
# initialize the camera
cam = cv2.VideoCapture(0)
ret, image = cam.read()
if ret:
cv2.imshow('SnapshotTest',image)
cv2.waitKey(0)
cv2.destroyWindow('SnapshotTest')
cam.release()
cam = cv2.VideoCapture(0)
print("Default Resolution is " + str(int(cam.get(3))) + "x" + str(int(cam.get(4))))
w=320
h=240
cam.set(3,w)
cam.set(4,h)
print("Now resolution is set to " + str(w) + "x" + str(h))
while(True):
# Capture frame-by-frame
ret, frame = cam.read()
# Display the resulting frame
cv2.imshow('Video Test',frame)
# Wait for Escape Key
if cv2.waitKey(1) == 27 :
break
# When everything done, release the capture
cam.release()
cv2.destroyAllWindows()
VideoWriter
function is changed in new version of OpenCV.
cam = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'WMV2')
output = cv2.VideoWriter('D:/VideoStream.avi',fourcc,40.0,(640,480))
while (cam.isOpened()):
ret, frame = cam.read()
if ret == True:
output.write(frame)
cv2.imshow('VideoStream', frame )
if cv2.waitKey(1) == 27 :
break
else:
break
cam.release()
output.release()
cv2.destroyAllWindows()
img1 = cv2.imread('./data_files/lena_color_512.tif',1)
img2 = cv2.imread('./data_files/mandril_color.tif',1)
cv2.imshow('Lena',img1)
cv2.waitKey(0)
cv2.imshow('Mandril',img2)
cv2.waitKey(0)
cv2.imshow('Addition',cv2.add(img1,img2))
cv2.waitKey(0)
cv2.imshow('Lena-Mandril',cv2.subtract(img1,img2))
cv2.waitKey(0)
cv2.imshow('Mandril-Lena',cv2.subtract(img2,img1))
cv2.waitKey(0)
cv2.destroyAllWindows()
|
|
|
|