bv_monitor.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, LABUST, UNIZG-FER
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 LABUST 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 #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         //frame.convertTo(nimg,CV_32FC1);
00061         //nimg /= 65536;
00062         cv::meanStdDev(nimg,mean,std);
00063         //std::cout<<"Mean:"<<mean<<","<<std<<std::endl;
00064         nimg = (nimg - mean.val[0])*std.val[0];
00065 
00066         cv::minMaxLoc(nimg,&min,&max);
00067         //std::cout<<"Max:"<<max<<std::endl;
00068 
00069         //cv::imshow("Normalized",disp(nimg,512));
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         //cv::imwrite("high_res.bmp",cv_ptr->image,);
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                         //cv::rectangle(org,roi,cv::Scalar(255,2552,255),1);
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                         //std::cout<<"The ROI mean:"<<noisep.first<<std::endl;
00132                         //roiImg = roiImg - noisep.first;
00133                         //normalize(roiImg);
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                         //cv::rectangle(org,roi,cv::Scalar(255,2552,255),1);
00171                         //std::pair<float, float> noisep = noise_estimate(meanM, stdM, i,j, colSpan, rowSpan);
00172                         //std::cout<<"The ROI mean:"<<noisep.first<<std::endl;
00173                         roiImg = (roiImg - meanM.at<float>(i,j))*stdM.at<float>(i,j);
00174                         //normalize(roiImg);
00175                 }
00176         }
00177 
00178   cv::imshow("Normalized3",disp(org,512));
00179 
00180         cv::waitKey(10);
00181 
00182 
00183         /*ros::NodeHandle nh;
00184         ros::ServiceClient client = nh.serviceClient<labust_bv::SetRange>("SetRange");
00185         labust_bv::SetRange service;
00186 
00187         service.request.stop = 15;
00188         service.request.start = 5;
00189 
00190         if (client.call(service))
00191         {
00192                 ROS_INFO("Service ok.");
00193         }
00194         else
00195         {
00196                 ROS_INFO("Service failed.");
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         /*ros::NodeHandle nh;
00214         ros::ServiceClient client = nh.serviceClient<labust_bv::SetRange>("SetRange");
00215         labust_bv::SetRange service;
00216 
00217         service.request.stop = 15;
00218         service.request.start = 5;
00219 
00220         if (client.call(service))
00221         {
00222                 ROS_INFO("Service ok.");
00223         }
00224         else
00225         {
00226                 ROS_INFO("Service failed.");
00227         }
00228         */
00229 }
00230 
00231 int main(int argc, char* argv[])
00232 try
00233 {
00234         //Setup ROS
00235         ros::init(argc,argv,"bv_monitor");
00236         ros::NodeHandle nhandle;
00237 
00238         //Configure this node
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         //Create topic subscription
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 


target_detector
Author(s): Gyula Nagy
autogenerated on Fri Feb 7 2014 11:36:05