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/cxcore.h"
00038 #include "opencv/cvwimage.h"
00039 #include "opencv/cv.h"
00040 #include "opencv/highgui.h"
00041
00042 #include "cv_bridge/CvBridge.h"
00043 #include "sensor_msgs/Image.h"
00044
00045 #define FIXME_HACKISH_RELEASE_ELECTRIC 0
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 EXPECT_TRUE(success);
00069 success = img_bridge_.fromImage(image_message);
00070 EXPECT_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 EXPECT_TRUE(success);
00099
00100 success = img_bridge_.fromImage(image_message, "passthrough");
00101 EXPECT_TRUE(success);
00102 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 1);
00103
00104 success = img_bridge_.fromImage(image_message, "mono8");
00105 EXPECT_TRUE(success);
00106 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 1);
00107
00108 success = img_bridge_.fromImage(image_message, "rgb8");
00109 EXPECT_TRUE(success);
00110 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00111
00112 success = img_bridge_.fromImage(image_message, "bgr8");
00113 EXPECT_TRUE(success);
00114 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00115 }
00116 #if FIXME_HACKISH_RELEASE_ELECTRIC
00117 TEST(OpencvTests, testCase_decode_16u)
00118 {
00119 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_16U, 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 EXPECT_TRUE(success);
00129
00130 success = img_bridge_.fromImage(image_message, "mono16");
00131 EXPECT_TRUE(success);
00132 printf("%d\n", cvGetElemType(img_bridge_.toIpl()));
00133 EXPECT_TRUE(cvGetElemType(img_bridge_.toIpl()) == CV_16UC1);
00134 }
00135 #endif
00136
00137 TEST(OpencvTests, testCase_decode_8uc3)
00138 {
00139 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);
00140 CvRNG r = cvRNG(77);
00141 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00142
00143 sensor_msgs::Image image_message;
00144 sensor_msgs::CvBridge img_bridge_;
00145
00146 int success;
00147 success = img_bridge_.fromIpltoRosImage(original, image_message);
00148 EXPECT_TRUE(success);
00149
00150 success = img_bridge_.fromImage(image_message, "passthrough");
00151 EXPECT_TRUE(success);
00152 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00153
00154 success = img_bridge_.fromImage(image_message, "mono8");
00155 EXPECT_TRUE(success);
00156 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 1);
00157
00158 success = img_bridge_.fromImage(image_message, "rgb8");
00159 EXPECT_TRUE(success);
00160 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00161
00162 success = img_bridge_.fromImage(image_message, "bgr8");
00163 EXPECT_TRUE(success);
00164 EXPECT_TRUE(CV_MAT_CN(cvGetElemType(img_bridge_.toIpl())) == 3);
00165 }
00166
00167 TEST(OpencvTests, testCase_new_methods)
00168 {
00169 int channels = 3;
00170 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, channels);
00171 CvRNG r = cvRNG(77);
00172 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00173
00174 sensor_msgs::CvBridge img_bridge_;
00175 sensor_msgs::Image::Ptr rosimg = img_bridge_.cvToImgMsg(original);
00176
00177 IplImage *final = img_bridge_.imgMsgToCv(rosimg);
00178
00179 IplImage *diff = cvCloneImage(original);
00180 cvAbsDiff(original, final, diff);
00181 CvScalar sum = cvSum(diff);
00182 for (int c = 0; c < channels; c++) {
00183 EXPECT_TRUE(sum.val[c] == 0);
00184 }
00185 }
00186
00187 #if FIXME_HACKISH_RELEASE_ELECTRIC
00188 TEST(OpencvTests, testCase_16u_bgr)
00189 {
00190 int channels = 1;
00191 IplImage *original = cvCreateImage(cvSize(640, 480), IPL_DEPTH_16U, channels);
00192 CvRNG r = cvRNG(77);
00193 cvRandArr(&r, original, CV_RAND_UNI, cvScalar(0,0,0,0), cvScalar(255,255,255,255));
00194
00195 sensor_msgs::CvBridge img_bridge_;
00196 sensor_msgs::Image::Ptr image_message = img_bridge_.cvToImgMsg(original, "mono16");
00197
00198 int success;
00199 success = img_bridge_.fromImage(*image_message, "mono16");
00200 EXPECT_TRUE(success);
00201 EXPECT_TRUE(cvGetElemType(img_bridge_.toIpl()) == CV_16UC1);
00202
00203
00204 success = img_bridge_.fromImage(*image_message, "bgr8");
00205 EXPECT_TRUE(success);
00206 EXPECT_TRUE(cvGetElemType(img_bridge_.toIpl()) == CV_8UC3);
00207 }
00208 #endif
00209
00210 int main(int argc, char **argv)
00211 {
00212 testing::InitGoogleTest(&argc, argv);
00213 return RUN_ALL_TESTS();
00214 }