YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
depthimage_to_monoTest.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <yarp/os/Network.h>
8#include <yarp/os/LogStream.h>
9#include <yarp/os/Time.h>
11#include <yarp/sig/Image.h>
12
14
15#include <array>
16#include <thread>
17#include <cmath>
18
19#include <catch2/catch_amalgamated.hpp>
20#include <harness.h>
21
22TEST_CASE("pm::depthimage_to_monoTest", "[yarp::pm]")
23{
24 YARP_REQUIRE_PLUGIN("depthimage_to_mono", "portmonitor")
25
27
30
31 struct TestCase {
32 std::string carrier;
33 bool should_convert;
34 };
35
36 auto tc = GENERATE(
37 TestCase {"fast_tcp", false},
38 TestCase {"fast_tcp+recv.portmonitor+file.depthimage_to_mono+type.dll", true}
39 );
40
41 SECTION("Test depth image to mono conversion with portmonitor: " + tc.carrier)
42 {
45
46 sender.open("/send");
47 receiver.open("/recv");
48
49 bool b = yarp::os::Network::connect("/send", "/recv", tc.carrier);
50 REQUIRE(b);
51
52 // Create a test depth image (16x16 pixels)
53 constexpr int width = 16;
54 constexpr int height = 16;
55
57 depthImg.resize(width, height);
58
59 // Fill with test data - values between 0.2 and 10.0 meters
60 // Use a gradient pattern for easier verification
61 for (int y = 0; y < height; y++)
62 {
63 for (int x = 0; x < width; x++)
64 {
65 float depth_value = 0.2f + (static_cast<float>(x + y * width) / (width * height - 1)) * 9.8f;
66 depthImg.pixel(x, y) = depth_value;
67 }
68 }
69
70 sender.write();
71
73
74 yarp::sig::FlexImage* receivedImg = receiver.read();
75 REQUIRE(receivedImg != nullptr);
76
77 // Verify dimensions
78 CHECK(receivedImg->width() == width);
79 CHECK(receivedImg->height() == height);
80
81 if (tc.should_convert)
82 {
83 // Verify it's converted to mono (grayscale)
84 CHECK(receivedImg->getPixelCode() == VOCAB_PIXEL_MONO);
85 CHECK(receivedImg->getPixelSize() == 1);
86
87 // Verify some pixel values are in grayscale range [0-255]
88 unsigned char* pixels = receivedImg->getRawImage();
89 for (int i = 0; i < width * height; i++)
90 {
91 CHECK(pixels[i] <= 255);
92 }
93 }
94 else
95 {
96 // Without conversion, should still be float
97 CHECK(receivedImg->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT);
98 }
99
100 receiver.close();
101 sender.close();
102 }
103
105}
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_MONO
Definition Image.h:42
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:751
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1118
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:718
Image class with user control of representation details.
Definition Image.h:361
Typed image class.
Definition Image.h:603
T & pixel(size_t x, size_t y)
Definition Image.h:619
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:462
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition Image.cpp:370
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition Image.cpp:387
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:375
TEST_CASE("pm::depthimage_to_monoTest", "[yarp::pm]")
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16