utest2.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
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                            //BAYER_RGGB8, BAYER_BGGR8, BAYER_GBRG8, BAYER_GRBG8, BAYER_RGGB16, BAYER_BGGR16, BAYER_GBRG16, BAYER_GRBG16,
00052                            //YUV422
00053 }
00054 std::vector<std::string>
00055 getEncodings() {
00056 // TODO for Groovy, the following types should be uncommented
00057 std::string encodings[] = { RGB8, RGBA8, RGB16, RGBA16, BGR8, BGRA8, BGR16, BGRA16, MONO8, MONO16,
00058                            TYPE_8UC1, /*TYPE_8UC2,*/ TYPE_8UC3, TYPE_8UC4,
00059                            TYPE_8SC1, /*TYPE_8SC2,*/ TYPE_8SC3, TYPE_8SC4,
00060                            TYPE_16UC1, /*TYPE_16UC2,*/ TYPE_16UC3, TYPE_16UC4,
00061                            TYPE_16SC1, /*TYPE_16SC2,*/ TYPE_16SC3, TYPE_16SC4,
00062                            TYPE_32SC1, /*TYPE_32SC2,*/ TYPE_32SC3, TYPE_32SC4,
00063                            TYPE_32FC1, /*TYPE_32FC2,*/ TYPE_32FC3, TYPE_32FC4,
00064                            TYPE_64FC1, /*TYPE_64FC2,*/ TYPE_64FC3, TYPE_64FC4,
00065                            //BAYER_RGGB8, BAYER_BGGR8, BAYER_GBRG8, BAYER_GRBG8, BAYER_RGGB16, BAYER_BGGR16, BAYER_GBRG16, BAYER_GRBG16,
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     // Convert to a sensor_msgs::Image
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       // It makes sense you cannot convert to a different number of channels and back
00089       // TODO Actually, that should be a < but we can't with the TYPE_8UC1 conversions: waiting for Groovy
00090       if (numChannels(encoding1) != numChannels(encoding2)) {
00091         //EXPECT_THROW(cvtColor(cv_bridge::toCvShare(image_msg, encoding2), encoding1));
00092         continue;
00093       }
00094       // Same if one is a color encoding and not the other one
00095       // TODO: that should throw but we'll wait for Groovy
00096       if (isColor(encoding1) != isColor(encoding2))
00097         continue;
00098       // Same if they are of different signs
00099       if ((isUnsigned(encoding1) != isUnsigned(encoding2)) && (!isColor(encoding1)))
00100         continue;
00101       // Because of the scaling, we cannot test 16-8 conversion here
00102       if (bitDepth(encoding1)==16 && bitDepth(encoding2)==8)
00103         continue;
00104       // We do not support conversion to YUV422 for now
00105       if (encoding2 == YUV422)
00106         continue;
00107       // We cannot convert to YUV422 so we just perform the first conversion, not the back
00108       if (encoding1 == YUV422) {
00109         cv_bridge::toCvShare(image_msg, encoding2);
00110         continue;
00111       }
00112 
00113       // And convert back to a cv::Mat
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 }


opencv_tests
Author(s): James Bowman
autogenerated on Sat Dec 28 2013 17:53:20