Skip to content

Commit

Permalink
adding openCV files to cv folder. organizing files into folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
bk521234 committed Jul 9, 2019
1 parent e950ed6 commit 8544ac8
Show file tree
Hide file tree
Showing 19 changed files with 474 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cv/adaptive_threshold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Python program to illustrate
# adaptive thresholding type on an image

# organizing imports
import os

import cv2
import numpy as np

FILE_DIR = os.path.dirname(__file__)

# path to input image is specified and
# image is loaded with imread command
image1 = cv2.imread(os.path.join(FILE_DIR, 'input_book_image.jpg'))

# cv2.cvtColor is applied over the
# image input with applied parameters
# to convert the image in grayscale
img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# applying different thresholding
# techniques on the input image
thresh1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 199, 5)

thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 199, 5)

# the window showing output images
# with the corresponding thresholding
# techniques applied to the input image
cv2.imshow('Adaptive Mean', thresh1)
cv2.imshow('Adaptive Gaussian', thresh2)


# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
37 changes: 37 additions & 0 deletions cv/car_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# OpenCV Python program to detect cars in video frame
# import libraries of python OpenCV
import cv2

# capture frames from a video
cap = cv2.VideoCapture('video.avi')

# Trained XML classifiers describes some features of some object we want to detect
car_cascade = cv2.CascadeClassifier('cars.xml')

# loop runs if capturing has been initialized.
while True:
# reads frames from a video
ret, frames = cap.read()

# convert to gray scale of each frames
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)


# Detects cars of different sizes in the input image
cars = car_cascade.detectMultiScale(gray, 1.1, 1)

# To draw a rectangle in each cars
for (x,y,w,h) in cars:
cv2.rectangle(frames,(x,y),(x+w,y+h),(0,0,255),2)

# Display frames in a window
cv2.imshow('video2', frames)

# Wait for Esc key to stop
if cv2.waitKey(33) == 27:
break

# De-allocate any associated memory usage
cv2.destroyAllWindows()


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions cv/otsu_thresholding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Python program to illustrate
# Otsu thresholding type on an image

# organizing imports
import os

import cv2
import numpy as np

FILE_PATH = os.path.dirname(__file__)
# path to input image is specified and
# image is loaded with imread command
for i in ['input1.jpg', 'input_book_image.jpg']:
image1 = cv2.imread(os.path.join(FILE_PATH, i))

# cv2.cvtColor is applied over the
# image input with applied parameters
# to convert the image in grayscale
img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# applying Otsu thresholding
# as an extra flag in binary
# thresholding
ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)

# the window showing output image
# with the corresponding thresholding
# techniques applied to the input image
cv2.imshow('Otsu Threshold', thresh1)

# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()


24 changes: 24 additions & 0 deletions cv/python_background_subtraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Python code for Background subtraction using OpenCV
import numpy as np
import cv2

cap = cv2.VideoCapture('/home/username/Downloads/people-walking.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()

while(1):
ret, frame = cap.read()

fgmask = fgbg.apply(frame)

cv2.imshow('fgmask', frame)
cv2.imshow('frame', fgmask)


k = cv2.waitKey(30) & 0xff
if k == 27:
break


cap.release()
cv2.destroyAllWindows()

File renamed without changes.
43 changes: 43 additions & 0 deletions cv/simple_thresholding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Python programe to illustrate
# simple thresholding type on an image

# organizing imports
import os

import cv2
import numpy as np

FILE_PATH = os.path.dirname(__file__)
# path to input image is specified and
# image is loaded with imread command
image1 = cv2.imread(os.path.join(FILE_PATH, 'input1.jpg'))

# cv2.cvtColor is applied over the
# image input with applied parameters
# to convert the image in grayscale
img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# applying different thresholding
# techniques on the input image
# all pixels value above 120 will
# be set to 255
ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 120, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO_INV)

# the window showing output images
# with the corresponding thresholding
# techniques applied to the input images
cv2.imshow('Binary Threshold', thresh1)
cv2.imshow('Binary Threshold Inverted', thresh2)
cv2.imshow('Truncated Threshold', thresh3)
cv2.imshow('Set to 0', thresh4)
cv2.imshow('Set to 0 Inverted', thresh5)

# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()


