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/os/Carriers.h>
12#include <yarp/sig/Image.h>
13
15
16#include <array>
17#include <thread>
18#include <cmath>
19
20#include <catch2/catch_amalgamated.hpp>
21#include <harness.h>
22
23TEST_CASE("pm::depthimage_to_monoTest", "[yarp::pm]")
24{
25 YARP_REQUIRE_PLUGIN("depthimage_to_mono", "portmonitor")
26
28
30
31 REQUIRE(yarp::os::Carriers::getCarrierTemplate("portmonitor") != nullptr);
32
34
35 struct TestCase {
36 std::string carrier;
37 bool should_convert;
38 };
39
40 auto tc = GENERATE(
41 TestCase {"fast_tcp", false},
42 TestCase {"fast_tcp+recv.portmonitor+file.depthimage_to_mono+type.dll", true}
43 );
44
45 SECTION("Test depth image to mono conversion with portmonitor: " + tc.carrier)
46 {
49
50 sender.open("/send");
51 receiver.open("/recv");
52
53 bool b = yarp::os::Network::connect("/send", "/recv", tc.carrier);
54 REQUIRE(b);
55
56 // Create a test depth image (16x16 pixels)
57 constexpr int width = 16;
58 constexpr int height = 16;
59
61 depthImg.resize(width, height);
62
63 // Fill with test data - values between 0.2 and 10.0 meters
64 // Use a gradient pattern for easier verification
65 for (int y = 0; y < height; y++)
66 {
67 for (int x = 0; x < width; x++)
68 {
69 float depth_value = 0.2f + (static_cast<float>(x + y * width) / (width * height - 1)) * 9.8f;
70 depthImg.pixel(x, y) = depth_value;
71 }
72 }
73
74 sender.write();
75
77
78 yarp::sig::FlexImage* receivedImg = receiver.read();
79 REQUIRE(receivedImg != nullptr);
80
81 // Verify dimensions
82 CHECK(receivedImg->width() == width);
83 CHECK(receivedImg->height() == height);
84
85 if (tc.should_convert)
86 {
87 // Verify it's converted to mono (grayscale)
88 CHECK(receivedImg->getPixelCode() == VOCAB_PIXEL_MONO);
89 CHECK(receivedImg->getPixelSize() == 1);
90
91 // Verify some pixel values are in grayscale range [0-255]
92 unsigned char* pixels = receivedImg->getRawImage();
93 for (int i = 0; i < width * height; i++)
94 {
95 CHECK(pixels[i] <= 255);
96 }
97 }
98 else
99 {
100 // Without conversion, should still be float
101 CHECK(receivedImg->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT);
102 }
103
104 receiver.close();
105 sender.close();
106 }
107
109}
@ 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 Carrier * getCarrierTemplate(const std::string &name)
Get template for carrier.
Definition Carriers.cpp:238
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:784
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1137
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