Colour masking with python cv2 and CUDA on Jetson Orin Nano

nvidia-l4t-core 36.4.4-20250616085344
Cuda v.12.6
JP 6.2 
opencv: 4.10.0

Using python code as below:

import cv2 as cv
import numpy as np
import sys

# Define the image path
image_path = "/media/nano/KIOXIA/Jetson_orin_nano_stuff/yolo_stuff/training_stuff/data/coco8/images/train/000000000009.jpg"

# --- 1. GPU/CUDA Initialization Check ---
if cv.cuda.getCudaEnabledDeviceCount() == 0:
    print("FATAL ERROR: CUDA is not available for OpenCV. Exiting.")
    sys.exit(1)
else:
    print("CUDA is available and enabled.")

# --- 2. Color Bounds (HSV) ---
lower_green_np = np.array([40, 50, 50], dtype=np.uint8) 
upper_green_np = np.array([80, 255, 255], dtype=np.uint8) 

# --- 3. Load Image and Setup GpuMat Objects ---
cpu_image = cv.imread(image_path)

# --- DEBUG STEP 1: VERIFY IMAGE WAS LOADED ---
if cpu_image is None:
    print(f"FATAL ERROR: Could not load image at {image_path}. cv.imread returned None.")
    sys.exit(1)
print(f"CPU image loaded successfully. Shape: {cpu_image.shape}")

# Initialize GpuMat objects...
gpu_image_bgr = cv.cuda_GpuMat()
gpu_image_hsv = cv.cuda_GpuMat()
# ... (rest of the initializations are fine) ...
gpu_mask = cv.cuda_GpuMat() 
gpu_h_channel = cv.cuda_GpuMat()
gpu_s_channel = cv.cuda_GpuMat()
gpu_v_channel = cv.cuda_GpuMat()
mask_h_lower = cv.cuda_GpuMat()
mask_h_upper = cv.cuda_GpuMat()
mask_s_lower = cv.cuda_GpuMat()
mask_v_lower = cv.cuda_GpuMat()


import cv2 as cv
import numpy as np
import sys

# ... (Previous code remains the same up to this point) ...

# --- 4. GPU Processing Pipeline (With CPU Conversion Workaround) ---

try:
    print("Starting GPU processing with CPU->HSV workaround...")
    
    # 4a. Upload original BGR image to the GPU (still useful if you need it)
    gpu_image_bgr.upload(cpu_image)
    print(f"GPU BGR image uploaded. Size: {gpu_image_bgr.size()}")

    # --- WORKAROUND: Perform HSV Conversion on CPU ---
    # 1. Convert BGR to HSV on the CPU as the GPU version is failing.
    cpu_image_hsv = cv.cvtColor(cpu_image, cv.COLOR_BGR2HSV)
    # 2. Upload the result to the destination GpuMat.
    gpu_image_hsv.upload(cpu_image_hsv)
    # --- END WORKAROUND ---

    if gpu_image_hsv.empty():
        print("FATAL ERROR: GpuMat is still empty even after CPU workaround.")
        sys.exit(1)
    print(f"GPU HSV image created via CPU and uploaded. Size: {gpu_image_hsv.size()}")

    # 4c. Split HSV into three 1-channel GpuMats (This should now work)
    hsv_channels = [gpu_h_channel, gpu_s_channel, gpu_v_channel]
    cv.cuda.split(gpu_image_hsv, hsv_channels)
    print("GPU HSV image split into channels.")

    # 4d. Create Mask using the more reliable cv.cuda.threshold
    cv.cuda.threshold(gpu_h_channel, int(lower_green_np[0]), 255, cv.THRESH_BINARY, mask_h_lower)
    print("a")
    cv.cuda.threshold(gpu_h_channel, int(upper_green_np[0]), 255, cv.THRESH_BINARY_INV, mask_h_upper)
    print("b")
    cv.cuda.bitwise_and(mask_h_lower, mask_h_upper, gpu_mask)
    print("c")

    cv.cuda.threshold(gpu_s_channel, int(lower_green_np[1]), 255, cv.THRESH_BINARY, mask_s_lower)
    print("d")
    cv.cuda.bitwise_and(gpu_mask, mask_s_lower, gpu_mask)
    print("e")

    cv.cuda.threshold(gpu_v_channel, int(lower_green_np[2]), 255, cv.THRESH_BINARY, mask_v_lower)
    print("f")
    cv.cuda.bitwise_and(gpu_mask, mask_v_lower, gpu_mask)
    print("g")

    # 4e. Download the final mask from the GPU to the CPU
    cpu_mask = gpu_mask.download()
    print("h")
    
    cpu_masked_image = cv.bitwise_and(cpu_image, cpu_image, mask=cpu_mask)
    print("i")

    print("GPU processing complete.")

    # --- 5. Display Results ---
    cv.imshow("Original Image", cpu_image)
    cv.imshow("Green Mask (Binary)", cpu_mask)
    cv.imshow("Masked Image", cpu_masked_image)
    
    cv.waitKey(0)
    cv.destroyAllWindows()

