# RoboDK and Open CV Part Measurement

**URL:** <https://forum.opencv.org/t/robodk-and-open-cv-part-measurement/20706>\
**Category:** Python\
**Tags:** aruco\
**Created:** [April 1, 2025, 8:32pm UTC](https://forum.opencv.org/t/robodk-and-open-cv-part-measurement/20706 "2025-04-01T20:32:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khaboom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/khaboom/32/11761_2.png) [@Khaboom](https://forum.opencv.org/u/Khaboom)\
**Post date:** [April 1, 2025, 8:32pm UTC](https://forum.opencv.org/t/robodk-and-open-cv-part-measurement/20706/1 "2025-04-01T20:32:48Z")

</div>

I am using RoboDK to simulate vision detection.  
Camera is perpendicular to the ArUco marker  
However, I cannot get the virtual camera to have correct contour detection.  
The image is suppose to me 100mm x 100mm. However, once it does the contour detected it is always incorrect.  
Any ideas?

 ![ArUco](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/dfb3c1c90b4be94c58ee83abeee54146a6dc55cd.png)

```python
import cv2
import numpy as np
import cv2.aruco as aruco
from robodk.robolink import Robolink

# Initialize RoboDK connection
RDK = Robolink()
camera = RDK.Item('CAM')

# Camera calibration parameters (virtual camera, no distortion)
camera_matrix = np.array([[628.3385, 0, 640.4397], [0, 628.244, 358.7204], [0, 0, 1]], dtype=np.float32)
dist_coeffs = np.zeros((5, 1))

# Define ArUco dictionary
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_6X6_250)
parameters = aruco.DetectorParameters()

# Define known ArUco marker size in mm
MARKER_SIZE_MM = 100 # 100mm marker

def get_robodk_camera_frame():
    """Grab an image from RoboDK's virtual camera."""
    img = RDK.Cam2D_Snapshot("", camera)
    if not img:
        print("No image from RoboDK camera!")
        return None

    img = cv2.imdecode(np.frombuffer(img, dtype=np.uint8), cv2.IMREAD_COLOR)
    return img

def get_pixel_to_mm_ratio(corners):
    """Calculate pixel-to-mm conversion ratio using the ArUco marker."""
    if corners is None:
        return None

    # Calculate the distance between the two corners of the ArUco marker (e.g., horizontal distance)
    pixel_width = np.linalg.norm(corners[0][0][0] - corners[0][0][1])
    pixel_to_mm_ratio = MARKER_SIZE_MM / pixel_width
    return pixel_to_mm_ratio

def detect_objects_and_measure():
    """Detect ArUco marker, find contours, and display object dimensions."""
    frame = get_robodk_camera_frame()
    if frame is None:
        return

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    corners, ids, _ = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)

    if ids is None:
        print("No ArUco marker detected.")
        return

    aruco.drawDetectedMarkers(frame, corners, ids)
    pixel_to_mm_ratio = get_pixel_to_mm_ratio(corners)

    if pixel_to_mm_ratio is None:
        print("Could not determine pixel-to-mm ratio.")
        return

    print(f"Pixel-to-MM ratio: {pixel_to_mm_ratio:.2f}")

    # Apply Gaussian Blur to smooth out edges before detection
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    _, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
    # Use Canny edge detection for better contour detection
    edges = cv2.Canny(binary, 50, 150)

    # Find contours in the edge-detected image
    contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Filter out contours that are too large or too small based on the expected scale
    for contour in contours:
        if cv2.contourArea(contour) < 500: # Filter out small contours (noise)
            continue

        # Get bounding rectangle (no rotation)
        x, y, w, h = cv2.boundingRect(contour)

        # Convert to mm using the pixel-to-mm ratio
        width_mm = w * pixel_to_mm_ratio
        height_mm = h * pixel_to_mm_ratio

        # Correcting the scale factor: Adjust the width and height by a constant factor (e.g., 0.98)
        width_mm *= 1 # Apply scale correction factor (you can adjust this value based on testing)
        height_mm *= 1

        # Filter out unrealistically large dimensions (optional based on expected object size)
        if width_mm > 1000 or height_mm > 1000:
            continue # Skip any detections with dimensions too large

        # Draw bounding box and dimensions on the image
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(frame, f"{width_mm:.2f} mm x {height_mm:.2f} mm",
                    (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)

    # Display the result
    cv2.imshow("Object Detection & Measurement", frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# Run detection
detect_objects_and_measure()

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [April 1, 2025, 10:50pm UTC](https://forum.opencv.org/t/robodk-and-open-cv-part-measurement/20706/3 "2025-04-01T22:50:16Z")

</div>

please also supply that picture without anything drawn on it, just the picture with the aruco. simply add a post with that.
