You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于YCbCr色彩空间的肤色分割代码报错:数组尺寸不匹配

Fixing Array Shape Mismatch in YCbCr Skin Segmentation Code

Hey there, let’s work through that array shape mismatch error you’re hitting with your skin segmentation code. I’ve run into similar headaches before, so let’s break down the likely culprits and fix them step by step.

Common Causes & Targeted Fixes

1. You’re Using the Wrong Color Space Name in OpenCV

A super easy mistake to overlook: OpenCV uses YCrCb (not YCbCr) as the official color space identifier. If you’re converting with cv2.COLOR_BGR2YCbCr instead of cv2.COLOR_BGR2YCrCb, your channel order gets scrambled, leading to shape or threshold mismatches right off the bat.

2. Threshold vs. Image Channel Mismatch

Even after removing np.array() from your threshold definitions, you need to confirm two key things:

  • Your input image is a 3-channel color image (not grayscale). Print its shape with print(img.shape) — it should look like (height, width, 3). If it’s just (height, width), you accidentally loaded a grayscale image; fix that by explicitly loading in color: img = cv2.imread("your_image.jpg", cv2.IMREAD_COLOR).
  • Your thresholds align with the YCrCb channel order (Y, Cr, Cb). Your current values (0, 138, 67) and (255, 173, 133) are correct for this order, but if you were mapping to YCbCr (Y, Cb, Cr), that would break everything.

3. Verify cv2.inRange() Inputs

The cv2.inRange() function has strict shape requirements:

  • It needs a 3-channel source image (your YCrCb-converted image)
  • Lower and upper bounds must be 1D sequences of 3 values (matching the 3 channels)

If either the image or thresholds have the wrong number of dimensions, you’ll get that frustrating shape mismatch error.

Corrected Code Snippet

Here’s a cleaned-up version of your code with these fixes and built-in debugging checks:

import cv2
import numpy as np
import util as ut
import svm_train as st

# Load image (force 3-channel color mode)
img = cv2.imread("your_input_image.jpg", cv2.IMREAD_COLOR)
if img is None:
    raise ValueError("Failed to load image — double-check the file path!")
print(f"Input image shape: {img.shape}")  # Should output (h, w, 3)

# Convert to YCrCb (correct OpenCV color space name)
ycrcb_img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
print(f"YCrCb image shape: {ycrcb_img.shape}")  # Should still be (h, w, 3)

# Define thresholds (tuples work perfectly fine here, no np.array needed)
skin_ycrcb_min = (0, 138, 67)
skin_ycrcb_max = (255, 173, 133)

# Generate skin segmentation mask
skin_mask = cv2.inRange(ycrcb_img, skin_ycrcb_min, skin_ycrcb_max)
print(f"Skin mask shape: {skin_mask.shape}")  # Should output (h, w) (single channel)

# Load your trained SVM model
model = st.trainSVM(17)

# Proceed with your logic — just ensure any data passed to the model matches its expected input shape
# For example, if the model expects flattened features: skin_features = skin_mask.flatten().reshape(1, -1)

Extra Debugging Tips

  • Print the shape of every key array (input image, YCrCb image, mask) to pinpoint exactly where the mismatch occurs.
  • Check your svm_train.py file to confirm the trainSVM() model expects inputs of a specific shape. If it’s trained on flattened features, you’ll need to reshape your mask or segmentation results before passing them in.

内容的提问来源于stack exchange,提问作者Madhusudan Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:38:33