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 encoding1 = encodings[i];
00076 cv::Mat image_original(cv::Size(400, 400), cv_bridge::getCvType(encoding1));
00077 cv::RNG r(77);
00078 r.fill(image_original, cv::RNG::UNIFORM, 0, 255);
00079
00080 sensor_msgs::Image image_message;
00081 cv_bridge::CvImage image_bridge(std_msgs::Header(), encoding1, image_original);
00082
00083
00084 sensor_msgs::ImagePtr image_msg = image_bridge.toImageMsg();
00085
00086 for(size_t j=0; j<encodings.size(); ++j) {
00087 std::string encoding2 = encodings[j];
00088
00089
00090 if (numChannels(encoding1) != numChannels(encoding2)) {
00091
00092 continue;
00093 }
00094
00095
00096 if (isColor(encoding1) != isColor(encoding2))
00097 continue;
00098
00099 if ((isUnsigned(encoding1) != isUnsigned(encoding2)) && (!isColor(encoding1)))
00100 continue;
00101
00102 if (bitDepth(encoding1)==16 && bitDepth(encoding2)==8)
00103 continue;
00104
00105 if (encoding2 == YUV422)
00106 continue;
00107
00108 if (encoding1 == YUV422) {
00109 cv_bridge::toCvShare(image_msg, encoding2);
00110 continue;
00111 }
00112
00113
00114 std::cout << encoding1 << " " << encoding2 << std::endl;
00115 cv::Mat image_back = cvtColor(cv_bridge::toCvShare(image_msg, encoding2), encoding1)->image;
00116
00117 EXPECT_LT(cv::norm(image_original, image_back)/image_original.cols/image_original.rows, 0.5) << "problem converting from " << encoding1 << " to " << encoding2 << " and back.";
00118 }
00119 }
00120 }
00121
00122 int main(int argc, char **argv)
00123 {
00124 testing::InitGoogleTest(&argc, argv);
00125 return RUN_ALL_TESTS();
00126 }