31 OpenPoseEstimation::setupPropertyDefinitions(def);
33 def->optional(radius,
"DepthMedianRadius",
"Depth Median Radius");
37 "Pixels with a distance higher than this value are masked out. Only for depth camera mode.",
38 PropertyDefinitionBase::eModifiable);
39 def->optional(maxDepthDifference,
41 "Allowed difference of depth value for one keypoint to median of all keypoints.",
42 PropertyDefinitionBase::eModifiable);
43 def->optional(cameraNodeName,
"CameraNodeName");
49 OpenPoseEstimation::setupLocalVariables();
55 OpenPoseEstimation::destroyLocalVariables();
61 if (!localRobot || !running3D_is_possible || !running3D)
66 RemoteRobot::synchronizeLocalCloneToTimestamp(
67 localRobot, robotStateInterface, timestamp_of_update);
69 const int depthThreshold = maxDepthDifference;
70 openposeResult3D.clear();
72 for (
const auto& entities_iterator : openposeResult)
74 const std::string& name = entities_iterator->first;
75 const Entity2D& entity = entities_iterator->second;
77 if (entity.keypointMap.size() == 0)
82 std::map<std::string, int>
84 std::vector<int> depthsCopy;
87 for (
const auto& entity_iterator : entity.keypointMap)
89 const std::string& kp_name = entities_iterator->first;
90 const Keypoint2D& point = entities_iterator->second;
92 int depth = getMedianDepthFromImage(
93 static_cast<int>(point.x),
static_cast<int>(point.y), radius);
94 depthStorage[kp_name] = depth;
95 depthsCopy.push_back(depth);
99 std::sort(depthsCopy.begin(), depthsCopy.end());
100 const int medianDepth = depthsCopy.at(depthsCopy.size() / 2);
101 for (
auto& depthStorage_iterator : depthStorage)
103 const std::string& storage_name = depthStorage_iterator->first;
104 int& depth = depthStorage_iterator->second;
106 if (depth > medianDepth + depthThreshold || depth < medianDepth - depthThreshold)
114 openposeResult3D[name] = Entity3D();
115 for (
const auto& entity_iterator : entity.keypointMap)
117 const std::string& kp_name = entity_iterator->first;
118 const Keypoint3D& point = entity_iterator->second;
121 imagePoint.x = point.x;
122 imagePoint.y = point.y;
126 calibration->ImageToWorldCoordinates(imagePoint,
128 static_cast<float>(depthStorage.at(i)),
129 useDistortionParameters);
132 openposeResult3D[name].keypointMap[kp_name].x = result.x;
133 openposeResult3D[name].keypointMap[kp_name].y = result.y;
134 openposeResult3D[name].keypointMap[kp_name].z = result.z;
144 OpenPose3DDepthImageConverter::getMedianDepthFromImage(
int x,
int y,
int radius)
const
146 std::vector<int> depths;
147 for (
int xoffset = -radius; xoffset < radius; xoffset++)
149 int xo = x + xoffset;
150 if (xo < 0 || xo > depthImageBuffer->width)
154 for (
int yoffset = -radius; yoffset < radius; yoffset++)
156 int yo = y + yoffset;
157 if (yo < 0 || yo > depthImageBuffer->height)
163 if (xoffset * xoffset + yoffset * yoffset <= radius * radius)
165 unsigned int pixelPos =
166 static_cast<unsigned int>(3 * (yo * depthImageBuffer->width + xo));
167 int z_value = depthImageBuffer->pixels[pixelPos + 0] +
168 (depthImageBuffer->pixels[pixelPos + 1] << 8) +
169 (depthImageBuffer->pixels[pixelPos + 2] << 16);
172 depths.push_back(z_value);
177 std::sort(depths.begin(), depths.end());
179 return depths.empty() ? 0 : depths[depths.size() / 2];
183 OpenPose3DDepthImageConverter::maskOutBasedOnDepth(CByteImage& image,
int maxDepth)
185 std::unique_lock lock(depthImageBufferMutex);
191 int pixelCount = depthImageBuffer->width * depthImageBuffer->height;
192 int depthThresholdmm = maxDepth;
193 CByteImage maskImage(depthImageBuffer->width, depthImageBuffer->height, CByteImage::eGrayScale);
194 for (
int i = 0; i < pixelCount; i += 1)
196 int z_value = depthImageBuffer->pixels[i * 3 + 0] +
197 (depthImageBuffer->pixels[i * 3 + 1] << 8) +
198 (depthImageBuffer->pixels[i * 3 + 2] << 16);
199 maskImage.pixels[i] = z_value > depthThresholdmm || z_value == 0 ? 0 : 255;
201 ::ImageProcessor::Erode(&maskImage, &maskImage, 5);
202 ::ImageProcessor::Dilate(&maskImage, &maskImage, 20);
203 for (
int i = 0; i < pixelCount; i += 1)
205 if (maskImage.pixels[i] == 0)
207 image.pixels[i * 3] = 0;
208 image.pixels[i * 3 + 1] = 255;
209 image.pixels[i * 3 + 2] = 0;