48 changes: 48 additions & 0 deletions cv/video_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Python program to illustrate
# saving an operated video

# organize imports
import numpy as np
import cv2

# This will return video from the first webcam on your computer.
cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

# loop runs if capturing has been initialized.
while(True):
# reads frames from a camera
# ret checks return at each frame
ret, frame = cap.read()

# Converts to HSV color space, OCV reads colors as BGR
# frame is converted to hsv
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# output the frame
out.write(hsv)

# The original input frame is shown in the window
cv2.imshow('Original', frame)

# The window showing the operated video stream
cv2.imshow('frame', hsv)


# Wait for 'a' key to stop the program
if cv2.waitKey(1) & 0xFF == ord('a'):
break

# Close the window / Release webcam
cap.release()

# After we release our webcam, we also release the output
out.release()

# De-allocate any associated memory usage
cv2.destroyAllWindows()


48 changes: 48 additions & 0 deletions cv/video_capture_grayscale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Python program to illustrate
# saving an operated video

# organize imports
import numpy as np
import cv2

# This will return video from the first webcam on your computer.
cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output_grayscale.avi', fourcc, 20.0, (640, 480))

# loop runs if capturing has been initialized.
while(True):
# reads frames from a camera
# ret checks return at each frame
ret, frame = cap.read()

# Converts to grayscale space, OCV reads colors as BGR
# frame is converted to gray
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# output the frame
out.write(gray)

# The original input frame is shown in the window
cv2.imshow('Original', frame)

# The window showing the operated video stream
cv2.imshow('frame', gray)


# Wait for 'a' key to stop the program
if cv2.waitKey(1) & 0xFF == ord('a'):
break

# Close the window / Release webcam
cap.release()

# After we release our webcam, we also release the out-out.release()
out.release()

# De-allocate any associated memory usage
cv2.destroyAllWindows()


55 changes: 55 additions & 0 deletions cv/video_edge_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# OpenCV program to perform Edge detection in real time
# import libraries of python OpenCV
# where its functionality resides
import cv2

# np is an alias pointing to numpy library
import numpy as np


# capture frames from a camera
cap = cv2.VideoCapture(0)


# loop runs if capturing has been initialized
while(1):

# reads frames from a camera
ret, frame = cap.read()

# converting BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of red color in HSV
lower_red = np.array([30,150,50])
upper_red = np.array([255,255,180])

# create a red HSV colour boundary and
# threshold HSV image
mask = cv2.inRange(hsv, lower_red, upper_red)

# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)

# Display an original image
cv2.imshow('Original',frame)

# finds edges in the input image image and
# marks them in the output map edges
edges = cv2.Canny(frame,100,200)

# Display edges in a frame
cv2.imshow('Edges',edges)

# Wait for Esc key to stop
k = cv2.waitKey(5) & 0xFF
if k == 27:
break


# Close the window
cap.release()

# De-allocate any associated memory usage
cv2.destroyAllWindows()

27 changes: 27 additions & 0 deletions time_series/autoregression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Autoregression example
from statsmodels.tsa.ar_model import AR
from random import random

import matplotlib.pyplot as plt

# contrived dataset
data = [x + random() for x in range(1,100)]

# fit the model
model = AR(data)

model_fit = model.fit()

# make predication
yhat = model_fit.predict(len(data), len(data))
print(data)
print(yhat)


fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
ax.plot(range(len(data)), data, 'o',label="data")
ax.plot(len(data)+ 1, yhat, 'P',label="Predicted")
plt.show()


16 changes: 16 additions & 0 deletions time_series/autoregressive_moving_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ARMA example
from statsmodels.tsa.arima_model import ARMA
from random import random

# contrived dataset
data = [random() for x in range(1,100)]

# fit model
model = ARMA(data, order=(2,1))
model_fit = model.fit(disp=False)

# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)


15 changes: 15 additions & 0 deletions time_series/moving_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Moving Average example
from statsmodels.tsa.arima_model import ARMA
from random import random

# contrived dataset
data = [x + random() for x in range(1, 100)]

# fit model
model = ARMA(data, order=(0,1))
model_fit = model.fit(disp=False)

# make prediction
yhat = model_fit.predict(len(data), len(data))
print(yhat)

Loading

0 comments on commit 8544ac8

Please sign in to comment.