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 <gtest/gtest.h>
00036
00037 #include <opencv/cxtypes.h>
00038 #include "opencv/cxcore.h"
00039 #include "opencv/cvwimage.h"
00040 #include "opencv/cv.h"
00041 #include "opencv/highgui.h"
00042
00043 #include "cv_bridge/CvBridge.h"
00044 #include "sensor_msgs/Image.h"
00045
00046
00047 TEST(OpencvTests, testCase_encode_decode)
00048 {
00049 int fmts[] = { IPL_DEPTH_8U, -1, IPL_DEPTH_8S, IPL_DEPTH_16U, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F, -1 };
00050
00051 for (int w = 100; w < 800; w += 100) {
00052 for (int h = 100; h < 800; h += 100) {
00053 for (int fi = 0; fmts[fi] != -1; fi++) {
00054 for (int channels = 1; channels <= 4; channels++) {
00055 IplImage *original = cvCreateImage(cvSize(w, h), fmts[fi], channels);
00056 #if 0 // XXX OpenCV bug, change this when opencv_latest next moves
00057 CvRNG r = cvRNG(77);
00058 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00059 #else
00060 cvSet(original, cvScalar(1,2,3,4));
00061 #endif
00062
00063 sensor_msgs::Image image_message;
00064 sensor_msgs::CvBridge img_bridge_;
00065
00066 int success;
00067 success = img_bridge_.fromIpltoRosImage(original, image_message);
00068 ASSERT_TRUE(success);
00069 success = img_bridge_.fromImage(image_message);
00070 ASSERT_TRUE(success);
00071 IplImage *final = img_bridge_.toIpl();
00072
00073
00074 IplImage *diff = cvCloneImage(original);
00075 cvAbsDiff(original, final, diff);
00076 CvScalar sum = cvSum(diff);
00077 for (int c = 0; c < channels; c++) {
00078 EXPECT_TRUE(sum.val[c] == 0);
00079 }
00080 }
00081 }
00082 }
00083 }
00084 }
00085
00086
00087 TEST(OpencvTests, testCase_decode_8u)
00088 {
00089 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
00090 CvRNG r = cvRNG(77);
00091 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00092
00093 sensor_msgs::Image image_message;
00094 sensor_msgs::CvBridge img_bridge_;
00095
00096 int success;
00097 success = img_bridge_.fromIpltoRosImage(original, image_message);
00098 ASSERT_TRUE(success);
00099
00100 success = img_bridge_.fromImage(image_message, "passthrough");
00101 ASSERT_TRUE(success);
00102 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 1);
00103
00104 success = img_bridge_.fromImage(image_message, "mono8");
00105 ASSERT_TRUE(success);
00106 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 1);
00107
00108 success = img_bridge_.fromImage(image_message, "rgb8");
00109 ASSERT_TRUE(success);
00110 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00111
00112 success = img_bridge_.fromImage(image_message, "bgr8");
00113 ASSERT_TRUE(success);
00114 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00115 }
00116
00117 TEST(OpencvTests, testCase_decode_16u)
00118 {
00119 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
00120 CvRNG r = cvRNG(77);
00121 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00122
00123 sensor_msgs::Image image_message;
00124 sensor_msgs::CvBridge img_bridge_;
00125
00126 int success;
00127 success = img_bridge_.fromIpltoRosImage(original, image_message);
00128 ASSERT_TRUE(success);
00129
00130 success = img_bridge_.fromImage(image_message, "mono16");
00131 ASSERT_TRUE(success);
00132 printf("%d\n", cvGetElemType(img_bridge_.toIpl()));
00133 EXPECT_TRUE(cvGetElemType(img_bridge_.toIpl()) == CV_16UC1);
00134 }
00135
00136 TEST(OpencvTests, testCase_decode_8uc3)
00137 {
00138 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);
00139 CvRNG r = cvRNG(77);
00140 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00141
00142 sensor_msgs::Image image_message;
00143 sensor_msgs::CvBridge img_bridge_;
00144
00145 int success;
00146 success = img_bridge_.fromIpltoRosImage(original, image_message);
00147 ASSERT_TRUE(success);
00148
00149 success = img_bridge_.fromImage(image_message, "passthrough");
00150 ASSERT_TRUE(success);
00151 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00152
00153 success = img_bridge_.fromImage(image_message, "mono8");
00154 ASSERT_TRUE(success);
00155 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 1);
00156
00157 success = img_bridge_.fromImage(image_message, "rgb8");
00158 ASSERT_TRUE(success);
00159 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00160
00161 success = img_bridge_.fromImage(image_message, "bgr8");
00162 ASSERT_TRUE(success);
00163 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00164 }
00165
00166 TEST(OpencvTests, testCase_new_methods)
00167 {
00168 int channels = 3;
00169 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, channels);
00170 CvRNG r = cvRNG(77);
00171 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00172
00173 sensor_msgs::CvBridge img_bridge_;
00174 sensor_msgs::Image::Ptr rosimg = img_bridge_.cvToImgMsg(original);
00175
00176 IplImage *final = img_bridge_.imgMsgToCv(rosimg);
00177
00178 IplImage *diff = cvCloneImage(original);
00179 cvAbsDiff(original, final, diff);
00180 CvScalar sum = cvSum(diff);
00181 for (int c = 0; c < channels; c++) {
00182 EXPECT_TRUE(sum.val[c] == 0);
00183 }
00184 }
00185
00186 TEST(OpencvTests, testCase_16u_bgr)
00187 {
00188 int channels = 1;
00189 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_16U, channels);
00190 CvRNG r = cvRNG(77);
00191 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00192
00193 sensor_msgs::CvBridge img_bridge_;
00194 sensor_msgs::Image::Ptr image_message = img_bridge_.cvToImgMsg(original, "mono16");
00195
00196 int success;
00197 success = img_bridge_.fromImage(*image_message, "mono16");
00198 ASSERT_TRUE(success);
00199 EXPECT_TRUE(cvGetElemType(img_bridge_.toIpl()) == CV_16UC1);
00200
00201 success = img_bridge_.fromImage(*image_message, "bgr8");
00202 ASSERT_TRUE(success);
00203 EXPECT_TRUE(cvGetElemType(img_bridge_.toIpl()) == CV_8UC3);
00204 }
00205
00206 int main(int argc, char **argv)
00207 {
00208 testing::InitGoogleTest(&argc, argv);
00209 return RUN_ALL_TESTS();
00210 }