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 #include <aidnav_msgs/MBSonar.h>
00035 #include <sensor_msgs/image_encodings.h>
00036 #include <aidnav_msgs/SetRange.h>
00037 #include <cv_bridge/cv_bridge.h>
00038
00039
00040 #include <opencv2/highgui/highgui.hpp>
00041 #include <opencv2/opencv.hpp>
00042 #include <ros/ros.h>
00043 #include <exception>
00044
00045 ros::Time last;
00046
00047 cv::Mat disp(const cv::Mat& img, float max = 1024)
00048 {
00049 cv::Mat nimg;
00050 img.convertTo(nimg,CV_32FC1);
00051 return (nimg=nimg/max);
00052 }
00053
00054 void normalize(const cv::Mat& frame)
00055 {
00056 cv::Scalar mean, std;
00057 double min,max;
00058
00059 cv::Mat nimg = frame;
00060
00061
00062 cv::meanStdDev(nimg,mean,std);
00063
00064 nimg = (nimg - mean.val[0])*std.val[0];
00065
00066 cv::minMaxLoc(nimg,&min,&max);
00067
00068
00069
00070 }
00071
00072 std::pair<float, float> noise_estimate(cv::Mat& meanM, cv::Mat& stdM, size_t x, size_t y, int colSpan, int rowSpan)
00073 {
00074 std::pair<float, float> noisep;
00075 noisep.first = 0;
00076 noisep.second = 0;
00077
00078 int nrois = 0;
00079 for (int i=-colSpan; i<colSpan; i+=colSpan)
00080 {
00081 for (int j=-rowSpan; j<rowSpan; j+=rowSpan)
00082 {
00083 if ((i!=0) || (j!=0))
00084 {
00085 int xroi = x+i;
00086 int yroi = y+j;
00087
00088 if ((xroi<0) || (xroi>meanM.cols) || (yroi<0) || (yroi>meanM.rows)) continue;
00089 ++nrois;
00090 noisep.first += meanM.at<float>(xroi,yroi);
00091 noisep.second += stdM.at<float>(xroi,yroi);
00092 }
00093 }
00094 }
00095
00096 noisep.first /= nrois;
00097 noisep.second /= nrois;
00098
00099 return noisep;
00100 }
00101
00102 void callback(const aidnav_msgs::MBSonarConstPtr& image)
00103 {
00104 ROS_INFO("Received image: %d x %d x 2 = %d",image->image.width, image->image.height,image->image.data.size());
00105 ROS_INFO("Elapsed time: %f",(ros::Time::now() - last).toSec());
00106 last = ros::Time::now();
00107
00108 cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvCopy(image->image, sensor_msgs::image_encodings::MONO16);
00109
00110 cv::imshow("Original",disp(cv_ptr->image,512));
00111
00112 cv::Mat org = cv_ptr->image.clone();
00113
00114
00115
00116 size_t colSpan = 40, rowSpan = 40;
00117
00118 cv::Mat meanM(org.cols,org.rows,CV_32FC1);
00119 cv::Mat stdM(org.cols,org.rows,CV_32FC1);
00120 for(size_t i=colSpan/2; i<org.cols; i+=colSpan)
00121 {
00122 for (size_t j=rowSpan/2; j<org.rows; j+=rowSpan)
00123 {
00124 cv::Rect roi(i-colSpan/2,j-rowSpan/2,colSpan,rowSpan);
00125 cv::Mat roiImg(org,roi);
00126
00127 cv::Scalar mean, std;
00128 cv::meanStdDev(roiImg,mean,std);
00129 meanM.at<float>(i,j) = mean.val[0];
00130 stdM.at<float>(i,j) = std.val[0];
00131
00132
00133
00134 }
00135 }
00136
00137 cv::imshow("Normalized",disp(org,512));
00138
00139 org = cv_ptr->image.clone();
00140 cv::Mat org2;
00141 org.convertTo(org2, CV_32FC1);
00142 org = org2;
00143
00144 for(size_t i=colSpan/2; i<org.cols; i+=colSpan)
00145 {
00146 for (size_t j=rowSpan/2; j<org.rows; j+=rowSpan)
00147 {
00148 cv::Rect roi(i-colSpan/2,j-rowSpan/2,colSpan,rowSpan);
00149 cv::Mat roiImg(org,roi);
00150 std::pair<float, float> noisep = noise_estimate(meanM, stdM, i,j, colSpan, rowSpan);
00151 std::cout<<"The ROI mean:"<<noisep.first<<std::endl;
00152 roiImg = (roiImg - noisep.first);
00153 normalize(roiImg);
00154 cv::rectangle(org,roi,cv::Scalar(255,2552,255),1);
00155 }
00156 }
00157 double min,max;
00158 cv::minMaxLoc(org,&min,&max);
00159 cv::imshow("Normalized2",disp(org,1));
00160
00161
00162 org = cv_ptr->image.clone();
00163
00164 for(size_t i=colSpan/2; i<org.cols; i+=colSpan)
00165 {
00166 for (size_t j=rowSpan/2; j<org.rows; j+=rowSpan)
00167 {
00168 cv::Rect roi(i-colSpan/2,j-rowSpan/2,colSpan,rowSpan);
00169 cv::Mat roiImg(org,roi);
00170
00171
00172
00173 roiImg = (roiImg - meanM.at<float>(i,j))*stdM.at<float>(i,j);
00174
00175 }
00176 }
00177
00178 cv::imshow("Normalized3",disp(org,512));
00179
00180 cv::waitKey(10);
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 }
00200
00201 void callback2(const aidnav_msgs::MBSonarConstPtr& image)
00202 {
00203 ROS_INFO("Received image: %d x %d x 4 = %d",image->image.width, image->image.height,image->image.data.size());
00204 ROS_INFO("Elapsed time: %f",(ros::Time::now() - last).toSec());
00205 last = ros::Time::now();
00206
00207 cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvCopy(image->image, sensor_msgs::image_encodings::BGRA8);
00208
00209 cv::imshow("SonarColor",cv_ptr->image);
00210 cv::waitKey(10);
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 }
00230
00231 int main(int argc, char* argv[])
00232 try
00233 {
00234
00235 ros::init(argc,argv,"bv_monitor");
00236 ros::NodeHandle nhandle;
00237
00238
00239 std::string topicName("bvsonar_img");
00240 std::string topicName2("bvsonar_cimg");
00241 int bufferSize(1),rate(10);
00242 ros::NodeHandle phandle("~");
00243 phandle.param("TopicName",topicName,topicName);
00244 phandle.param("BufferSize",bufferSize,bufferSize);
00245 phandle.param("Rate",rate,rate);
00246
00247
00248 ros::Subscriber imageTopic = nhandle.subscribe(topicName,bufferSize,callback);
00249 ros::Subscriber imageTopic2 = nhandle.subscribe(topicName2,bufferSize,callback2);
00250
00251 cv::namedWindow("Original",0);
00252 cv::namedWindow("Normalized",0);
00253
00254 ros::spin();
00255 return 0;
00256 }
00257 catch (std::exception& e)
00258 {
00259 std::cerr<<e.what()<<std::endl;
00260 }
00261
00262
00263