except cv.error as e:
    print(f"\nOpenCV CUDA Error during processing: {e}")
    sys.exit(1)

getting errors such as :

CUDA is available and enabled.
CPU image loaded successfully. Shape: (480, 640, 3)
Starting GPU processing with CPU->HSV workaround...
GPU BGR image uploaded. Size: (640, 480)
GPU HSV image created via CPU and uploaded. Size: (640, 480)
GPU HSV image split into channels.

OpenCV CUDA Error during processing: OpenCV(4.10.0) /home/nano/Documents/yolo_stuff/workspace/opencv_contrib-4.10.0/modules/cudev/include/opencv2/cudev/grid/detail/transform.hpp:389: error: (-217:Gpu API call) invalid configuration argument in function 'call'

I can see this:

 members of cv2.cuda

('ALPHA_ATOP', 3),
 ('ALPHA_ATOP_PREMUL', 9),
 ('ALPHA_IN', 1),
 ('ALPHA_IN_PREMUL', 7),
 ('ALPHA_OUT', 2),
 ('ALPHA_OUT_PREMUL', 8),
 ('ALPHA_OVER', 0),
 ('ALPHA_OVER_PREMUL', 6),
 ('ALPHA_PLUS', 5),
 ('ALPHA_PLUS_PREMUL', 11),
 ('ALPHA_PREMUL', 12),
 ('ALPHA_XOR', 4),
 ('ALPHA_XOR_PREMUL', 10),
 ('BroxOpticalFlow_create', <function BroxOpticalFlow_create>),
 ('COLOR_BAYER_BG2BGR_MHT', 256),
 ('COLOR_BAYER_BG2GRAY_MHT', 260),
 ('COLOR_BAYER_BG2RGB_MHT', 258),
 ('COLOR_BAYER_GB2BGR_MHT', 257),
 ('COLOR_BAYER_GB2GRAY_MHT', 261),
 ('COLOR_BAYER_GB2RGB_MHT', 259),
 ('COLOR_BAYER_GR2BGR_MHT', 259),
 ('COLOR_BAYER_GR2GRAY_MHT', 263),
 ('COLOR_BAYER_GR2RGB_MHT', 257),
 ('COLOR_BAYER_RG2BGR_MHT', 258),
 ('COLOR_BAYER_RG2GRAY_MHT', 262),
 ('COLOR_BAYER_RG2RGB_MHT', 256),
 ('COLOR_BayerBG2BGR_MHT', 256),
 ('COLOR_BayerBG2GRAY_MHT', 260),
 ('COLOR_BayerBG2RGB_MHT', 258),
 ('COLOR_BayerGB2BGR_MHT', 257),
 ('COLOR_BayerGB2GRAY_MHT', 261),
 ('COLOR_BayerGB2RGB_MHT', 259),
 ('COLOR_BayerGR2BGR_MHT', 259),
 ('COLOR_BayerGR2GRAY_MHT', 263),
 ('COLOR_BayerGR2RGB_MHT', 257),
 ('COLOR_BayerRG2BGR_MHT', 258),
 ('COLOR_BayerRG2GRAY_MHT', 262),
 ('COLOR_BayerRG2RGB_MHT', 256),
 ('CascadeClassifier_create', <function CascadeClassifier_create>),
 ('DEVICE_INFO_COMPUTE_MODE_DEFAULT', 0),
 ('DEVICE_INFO_COMPUTE_MODE_EXCLUSIVE', 1),
 ('DEVICE_INFO_COMPUTE_MODE_EXCLUSIVE_PROCESS', 3),
 ('DEVICE_INFO_COMPUTE_MODE_PROHIBITED', 2),
 ('DYNAMIC_PARALLELISM', 35),
 ('DensePyrLKOpticalFlow_create', <function DensePyrLKOpticalFlow_create>),
 ('DescriptorMatcher_createBFMatcher',
  <function DescriptorMatcher_createBFMatcher>),
 ('DeviceInfo_ComputeModeDefault', 0),
 ('DeviceInfo_ComputeModeExclusive', 1),
 ('DeviceInfo_ComputeModeExclusiveProcess', 3),
 ('DeviceInfo_ComputeModeProhibited', 2),
 ('EVENT_BLOCKING_SYNC', 1),
 ('EVENT_DEFAULT', 0),
 ('EVENT_DISABLE_TIMING', 2),
 ('EVENT_INTERPROCESS', 4),
 ('Event_BLOCKING_SYNC', 1),
 ('Event_DEFAULT', 0),
 ('Event_DISABLE_TIMING', 2),
 ('Event_INTERPROCESS', 4),
 ('Event_elapsedTime', <function Event_elapsedTime>),
 ('FEATURE_SET_COMPUTE_10', 10),
 ('FEATURE_SET_COMPUTE_11', 11),
 ('FEATURE_SET_COMPUTE_12', 12),
 ('FEATURE_SET_COMPUTE_13', 13),
 ('FEATURE_SET_COMPUTE_20', 20),
 ('FEATURE_SET_COMPUTE_21', 21),
 ('FEATURE_SET_COMPUTE_30', 30),
 ('FEATURE_SET_COMPUTE_32', 32),
 ('FEATURE_SET_COMPUTE_35', 35),
 ('FEATURE_SET_COMPUTE_50', 50),
 ('FarnebackOpticalFlow_create', <function FarnebackOpticalFlow_create>),
 ('FastFeatureDetector_create', <function FastFeatureDetector_create>),
 ('GLOBAL_ATOMICS', 11),
 ('GpuMat_defaultAllocator', <function GpuMat_defaultAllocator>),
 ('GpuMat_setDefaultAllocator', <function GpuMat_setDefaultAllocator>),
 ('HOG_create', <function HOG_create>),
 ('HOST_MEM_PAGE_LOCKED', 1),
 ('HOST_MEM_SHARED', 2),
 ('HOST_MEM_WRITE_COMBINED', 4),
 ('HostMem_PAGE_LOCKED', 1),
 ('HostMem_SHARED', 2),
 ('HostMem_WRITE_COMBINED', 4),
 ('NATIVE_DOUBLE', 13),
 ('NVIDIA_OPTICAL_FLOW_1_0_NV_OF_PERF_LEVEL_FAST', 20),
 ('NVIDIA_OPTICAL_FLOW_1_0_NV_OF_PERF_LEVEL_MAX', 21),
 ('NVIDIA_OPTICAL_FLOW_1_0_NV_OF_PERF_LEVEL_MEDIUM', 10),
 ('NVIDIA_OPTICAL_FLOW_1_0_NV_OF_PERF_LEVEL_SLOW', 5),
 ('NVIDIA_OPTICAL_FLOW_1_0_NV_OF_PERF_LEVEL_UNDEFINED', 0),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_1', 1),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_2', 2),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_4', 4),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_8', 8),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_MAX', 9),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_UNDEFINED', 0),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_1', 1),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_2', 2),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_4', 4),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_MAX', 5),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_UNDEFINED', 0),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_PERF_LEVEL_FAST', 20),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_PERF_LEVEL_MAX', 21),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_PERF_LEVEL_MEDIUM', 10),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_PERF_LEVEL_SLOW', 5),
 ('NVIDIA_OPTICAL_FLOW_2_0_NV_OF_PERF_LEVEL_UNDEFINED', 0),
 ('NvidiaOpticalFlow_1_0_NV_OF_PERF_LEVEL_FAST', 20),
 ('NvidiaOpticalFlow_1_0_NV_OF_PERF_LEVEL_MAX', 21),
 ('NvidiaOpticalFlow_1_0_NV_OF_PERF_LEVEL_MEDIUM', 10),
 ('NvidiaOpticalFlow_1_0_NV_OF_PERF_LEVEL_SLOW', 5),
 ('NvidiaOpticalFlow_1_0_NV_OF_PERF_LEVEL_UNDEFINED', 0),
 ('NvidiaOpticalFlow_1_0_create', <function NvidiaOpticalFlow_1_0_create>),
 ('NvidiaOpticalFlow_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_1', 1),
 ('NvidiaOpticalFlow_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_2', 2),
 ('NvidiaOpticalFlow_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_4', 4),
 ('NvidiaOpticalFlow_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_8', 8),
 ('NvidiaOpticalFlow_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_MAX', 9),
 ('NvidiaOpticalFlow_2_0_NV_OF_HINT_VECTOR_GRID_SIZE_UNDEFINED', 0),
 ('NvidiaOpticalFlow_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_1', 1),
 ('NvidiaOpticalFlow_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_2', 2),
 ('NvidiaOpticalFlow_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_4', 4),
 ('NvidiaOpticalFlow_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_MAX', 5),
 ('NvidiaOpticalFlow_2_0_NV_OF_OUTPUT_VECTOR_GRID_SIZE_UNDEFINED', 0),
 ('NvidiaOpticalFlow_2_0_NV_OF_PERF_LEVEL_FAST', 20),
 ('NvidiaOpticalFlow_2_0_NV_OF_PERF_LEVEL_MAX', 21),
 ('NvidiaOpticalFlow_2_0_NV_OF_PERF_LEVEL_MEDIUM', 10),
 ('NvidiaOpticalFlow_2_0_NV_OF_PERF_LEVEL_SLOW', 5),
 ('NvidiaOpticalFlow_2_0_NV_OF_PERF_LEVEL_UNDEFINED', 0),
 ('NvidiaOpticalFlow_2_0_create', <function NvidiaOpticalFlow_2_0_create>),
 ('ORB_create', <function ORB_create>),
 ('OpticalFlowDual_TVL1_create', <function OpticalFlowDual_TVL1_create>),
 ('SHARED_ATOMICS', 12),
 ('SURF_CUDA_ANGLE_ROW', 5),
 ('SURF_CUDA_HESSIAN_ROW', 6),
 ('SURF_CUDA_LAPLACIAN_ROW', 2),
 ('SURF_CUDA_OCTAVE_ROW', 3),
 ('SURF_CUDA_ROWS_COUNT', 7),
 ('SURF_CUDA_SIZE_ROW', 4),
 ('SURF_CUDA_X_ROW', 0),
 ('SURF_CUDA_Y_ROW', 1),
 ('SURF_CUDA_create', <function SURF_CUDA_create>),
 ('SparsePyrLKOpticalFlow_create', <function SparsePyrLKOpticalFlow_create>),
 ('StereoBeliefPropagation_estimateRecommendedParams',
  <function StereoBeliefPropagation_estimateRecommendedParams>),
 ('StereoConstantSpaceBP_estimateRecommendedParams',
  <function StereoConstantSpaceBP_estimateRecommendedParams>),
 ('Stream_Null', <function Stream_Null>),
 ('TargetArchs_has', <function TargetArchs_has>),
 ('TargetArchs_hasBin', <function TargetArchs_hasBin>),
 ('TargetArchs_hasEqualOrGreater', <function TargetArchs_hasEqualOrGreater>),
 ('TargetArchs_hasEqualOrGreaterBin',
  <function TargetArchs_hasEqualOrGreaterBin>),
 ('TargetArchs_hasEqualOrGreaterPtx',
  <function TargetArchs_hasEqualOrGreaterPtx>),
 ('TargetArchs_hasEqualOrLessPtx', <function TargetArchs_hasEqualOrLessPtx>),
 ('TargetArchs_hasPtx', <function TargetArchs_hasPtx>),
 ('WARP_SHUFFLE_FUNCTIONS', 30),
 ('__doc__', None),
 ('__loader__', None),
 ('__name__', 'cv2.cuda'),
 ('__package__', None),
 ('__spec__', None),
 ('abs', <function abs>),
 ('absSum', <function absSum>),
 ('absdiff', <function absdiff>),
 ('add', <function add>),
 ('addWeighted', <function addWeighted>),
 ('alphaComp', <function alphaComp>),
 ('bilateralFilter', <function bilateralFilter>),
 ('bitwise_and', <function bitwise_and>),
 ('bitwise_not', <function bitwise_not>),
 ('bitwise_or', <function bitwise_or>),
 ('bitwise_xor', <function bitwise_xor>),
 ('blendLinear', <function blendLinear>),
 ('buildWarpAffineMaps', <function buildWarpAffineMaps>),
 ('buildWarpPerspectiveMaps', <function buildWarpPerspectiveMaps>),
 ('calcAbsSum', <function calcAbsSum>),
 ('calcHist', <function calcHist>),
 ('calcNorm', <function calcNorm>),
 ('calcNormDiff', <function calcNormDiff>),
 ('calcSqrSum', <function calcSqrSum>),
 ('calcSum', <function calcSum>),
 ('cartToPolar', <function cartToPolar>),
 ('compare', <function compare>),
 ('copyMakeBorder', <function copyMakeBorder>),
 ('countNonZero', <function countNonZero>),
 ('createBackgroundSubtractorMOG', <function createBackgroundSubtractorMOG>),
 ('createBackgroundSubtractorMOG2', <function createBackgroundSubtractorMOG2>),
 ('createBoxFilter', <function createBoxFilter>),
 ('createBoxMaxFilter', <function createBoxMaxFilter>),
 ('createBoxMinFilter', <function createBoxMinFilter>),
 ('createCLAHE', <function createCLAHE>),
 ('createCannyEdgeDetector', <function createCannyEdgeDetector>),
 ('createColumnSumFilter', <function createColumnSumFilter>),
 ('createContinuous', <function createContinuous>),
 ('createConvolution', <function createConvolution>),
 ('createDFT', <function createDFT>),
 ('createDerivFilter', <function createDerivFilter>),
 ('createDisparityBilateralFilter', <function createDisparityBilateralFilter>),
 ('createGaussianFilter', <function createGaussianFilter>),
 ('createGeneralizedHoughBallard', <function createGeneralizedHoughBallard>),
 ('createGeneralizedHoughGuil', <function createGeneralizedHoughGuil>),
 ('createGoodFeaturesToTrackDetector',
  <function createGoodFeaturesToTrackDetector>),
 ('createHarrisCorner', <function createHarrisCorner>),
 ('createHoughCirclesDetector', <function createHoughCirclesDetector>),
 ('createHoughLinesDetector', <function createHoughLinesDetector>),
 ('createHoughSegmentDetector', <function createHoughSegmentDetector>),
 ('createLaplacianFilter', <function createLaplacianFilter>),
 ('createLinearFilter', <function createLinearFilter>),
 ('createLookUpTable', <function createLookUpTable>),
 ('createMedianFilter', <function createMedianFilter>),
 ('createMinEigenValCorner', <function createMinEigenValCorner>),
 ('createMorphologyFilter', <function createMorphologyFilter>),
 ('createRowSumFilter', <function createRowSumFilter>),
 ('createScharrFilter', <function createScharrFilter>),
 ('createSeparableLinearFilter', <function createSeparableLinearFilter>),
 ('createSobelFilter', <function createSobelFilter>),
 ('createStereoBM', <function createStereoBM>),
 ('createStereoBeliefPropagation', <function createStereoBeliefPropagation>),
 ('createStereoConstantSpaceBP', <function createStereoConstantSpaceBP>),
 ('createStereoSGM', <function createStereoSGM>),
 ('createTemplateMatching', <function createTemplateMatching>),
 ('cvtColor', <function cvtColor>),
 ('demosaicing', <function demosaicing>),
 ('dft', <function dft>),
 ('divide', <function divide>),
 ('drawColorDisp', <function drawColorDisp>),
 ('ensureSizeIsEnough', <function ensureSizeIsEnough>),
 ('equalizeHist', <function equalizeHist>),
 ('evenLevels', <function evenLevels>),
 ('exp', <function exp>),
 ('findMinMax', <function findMinMax>),
 ('findMinMaxLoc', <function findMinMaxLoc>),
 ('flip', <function flip>),
 ('gammaCorrection', <function gammaCorrection>),
 ('gemm', <function gemm>),
 ('getCudaEnabledDeviceCount', <function getCudaEnabledDeviceCount>),
 ('getDevice', <function getDevice>),
 ('histEven', <function histEven>),
 ('histRange', <function histRange>),
 ('inRange', <function inRange>),
 ('integral', <function integral>),
 ('log', <function log>),
 ('lshift', <function lshift>),
 ('magnitude', <function magnitude>),
 ('magnitudeSqr', <function magnitudeSqr>),
 ('max', <function max>),
 ('meanShiftFiltering', <function meanShiftFiltering>),
 ('meanShiftProc', <function meanShiftProc>),
 ('meanShiftSegmentation', <function meanShiftSegmentation>),
 ('meanStdDev', <function meanStdDev>),
 ('merge', <function merge>),
 ('min', <function min>),
 ('minMax', <function minMax>),
 ('minMaxLoc', <function minMaxLoc>),
 ('mulAndScaleSpectrums', <function mulAndScaleSpectrums>),
 ('mulSpectrums', <function mulSpectrums>),
 ('multiply', <function multiply>),
 ('norm', <function norm>),
 ('normalize', <function normalize>),
 ('phase', <function phase>),
 ('polarToCart', <function polarToCart>),
 ('pow', <function pow>),
 ('printCudaDeviceInfo', <function printCudaDeviceInfo>),
 ('printShortCudaDeviceInfo', <function printShortCudaDeviceInfo>),
 ('pyrDown', <function pyrDown>),
 ('pyrUp', <function pyrUp>),
 ('rectStdDev', <function rectStdDev>),
 ('reduce', <function reduce>),
 ('registerPageLocked', <function registerPageLocked>),
 ('remap', <function remap>),
 ('reprojectImageTo3D', <function reprojectImageTo3D>),
 ('resetDevice', <function resetDevice>),
 ('resize', <function resize>),
 ('rotate', <function rotate>),
 ('rshift', <function rshift>),
 ('setBufferPoolConfig', <function setBufferPoolConfig>),
 ('setBufferPoolUsage', <function setBufferPoolUsage>),
 ('setDevice', <function setDevice>),
 ('split', <function split>),
 ('sqr', <function sqr>),
 ('sqrIntegral', <function sqrIntegral>),
 ('sqrSum', <function sqrSum>),
 ('sqrt', <function sqrt>),
 ('subtract', <function subtract>),
 ('sum', <function sum>),
 ('threshold', <function threshold>),
 ('transpose', <function transpose>),
 ('unregisterPageLocked', <function unregisterPageLocked>),
 ('warpAffine', <function warpAffine>),
 ('warpPerspective', <function warpPerspective>)

