From bc211229b44e197c69240b17e03c2e254eaf197c Mon Sep 17 00:00:00 2001 From: Ashutoshh Date: Mon, 25 Jun 2018 11:55:47 +0530 Subject: [PATCH] Add files via upload motion detection with opencv and color extraction --- camera_red_capture.py | 28 ++++++++++++++++++ create_own_black.py | 10 +++++++ motion_detection.py | 69 +++++++++++++++++++++++++++++++++++++++++++ red_color_extract.py | 22 ++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 camera_red_capture.py create mode 100644 create_own_black.py create mode 100644 motion_detection.py create mode 100644 red_color_extract.py diff --git a/camera_red_capture.py b/camera_red_capture.py new file mode 100644 index 0000000..5da09c7 --- /dev/null +++ b/camera_red_capture.py @@ -0,0 +1,28 @@ +#!/usr/bin/python + +import cv2,time + +# reading image +cap=cv2.VideoCapture(0) + +while cap.isOpened(): + + # taking frames + status,frame=cap.read() + # extracting only red color + onlyred=cv2.inRange(frame,(0,0,0),(40,40,255)) + print onlyred + onlygreen=cv2.inRange(frame,(0,0,0),(40,255,0)) + #cv2.imshow('onlygreen',onlygreen) + + if cv2.waitKey(1) & 0xFF == ord('q') : + break + + +cv2.destroyAllWindows() +cap.release() + + + + + diff --git a/create_own_black.py b/create_own_black.py new file mode 100644 index 0000000..6cbbfca --- /dev/null +++ b/create_own_black.py @@ -0,0 +1,10 @@ +#!/usr/bin/python + +import numpy as np +import cv2 + +img=np.zeros((512,512)) + +cv2.imshow('balc',img) +cv2.waitKey(0) +cv2.destroyAllWindows() diff --git a/motion_detection.py b/motion_detection.py new file mode 100644 index 0000000..e17fedd --- /dev/null +++ b/motion_detection.py @@ -0,0 +1,69 @@ +#!/usr/bin/python + +import cv2 +# we are taking 3 frames +def imgdiff(x,y,z): + img1=cv2.absdiff(x,y) + img2=cv2.absdiff(y,z) + com_diff=cv2.bitwise_and(img1,img2) + return com_diff + +cap=cv2.VideoCapture(0) +# taking 3 consistant frames +frame1=cap.read()[1] +frame2=cap.read()[1] +frame3=cap.read()[1] + +# converting into grayscale +gray1=cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) +gray2=cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) +gray3=cv2.cvtColor(frame3,cv2.COLOR_BGR2GRAY) + +while cap.isOpened(): + # passing arg to above function + img_diff=imgdiff(gray1,gray2,gray3) + # showing diff + cv2.imshow('diffimg',img_diff) + # capturing new frames + status,frame=cap.read() + gray1=gray2 + gray2=gray3 + gray3=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) + + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + +cv2.destroyAllWindows() +cap.release() + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/red_color_extract.py b/red_color_extract.py new file mode 100644 index 0000000..8cc5107 --- /dev/null +++ b/red_color_extract.py @@ -0,0 +1,22 @@ +#!/usr/bin/python + +import cv2,time + +# reading image +img=cv2.imread('redhat.jpg') + +# checking shape +print img.shape +time.sleep(5) +# printing data +print img + +# extracting only red color +onlyred=cv2.inRange(img,(0,0,0),(40,40,255)) +cv2.imshow('original',img) +cv2.imshow('onlyred',onlyred) + +cv2.waitKey(0) + +cv2.destroyAllWindows() +