OpenCV
Use this skill when the user wants local computer-vision work with OpenCV, especially:
- image filtering, thresholding, morphology, contours, or edge detection
- resize, crop, rotate, perspective transform, or format conversion
- camera or video frame read/write with
cv2.VideoCapture or cv2.VideoWriter
- classical vision pipelines that should run locally in Python
Prefer this skill for deterministic image processing. If the task is mainly semantic understanding, captioning, or open-ended visual reasoning, consider an image-understanding expert instead of forcing OpenCV.
Workflow
- Confirm the real task type first: image enhancement, segmentation, contour extraction, geometry transform, or video I/O.
- Start with the smallest reproducible pipeline and save intermediate outputs when debugging.
- Use Python and import OpenCV as
cv2 as cv unless the surrounding code clearly uses a different style.
- For image-processing tasks, prefer a simple pipeline such as read -> colorspace convert -> threshold or filter -> morphology or contours -> save result.
- For video tasks, check
cap.isOpened(), check ret on every frame, and always release capture and writer objects.
- If parameter choices are unclear, expose them as function arguments instead of hard-coding many magic numbers.
- When the exact API behavior matters, read the relevant skill reference file before coding.
Reference Map
Read these skill-local references on demand instead of expanding the skill with long examples:
- Installation and package choice:
references/install.md
- Image-processing basics:
references/image-basics.md
- Thresholding and contours:
references/threshold-and-contours.md
- Video capture and writing:
references/video-io.md
If a request goes beyond these references, keep the implementation conservative and prefer simple, well-known OpenCV APIs over speculative or heavyweight patterns.
Guardrails
- OpenCV images are usually BGR, not RGB. Convert explicitly when mixing with PIL or matplotlib.
cv.threshold() and many related operations assume grayscale input; do not skip the colorspace conversion step.
- Contour detection works best on binary images, and the foreground should usually be white on black.
- For server or CI environments, prefer
opencv-python-headless or opencv-contrib-python-headless.
- Install exactly one OpenCV PyPI variant per environment unless there is a very specific reason not to.
- Do not drop in heavyweight DNN examples unless the user actually needs them and the model files are available locally.
- When debugging a broken pipeline, save intermediate files rather than guessing which stage failed.
Minimal Patterns
Read and threshold an image
import cv2 as cv
image = cv.imread(input_path, cv.IMREAD_COLOR)
if image is None:
raise FileNotFoundError(input_path)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
_, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv.imwrite(output_path, binary)
Find outer contours
import cv2 as cv
image = cv.imread(input_path, cv.IMREAD_GRAYSCALE)
if image is None:
raise FileNotFoundError(input_path)
_, binary = cv.threshold(image, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
Safe video loop
import cv2 as cv
cap = cv.VideoCapture(input_path)
if not cap.isOpened():
raise RuntimeError(f"Cannot open video: {input_path}")
while True:
ret, frame = cap.read()
if not ret:
break
# process frame here
cap.release()
1---2name: opencv3description: Use OpenCV for Python image processing, contour analysis, thresholding, filtering, geometric transforms, and basic video I/O. Trigger when the user asks to process images or video with cv2/OpenCV, or when a task clearly fits classical computer vision instead of a remote VLM.4---56# OpenCV78Use this skill when the user wants local computer-vision work with OpenCV, especially:910- image filtering, thresholding, morphology, contours, or edge detection11- resize, crop, rotate, perspective transform, or format conversion12- camera or video frame read/write with `cv2.VideoCapture` or `cv2.VideoWriter`13- classical vision pipelines that should run locally in Python1415Prefer this skill for deterministic image processing. If the task is mainly semantic understanding, captioning, or open-ended visual reasoning, consider an image-understanding expert instead of forcing OpenCV.1617## Workflow18191. Confirm the real task type first: image enhancement, segmentation, contour extraction, geometry transform, or video I/O.202. Start with the smallest reproducible pipeline and save intermediate outputs when debugging.213. Use Python and import OpenCV as `cv2 as cv` unless the surrounding code clearly uses a different style.224. For image-processing tasks, prefer a simple pipeline such as read -> colorspace convert -> threshold or filter -> morphology or contours -> save result.235. For video tasks, check `cap.isOpened()`, check `ret` on every frame, and always release capture and writer objects.246. If parameter choices are unclear, expose them as function arguments instead of hard-coding many magic numbers.257. When the exact API behavior matters, read the relevant skill reference file before coding.2627## Reference Map2829Read these skill-local references on demand instead of expanding the skill with long examples:3031- Installation and package choice:32 `references/install.md`33- Image-processing basics:34 `references/image-basics.md`35- Thresholding and contours:36 `references/threshold-and-contours.md`37- Video capture and writing:38 `references/video-io.md`3940If a request goes beyond these references, keep the implementation conservative and prefer simple, well-known OpenCV APIs over speculative or heavyweight patterns.4142## Guardrails4344- OpenCV images are usually BGR, not RGB. Convert explicitly when mixing with PIL or matplotlib.45- `cv.threshold()` and many related operations assume grayscale input; do not skip the colorspace conversion step.46- Contour detection works best on binary images, and the foreground should usually be white on black.47- For server or CI environments, prefer `opencv-python-headless` or `opencv-contrib-python-headless`.48- Install exactly one OpenCV PyPI variant per environment unless there is a very specific reason not to.49- Do not drop in heavyweight DNN examples unless the user actually needs them and the model files are available locally.50- When debugging a broken pipeline, save intermediate files rather than guessing which stage failed.5152## Minimal Patterns5354### Read and threshold an image5556```python57import cv2 as cv5859image = cv.imread(input_path, cv.IMREAD_COLOR)60if image is None:61 raise FileNotFoundError(input_path)6263gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)64_, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)65cv.imwrite(output_path, binary)66```6768### Find outer contours6970```python71import cv2 as cv7273image = cv.imread(input_path, cv.IMREAD_GRAYSCALE)74if image is None:75 raise FileNotFoundError(input_path)7677_, binary = cv.threshold(image, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)78contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)79```8081### Safe video loop8283```python84import cv2 as cv8586cap = cv.VideoCapture(input_path)87if not cap.isOpened():88 raise RuntimeError(f"Cannot open video: {input_path}")8990while True:91 ret, frame = cap.read()92 if not ret:93 break94 # process frame here9596cap.release()97```