Is there a cuda enabled cv2 example for colour mask?
Please do not reply with LLM
Thank you.

*** Please note that this reply is generated by LLM automatically ***

Based on the provided context, it appears that you are experiencing errors with OpenCV (cv2) on the Jetson Orin Nano, specifically with CUDA-enabled functionality. You are looking for an example of using CUDA-enabled cv2 for color masking.

After reviewing the context, I found a few relevant topics that might help:

  1. CUDA Containers for Edge AI & Robotics: This topic mentions the availability of CUDA 12.8 SBSA wheels for Python 3.10 / 22.04 or Python 3.12 / 24.04. You can install these wheels using pip3.
  2. Need Help Regarding Install Torchvision, Pytorch with CUDA support on Jetson nano (reComputer J1010 -4GB): This topic discusses installing Torchvision and PyTorch with CUDA support on the Jetson Nano. Although it’s not directly related to OpenCV, it might provide some insights into CUDA installation and configuration.
  3. CSI Camera 0 Hangs When Trying to Collect an Image with v4l2-ctl: This topic mentions issues with camera functionality on the Jetson Orin Nano. Although it’s not directly related to OpenCV, it might indicate some underlying issues with the Jetson’s camera or CUDA functionality.

To address your question, I couldn’t find a specific example of CUDA-enabled cv2 for color masking in the provided context. However, I can suggest some general steps to help you troubleshoot and potentially resolve the issue:

  1. Verify CUDA installation: Ensure that CUDA is properly installed and configured on your Jetson Orin Nano. You can check the CUDA version using nvcc --version.
  2. Check OpenCV version: Verify that you are using a CUDA-enabled version of OpenCV. You can check the OpenCV version using cv2.__version__.
  3. Use CUDA-enabled OpenCV functions: Make sure to use CUDA-enabled OpenCV functions, such as cv2.UMat or cv2.cuda_GpuMat, to leverage the GPU acceleration.
  4. Consult OpenCV documentation: Refer to the OpenCV documentation for examples and guidance on using CUDA-enabled functionality.

