00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <string>
00036 #include <vector>
00037 #include <gtest/gtest.h>
00038
00039 #include "opencv2/core/core.hpp"
00040
00041 #include "cv_bridge/cv_bridge.h"
00042 #include <sensor_msgs/Image.h>
00043 #include <sensor_msgs/image_encodings.h>
00044
00045 using namespace sensor_msgs::image_encodings;
00046
00047 bool isUnsigned(const std::string & encoding) {
00048 return encoding == RGB8 || encoding == RGBA8 || encoding == RGB16 || encoding == RGBA16 || encoding == BGR8 || encoding == BGRA8 || encoding == BGR16 || encoding == BGRA16 || encoding == MONO8 || encoding == MONO16 ||
00049 encoding == MONO8 || encoding == MONO16 || encoding == TYPE_8UC1 || encoding == TYPE_8UC2 || encoding == TYPE_8UC3 || encoding == TYPE_8UC4 ||
00050 encoding == TYPE_16UC1 || encoding == TYPE_16UC2 || encoding == TYPE_16UC3 || encoding == TYPE_16UC4;
00051
00052
00053 }
00054 std::vector<std::string>
00055 getEncodings() {
00056
00057 std::string encodings[] = { RGB8, RGBA8, RGB16, RGBA16, BGR8, BGRA8, BGR16, BGRA16, MONO8, MONO16,
00058 TYPE_8UC1, TYPE_8UC3, TYPE_8UC4,
00059 TYPE_8SC1, TYPE_8SC3, TYPE_8SC4,
00060 TYPE_16UC1, TYPE_16UC3, TYPE_16UC4,
00061 TYPE_16SC1, TYPE_16SC3, TYPE_16SC4,
00062 TYPE_32SC1, TYPE_32SC3, TYPE_32SC4,
00063 TYPE_32FC1, TYPE_32FC3, TYPE_32FC4,
00064 TYPE_64FC1, TYPE_64FC3, TYPE_64FC4,
00065
00066 YUV422
00067 };
00068 return std::vector<std::string>(encodings, encodings+47-8-7);
00069 }
00070
00071 TEST(OpencvTests, testCase_encode_decode)
00072 {
00073 std::vector<std::string> encodings = getEncodings();
00074 for(size_t i=0; i<encodings.size(); ++i) {
00075 std::string src_encoding = encodings[i];
00076 bool is_src_color_format = isColor(src_encoding) || isMono(src_encoding) || (src_encoding == sensor_msgs::image_encodings::YUV422);
00077 cv::Mat image_original(cv::Size(400, 400), cv_bridge::getCvType(src_encoding));
00078 cv::RNG r(77);
00079 r.fill(image_original, cv::RNG::UNIFORM, 0, 127);
00080
00081 sensor_msgs::Image image_message;
00082 cv_bridge::CvImage image_bridge(std_msgs::Header(), src_encoding, image_original);
00083
00084
00085 sensor_msgs::ImagePtr image_msg = image_bridge.toImageMsg();
00086
00087 for(size_t j=0; j<encodings.size(); ++j) {
00088 std::string dst_encoding = encodings[j];
00089 bool is_dst_color_format = isColor(dst_encoding) || isMono(dst_encoding) || (dst_encoding == sensor_msgs::image_encodings::YUV422);
00090 bool is_num_channels_the_same = (numChannels(src_encoding) == numChannels(dst_encoding));
00091
00092 cv_bridge::CvImageConstPtr cv_image;
00093 cv::Mat image_back;
00094
00095 if (!is_src_color_format) {
00096
00097 if (is_dst_color_format) {
00098 EXPECT_THROW(cv_bridge::toCvShare(image_msg, dst_encoding), cv_bridge::Exception);
00099 continue;
00100 }
00101
00102 if (!is_num_channels_the_same) {
00103 EXPECT_THROW(cv_bridge::toCvShare(image_msg, dst_encoding), cv_bridge::Exception);
00104 continue;
00105 }
00106 cv_image = cv_bridge::toCvShare(image_msg, dst_encoding);
00107 } else {
00108
00109 if (!is_dst_color_format) {
00110 if (!is_num_channels_the_same) {
00111 EXPECT_THROW(cv_bridge::toCvShare(image_msg, dst_encoding), cv_bridge::Exception);
00112 continue;
00113 }
00114 cv_image = cv_bridge::toCvShare(image_msg, dst_encoding);
00115
00116 EXPECT_THROW(cvtColor(cv_image, src_encoding)->image, cv_bridge::Exception);
00117 continue;
00118 }
00119
00120 if ((dst_encoding == YUV422) && (src_encoding != YUV422)) {
00121 EXPECT_THROW(cv_bridge::toCvShare(image_msg, dst_encoding), cv_bridge::Exception);
00122 continue;
00123 }
00124
00125 cv_image = cv_bridge::toCvShare(image_msg, dst_encoding);
00126
00127
00128 if ((src_encoding == YUV422) && (dst_encoding != YUV422)) {
00129 EXPECT_THROW(cvtColor(cv_image, src_encoding)->image, cv_bridge::Exception);
00130 continue;
00131 }
00132 }
00133
00134 image_back = cvtColor(cv_image, src_encoding)->image;
00135
00136
00137 if (!is_num_channels_the_same)
00138 continue;
00139 if (bitDepth(src_encoding) >= 32) {
00140
00141 EXPECT_LT(cv::norm(image_original, image_back, cv::NORM_INF), 1) << "problem converting from " << src_encoding << " to " << dst_encoding << " and back.";
00142 } else if ((bitDepth(src_encoding) == 16) && (bitDepth(dst_encoding) == 8)) {
00143
00144 EXPECT_LT(cv::norm(image_original, image_back, cv::NORM_INF), 128) << "problem converting from " << src_encoding << " to " << dst_encoding << " and back.";
00145 } else {
00146 EXPECT_EQ(cv::norm(image_original, image_back, cv::NORM_INF), 0) << "problem converting from " << src_encoding << " to " << dst_encoding << " and back.";
00147 }
00148 }
00149 }
00150 }