Created
June 14, 2022 13:24
-
-
Save elbruno/bdbdea6088fa841db9ff1183381813f8 to your computer and use it in GitHub Desktop.
cameratograyscaleresaltredandblue.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright (c) 2022 | |
| # Author : Bruno Capuano | |
| # Create Time : 2022 June | |
| # Change Log : | |
| # - Open the camera feed | |
| # - Convert the camera feed to Grayscale | |
| # - Resalt a specific colors >> RED and BLUE | |
| # | |
| # The MIT License (MIT) | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in | |
| # all copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| # THE SOFTWARE. | |
| import numpy as np | |
| import cv2 | |
| cap = cv2.VideoCapture(0) | |
| while True: | |
| # read image | |
| ret, img = cap.read() | |
| img = cv2.resize(img, (320, 240)) | |
| #convert the BGR image to HSV colour space | |
| hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
| #obtain the grayscale image of the original image | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| # set the bounds for the red and blue hue | |
| lower_red = np.array([25,37,40]) | |
| upper_red = np.array ([255, 255, 255]) | |
| # create a mask using the bounds set | |
| mask = cv2.inRange(hsv, lower_red, upper_red) | |
| # create an inverse of the mask | |
| mask_inv = cv2.bitwise_not(mask) | |
| # Filter only the red colour from the original image using the mask(foreground) | |
| res = cv2.bitwise_and(img, img, mask=mask) | |
| # Filter the regions containing colours other than red from the grayscale image(background) | |
| background = cv2.bitwise_and(gray, gray, mask = mask_inv) | |
| # convert the one channelled grayscale background to a three channelled image | |
| background = np.stack((background,)*3, axis=-1) | |
| # add the foreground and the background | |
| img_ca = cv2.add(res, background) | |
| cv2.imshow('@ElBruno - Office', img) | |
| cv2.imshow('@ElBruno - Captain America', img_ca) | |
| if cv2.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| # close camera | |
| cap.release() | |
| cv2.destroyAllWindows() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment