


Choosing Optimal HSV Boundaries for Color Detection with cv::inRange in OpenCV
In image processing tasks, it's often necessary to detect objects based on their color. For this purpose, the cv::inRange function is commonly used in OpenCV to identify pixels within a specified HSV color range. However, selecting the appropriate HSV boundaries can be challenging, especially when different applications utilize varying HSV scales and color formats.
Problem:
Consider the scenario of detecting the orange lid on a coffee can image. Using a gimp tool, the HSV value at the lid's center was found to be (22, 59, 100). However, applying the HSV range (18, 40, 90) - (27, 255, 255) resulted in unsatisfactory detection results.
Solution 1: Adjust HSV Scale
To resolve this issue, it's crucial to be aware that different applications use different HSV scales. In this case, gimp uses the H: 0-360, S: 0-100, V: 0-100 scale, while OpenCV employs H: 0-179, S: 0-255, V: 0-255. For the hue value (22) obtained from gimp, it's necessary to take half of it (11) and adjust the range accordingly. This translates to a new HSV range of (5, 50, 50) - (15, 255, 255).
Solution 2: Convert to BGR Format
Additionally, it's important to consider that OpenCV uses the BGR color format, not RGB. Therefore, in the Python code, the cv::CV_RGB2HSV conversion should be replaced with cv::CV_BGR2HSV.
By implementing these modifications, the detection algorithm should yield improved results. While minor false detections may still occur, the largest contour should correspond to the lid.
Improved Python Code with OpenCV 2:
import cv2 in_image = 'kaffee.png' out_image = 'kaffee_out.png' out_image_thr = 'kaffee_thr.png' ORANGE_MIN = np.array([5, 50, 50], np.uint8) ORANGE_MAX = np.array([15, 255, 255], np.uint8) def test1(): frame = cv2.imread(in_image) frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) frame_threshed = cv2.inRange(frameHSV, ORANGE_MIN, ORANGE_MAX) cv2.imwrite(out_image_thr, frame_threshed) if __name__ == '__main__': test1()
Enhanced Python Code with OpenCV 4:
import cv2 import numpy as np in_image = 'kaffee.png' out_image = 'kaffee_out.png' out_image_thr = 'kaffee_thr.png' ORANGE_MIN = np.array([5, 50, 50], np.uint8) ORANGE_MAX = np.array([15, 255, 255], np.uint8) def test1(): frame = cv2.imread(in_image) frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) frame_threshed = cv2.inRange(frameHSV, ORANGE_MIN, ORANGE_MAX) cv2.imwrite(out_image_thr, frame_threshed) if __name__ == '__main__': test1()
Using these updated codes, it is possible to accurately detect the orange lid on the coffee can image.
The above is the detailed content of How to Effectively Choose Optimal HSV Boundaries for Color Detection in OpenCV using `cv::inRange`?. For more information, please follow other related articles on the PHP Chinese website!

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
