Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.7 kB
2
Indexable
import cv2
import numpy as np


# To check the values, go to: https://pixspy.com/ 
import cv2
import numpy as np

# Load the first mask to get the center's location in pixels
mask = cv2.imread('/home/towoko/Documents/BundleTrack/YCBINEOAT/pallox/masks/0000.png', cv2.IMREAD_GRAYSCALE)

# Calculate the center of mass of the mask
M = cv2.moments(mask)
center_x = int(M['m10'] / M['m00'])
center_y = int(M['m01'] / M['m00'])

# Print the center coordinates
print("Center coordinates: ({}, {})".format(center_x, center_y))


# # THIS IS ANOTHER WAY THAT USES THE BOUNDING BOX INSTEAD OF THE CENTER OF MASS
# mask = cv2.imread('/home/towoko/Documents/BundleTrack/YCBINEOAT/pallox/masks/0000.png', cv2.IMREAD_GRAYSCALE)

# # Find contours of the mask
# contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# # Compute the center of the bounding rectangle
# x, y, w, h = cv2.boundingRect(contours[0])
# center_x = x + w // 2
# center_y = y + h // 2

# # Print the center coordinates
# print("Center coordinates: ({}, {})".format(center_x, center_y))

import cv2

# Load the PNG depth image (assumed to be in 16-bit format)
depth_image = cv2.imread('/home/towoko/Documents/BundleTrack/YCBINEOAT/pallox/depth/0000.png', cv2.IMREAD_ANYDEPTH)
print("Depth image shape:", depth_image.shape)
print("Depth image dtype:", depth_image.dtype)

# Get the shape of the depth image
height, width = depth_image.shape

# Assume you already have the center coordinates (center_x, center_y)
# Get the depth value at the center pixel
depth_value = depth_image[center_y, center_x]

# Print the depth value
print("Depth value at center pixel: ", depth_value)