Here’s a simple example of using CUDA-enabled cv2 for color masking:

import cv2

# Load the image
img = cv2.imread('image.jpg')

# Convert the image to a CUDA-enabled UMat
img_cuda = cv2.UMat(img)

# Define the color mask
lower_red = (0, 100, 100)
upper_red = (10, 255, 255)

# Apply the color mask using CUDA-enabled functionality
mask_cuda = cv2.cuda_GpuMat()
cv2.cuda.cvtColor(img_cuda, cv2.COLOR_BGR2HSV, mask_cuda)
cv2.cuda.inRange(mask_cuda, lower_red, upper_red, mask_cuda)

# Convert the mask back to a CPU-based numpy array
mask = mask_cuda.download()

# Display the result
cv2.imshow('Color Mask', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note that this example assumes you have a CUDA-enabled OpenCV installation and a compatible NVIDIA GPU.

*** Please give the thumbs up if you get answers you like. Or provide feedback to help us improve the answer. ***

Traceback (most recent call last):
  File "/home/nano/Documents/yolo_stuff/test_opencv_cuda_on_image_10.py", line 26, in <module>
    cv2.cuda.cvtColor(img_cuda, cv2.COLOR_BGR2HSV, mask_cuda)
cv2.error: OpenCV(4.10.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - src is not a numpy array, neither a scalar
>  - Expected Ptr<cv::cuda::GpuMat> for argument 'src'
>  - Expected Ptr<cv::UMat> for argument 'dst'

Hi,

It looks like duplicate to the topic 346750.
Let’s continue the discussion there.

Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.