basic.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2012, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
36 
37 #include <gtest/gtest.h>
38 
39 #include <cv_bridge/cv_bridge.h>
40 
41 static unsigned int receivedImages = 0;
42 std::vector<std::string> receivedEncodings;
43 
44 void handleImage(const sensor_msgs::ImageConstPtr& img)
45 {
46  cv_bridge::CvImagePtr cvImg = cv_bridge::toCvCopy(img, "bgr8");
47  receivedEncodings.push_back(img->encoding);
48 
49  ASSERT_EQ(cvImg->image.rows, 400);
50  ASSERT_EQ(cvImg->image.cols, 400);
51 
52  // Red corner
53  cv::Scalar upperLeft = cv::mean(cvImg->image(cv::Rect(0, 0, 200, 200)));
54  EXPECT_LT(upperLeft[0], 10);
55  EXPECT_LT(upperLeft[1], 10);
56  EXPECT_GT(upperLeft[2], 240);
57 
58  // Green corner
59  cv::Scalar upperRight = cv::mean(cvImg->image(cv::Rect(200, 0, 200, 200)));
60  EXPECT_LT(upperRight[0], 10);
61  EXPECT_GT(upperRight[1], 240);
62  EXPECT_LT(upperRight[2], 10);
63 
64  // Blue corner
65  cv::Scalar lowerRight = cv::mean(cvImg->image(cv::Rect(200, 200, 200, 200)));
66  EXPECT_GT(lowerRight[0], 240);
67  EXPECT_LT(lowerRight[1], 10);
68  EXPECT_LT(lowerRight[2], 10);
69 
70  // White corner
71  cv::Scalar lowerLeft = cv::mean(cvImg->image(cv::Rect(0, 200, 200, 200)));
72  EXPECT_GT(lowerLeft[0], 240);
73  EXPECT_GT(lowerLeft[1], 240);
74  EXPECT_GT(lowerLeft[2], 240);
75 
77 }
78 
79 TEST(Basic, loop)
80 {
81  ros::NodeHandle nh;
83 
84  image_transport::Publisher pub = it.advertise("img", 20);
86 
87  // Create a nice test image. Corners will be red, green, blue, white in clock-wise order.
88  cv::Mat_<cv::Vec3b> testImage(400, 400); // This is BGR!
89  for(int y = 0; y < 200; ++y)
90  {
91  for(int x = 0; x < 200; ++x)
92  testImage(y,x) = cv::Vec3b(0, 0, 255);
93 
94  for(int x = 200; x < 400; ++x)
95  testImage(y,x) = cv::Vec3b(0, 255, 0);
96  }
97  for(int y = 200; y < 400; ++y)
98  {
99  for(int x = 0; x < 200; ++x)
100  testImage(y,x) = cv::Vec3b(255, 255, 255);
101 
102  for(int x = 200; x < 400; ++x)
103  testImage(y,x) = cv::Vec3b(255, 0, 0);
104  }
105 
106  ros::spinOnce();
107 
108  unsigned int expectedImages = 0;
109  std::vector<std::string> expectedEncodings;
110 
111  // Try different formats
112  {
113  cv_bridge::CvImage cvImg;
114  testImage.copyTo(cvImg.image);
116 
117  sensor_msgs::ImagePtr msg = cvImg.toImageMsg();
118  EXPECT_EQ(msg->encoding, sensor_msgs::image_encodings::BGR8);
119 
120  pub.publish(msg);
121  expectedImages++;
122  expectedEncodings.push_back(sensor_msgs::image_encodings::BGR8);
123  }
124  {
125  cv_bridge::CvImage cvImg;
126  cv::cvtColor(testImage, cvImg.image, CV_BGR2RGB);
128 
129  sensor_msgs::ImagePtr msg = cvImg.toImageMsg();
130  EXPECT_EQ(msg->encoding, sensor_msgs::image_encodings::RGB8);
131 
132  pub.publish(msg);
133  expectedImages++;
134  expectedEncodings.push_back(sensor_msgs::image_encodings::RGB8);
135  }
136  {
137  cv_bridge::CvImage cvImg;
138  cv::cvtColor(testImage, cvImg.image, CV_BGR2RGBA);
140 
141  sensor_msgs::ImagePtr msg = cvImg.toImageMsg();
142  EXPECT_EQ(msg->encoding, sensor_msgs::image_encodings::RGBA8);
143 
144  pub.publish(msg);
145  expectedImages++;
146  expectedEncodings.push_back(sensor_msgs::image_encodings::RGBA8);
147  }
148  {
149  cv_bridge::CvImage cvImg;
150  cv::cvtColor(testImage, cvImg.image, CV_BGR2BGRA);
152 
153  sensor_msgs::ImagePtr msg = cvImg.toImageMsg();
154  EXPECT_EQ(msg->encoding, sensor_msgs::image_encodings::BGRA8);
155 
156  pub.publish(msg);
157  expectedImages++;
158  expectedEncodings.push_back(sensor_msgs::image_encodings::BGRA8);
159  }
160 
162  while(receivedImages < expectedImages && (ros::WallTime::now() - start) < ros::WallDuration(3.0))
163  {
164  ros::spinOnce();
165  ros::WallDuration(0.1).sleep();
166  }
167  EXPECT_EQ(receivedImages, expectedImages);
168 
169  ASSERT_EQ(receivedEncodings.size(), expectedEncodings.size());
170  for(std::size_t i = 0; i < receivedEncodings.size(); ++i)
171  EXPECT_EQ(receivedEncodings[i], expectedEncodings[i]);
172 }
173 
174 
175 int main(int argc, char **argv)
176 {
177  testing::InitGoogleTest(&argc, argv);
178  ros::init(argc, argv, "basic");
179  ros::NodeHandle nh;
180  return RUN_ALL_TESTS();
181 }
ros::WallDuration::sleep
bool sleep() const
image_transport::ImageTransport
boost::shared_ptr
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
handleImage
void handleImage(const sensor_msgs::ImageConstPtr &img)
Definition: basic.cpp:44
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
sensor_msgs::image_encodings::RGBA8
const std::string RGBA8
ros::spinOnce
ROSCPP_DECL void spinOnce()
sensor_msgs::image_encodings::RGB8
const std::string RGB8
main
int main(int argc, char **argv)
Definition: basic.cpp:175
image_transport::ImageTransport::advertise
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
image_transport::Subscriber
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
image_transport::ImageTransport::subscribe
Subscriber subscribe(const std::string &base_topic, uint32_t queue_size, const boost::function< void(const sensor_msgs::ImageConstPtr &)> &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
ros::WallTime::now
static WallTime now()
image_transport::Publisher::publish
void publish(const sensor_msgs::Image &message) const
ros::WallTime
receivedImages
static unsigned int receivedImages
Definition: basic.cpp:41
image_transport.h
start
ROSCPP_DECL void start()
sensor_msgs::image_encodings::BGRA8
const std::string BGRA8
TEST
TEST(Basic, loop)
Definition: basic.cpp:79
cv_bridge::CvImage::encoding
std::string encoding
image_transport::Publisher
cv_bridge.h
cv_bridge::CvImage
sensor_msgs::image_encodings::BGR8
const std::string BGR8
image_transport::TransportHints
ros::WallDuration
cv_bridge::CvImage::image
cv::Mat image
ros::NodeHandle
receivedEncodings
std::vector< std::string > receivedEncodings
Definition: basic.cpp:42


compressed_image_transport
Author(s): Patrick Mihelich, Julius Kammerl, David Gossow
autogenerated on Sat Jan 27 2024 03:31:06