Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Color Space #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions BGR2HSV/BGR_to_HSV .ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2 as cv\n",
"import numpy as np\n",
"cap = cv.VideoCapture(0)\n",
"while(1):\n",
" # Take each frame\n",
" _, frame = cap.read()\n",
" # Convert BGR to HSV\n",
" hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)\n",
" # define range of blue color in HSV\n",
" lower_blue = np.array([110,50,50])\n",
" upper_blue = np.array([130,255,255])\n",
" # Threshold the HSV image to get only blue colors\n",
" mask = cv.inRange(hsv, lower_blue, upper_blue)\n",
" # Bitwise-AND mask and original image\n",
" res = cv.bitwise_and(frame,frame, mask= mask)\n",
" cv.imshow('frame',frame)\n",
" cv.imshow('mask',mask)\n",
" cv.imshow('res',res)\n",
" k = cv.waitKey(5) & 0xFF\n",
" if k == 27:\n",
" break\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 30 additions & 0 deletions BGR2HSV/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

<![endif]-->

# Change Color Space


## Goal[](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html#goal "Permalink to this headline")

> - In this tutorial, you will learn how to convert images from one color-space to another, like BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) Gray, BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) HSV etc.
> - In addition to that, we will create an application which extracts a colored object in a video
> - You will learn following functions : **cv2.cvtColor()**, **cv2.inRange()** etc.
## # Changing Color space


There are more than 150 color-space conversion methods available in OpenCV. But we will look into only two which are most widely used ones, BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) Gray and BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) HSV.

For color conversion, we use the function `cv2.cvtColor(input_image, flag)` where `flag` determines the type of conversion.

For BGR ![\rightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/a9c4c6156d25f42923975ce449aadad9848ed7dc.png) Gray conversion we use the flags `cv2.COLOR_BGR2GRAY`. Similarly for BGR ![\rightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/a9c4c6156d25f42923975ce449aadad9848ed7dc.png) HSV, we use the flag `cv2.COLOR_BGR2HSV`. To get other flags, just run following commands in your Python terminal
>>> import cv2
>>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
>>> print flags

## How to Use

1. Run the notebook file
(webcam starts)
2. Three new windows opens showcasing the outputs


62 changes: 62 additions & 0 deletions ChangeColorSpace/BGR2HSV/BGR_to_HSV .ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2 as cv\n",
"import numpy as np\n",
"cap = cv.VideoCapture(0)\n",
"while(1):\n",
" # Take each frame\n",
" _, frame = cap.read()\n",
" # Convert BGR to HSV\n",
" hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)\n",
" # define range of blue color in HSV\n",
" lower_blue = np.array([110,50,50])\n",
" upper_blue = np.array([130,255,255])\n",
" # Threshold the HSV image to get only blue colors\n",
" mask = cv.inRange(hsv, lower_blue, upper_blue)\n",
" # Bitwise-AND mask and original image\n",
" res = cv.bitwise_and(frame,frame, mask= mask)\n",
" cv.imshow('frame',frame)\n",
" cv.imshow('mask',mask)\n",
" cv.imshow('res',res)\n",
" k = cv.waitKey(5) & 0xFF\n",
" if k == 27:\n",
" break\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 30 additions & 0 deletions ChangeColorSpace/BGR2HSV/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

<![endif]-->

# Change Color Space


## Goal[](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html#goal "Permalink to this headline")

> - In this tutorial, you will learn how to convert images from one color-space to another, like BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) Gray, BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) HSV etc.
> - In addition to that, we will create an application which extracts a colored object in a video
> - You will learn following functions : **cv2.cvtColor()**, **cv2.inRange()** etc.
## # Changing Color space


There are more than 150 color-space conversion methods available in OpenCV. But we will look into only two which are most widely used ones, BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) Gray and BGR ![\leftrightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/41a61df92c33a32be9fd6375536739eca63f43ab.png) HSV.

For color conversion, we use the function `cv2.cvtColor(input_image, flag)` where `flag` determines the type of conversion.

For BGR ![\rightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/a9c4c6156d25f42923975ce449aadad9848ed7dc.png) Gray conversion we use the flags `cv2.COLOR_BGR2GRAY`. Similarly for BGR ![\rightarrow](https://opencv-python-tutroals.readthedocs.io/en/latest/_images/math/a9c4c6156d25f42923975ce449aadad9848ed7dc.png) HSV, we use the flag `cv2.COLOR_BGR2HSV`. To get other flags, just run following commands in your Python terminal
>>> import cv2
>>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
>>> print flags

## How to Use

1. Run the notebook file
(webcam starts)
2. Three new windows opens showcasing the outputs