Color detection with Python OpenCV
Color Detection
https://github.com/PolyU-Robocon/PolyU_Workshop_OpenCV_2020
checking.py
1 | # Import OpenCV |
You should see something like this:
1 | --------------------------------------- |
Now you should be able to run the other files.
webcam.py
1 | # https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html |
cap.read()
returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.cv2.imshow(title,frame)
takes the title as first parameter then frame as second parameter.cap.release()
andcv2.destroyAllWindows()
are the methods to close video files or the capturing device, and destroy the window, which was created by theimshow
method.cv2.resize(frame, (width, height))
allows you to resize the video window.
imagelayers.py
The above image is not correct in OpenCV’s situation.
Why OpenCV store color in BGR?
The reason the early developers at OpenCV chose BGR color format is that back then BGR color format was popular among camera manufacturers and software providers. E.g. in Windows, when specifying color value using COLORREF they use the BGR format 0x00bbggrr.
BGR was a choice made for historical reasons and now we have to live with it.
1 | import numpy as np |
frame[column, row, channel]
R[:,:,0]
means all columns, all rows in the first channel (which is Blue) of the frameR
.
.copy()
is a method in Numpy. It will create a copy of the array you need.
colormasking.py
Why do we use the HSV colour space so often in vision and image processing?
The simple answer is that unlike RGB, HSV separates luma, or the image intensity, from chroma or the color information. This is very useful in many applications. For example, if you want to do histogram equalization of a color image, you probably want to do that only on the intensity component, and leave the color components alone. Otherwise you will get very strange colors.
In computer vision you often want to separate color components from intensity for various reasons, such as robustness to lighting changes, or removing shadows.
1 | # import the necessary packages |
Why HSV in OpenCV uses 0 - 179 for Hue?
Developers used
uchar
to store the value.
uchar
can only store -127 to 127 which means only 255 values can be stored.So they decided to just divide the Hue by 2.
np.array()
creates an array.cv2.cvtColor()
allows you to convert a color into another space.cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
from BGR to HSV
cv2.inRange(frame, lowerbound, upperbound)
- For color between lowerbound and upperbound, value become 255 (white)
- For color not in lowerbound and upperbound, value become 0 (black)
For Tuning the mask color we want, we can use :
- http://colorizer.org/
- or use the
colorthresholder.py
script in extra folder (More robust)
Color Tracking
1 | # import the necessary packages |
cv2.findContours()
returns (frame,contours,hierarchy).- Note for some versions of opencv might only return (contours,hierarchy).
cv2.RETR_EXTERNAL
retrieves only the extreme outer contours- You could also try with
cv2.RETR_LIST
,cv2.RETR_CCOMP
, orcv2.RETR_TREE
.
- You could also try with
cv2.CHAIN_APPROX_SIMPLE
means the algorithm we are using for getting contours.cv2.CHAIN_APPROX_SIMPLE
removes all redundant points and compresses the contour, thereby saving memory- You could also try with
cv2.CHAIN_APPROX_NONE
to find all the boundary points
- https://docs.opencv.org/master/dd/d49/tutorial_py_contour_features.html
cv2.circle(frame, center_coordinates, radius, color, thickness)
allows you to draw a circle in your window.cv2.circle(frame, (int(x), int(y)), int(radius),(0, 255, 255), 2)
Full Code
1 | # import the necessary packages |
What is GaussianBlur?
GuassianBlur is typically used to reduce image noise and reduce detail.
What is Dilation and Erosion, Opening and Closing?
TLDR:
- Dilation - Add pixels to the boundaries of objects in an image
- Erosion - Removes pixels at the boundaries of objects in an image
- Opening - Erosion then Dilation
- Closing - Dilation then Erosion
Note in OpenCV recognize WHITE as object itself.
So the effect of dilation and erosion might do the reverse of what you expect.
What is imutils
?
imutils
is A series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and both Python 2.7 and Python 3.https://github.com/jrosebr1/imutils/blob/master/imutils/convenience.py
Explaination of imutils.grab_contours()
:
1 | def grab_contours(cnts): |
Explaination of imutils.resize()
:
1 | def resize(image, width=None, height=None, inter=cv2.INTER_AREA): |