draw_log_polar_hist.cpp
Go to the documentation of this file.
00001 #include <sstream>
00002 #include <iostream>
00003 #include <fstream>
00004 #include <tabletop_pushing/shape_features.h>
00005 #include <ros/ros.h>
00006 #include <pcl16/point_cloud.h>
00007 #include <pcl16/point_types.h>
00008 #include <pcl16/io/pcd_io.h>
00009 #include <pcl16_ros/transforms.h>
00010 #include <pcl16/ros/conversions.h>
00011 #include <pcl16/common/pca.h>
00012 #include <tabletop_pushing/VisFeedbackPushTrackingAction.h>
00013 #include <tabletop_pushing/point_cloud_segmentation.h>
00014 #include <cpl_visual_features/helpers.h>
00015 #include <cpl_visual_features/features/kernels.h>
00016 #include <time.h> // for srand(time(NULL))
00017 
00018 using namespace cpl_visual_features;
00019 using namespace tabletop_pushing;
00020 
00021 #define XY_RES 0.001
00022 
00023 inline int objLocToIdx(double val, double min_val, double max_val)
00024 {
00025   return round((val-min_val)/XY_RES);
00026 }
00027 
00028 cv::Mat makeHistogramImage(ShapeDescriptor histogram, int n_x_bins, int n_y_bins, int bin_width_pixels)
00029 {
00030   cv::Mat hist_img(n_y_bins*bin_width_pixels+1, n_x_bins*bin_width_pixels+1, CV_8UC1, cv::Scalar(255));
00031   int max_count = 10;
00032   for (int i = 0; i < histogram.size(); ++i)
00033   {
00034     if (histogram[i] == 0) continue;
00035     int pix_val = (1 - histogram[i]/max_count)*255;
00036     ROS_INFO_STREAM("Pix val: " << pix_val);
00037     int start_x = (i%n_x_bins)*bin_width_pixels;
00038     int start_y = (i/n_x_bins)*bin_width_pixels;;
00039     ROS_INFO_STREAM("(start_x, start_y): (" << start_x << ", " << start_y << ")");
00040     for (int x = start_x; x < start_x+bin_width_pixels; ++x)
00041     {
00042       for (int y = start_y; y < start_y+bin_width_pixels; ++y)
00043       {
00044         hist_img.at<uchar>(y, x) = pix_val;
00045       }
00046     }
00047   }
00048   ROS_INFO_STREAM("Drawing columns");
00049   // Draw hist bin columns
00050   for (int i = 0; i <= n_x_bins; ++i)
00051   {
00052     int x = i*bin_width_pixels;
00053     for (int y = 0; y < hist_img.rows; ++y)
00054     {
00055       hist_img.at<uchar>(y,x) = 0;
00056     }
00057   }
00058   ROS_INFO_STREAM("Drawing rows");
00059   // Draw hist bin rows
00060   for (int i = 0; i <= n_y_bins; ++i)
00061   {
00062     int y = i*bin_width_pixels;
00063     for (int x = 0; x < hist_img.cols; ++x)
00064     {
00065       hist_img.at<uchar>(y,x) = 0;
00066     }
00067   }
00068   // cv::imshow("local_hist_img", hist_img);
00069   // cv::waitKey();
00070   ROS_INFO_STREAM("Returning");
00071   return hist_img;
00072 }
00073 
00074 int main(int argc, char** argv)
00075 {
00076   double max_y = 0.5;
00077   double min_y = -0.5;
00078   double max_x = 0.5;
00079   double min_x = -0.5;
00080 
00081 
00082   int max_dist = 0.5;
00083   int max_radius = objLocToIdx(max_dist, min_x, max_x);
00084   int radius_bins = 6; // actually number of radius separators (including 0)
00085   int theta_bins = 12;
00086 
00087   int xy_pad = 15;
00088   cv::Mat hist_img(max_radius*2+xy_pad*2, max_radius*2+xy_pad*2, CV_8UC3, cv::Scalar(255, 255, 255));
00089 
00090   cv::Point img_center(hist_img.rows/2, hist_img.cols/2);
00091   // Draw log spaced circles
00092   for (int r = 0; r < radius_bins; ++r)
00093   {
00094     float cur_radius = max_radius-log(r+1)/log(float(radius_bins))*max_radius;
00095     cv::circle(hist_img, img_center, cur_radius, cv::Scalar(255, 0 ,0), 3);
00096   }
00097 
00098   // Draw polar dividing lines
00099   for (int t = 0; t < theta_bins; ++t)
00100   {
00101     float cur_angle = 2.0*M_PI*(float(t)/theta_bins)-M_PI;
00102     cv::Point end_point(cos(cur_angle)*max_radius, sin(cur_angle)*max_radius);
00103     cv::line(hist_img, img_center, end_point+img_center, cv::Scalar(255, 0, 0), 3);
00104   }
00105 
00106   cv::imshow("log-polar hist", hist_img);
00107   cv::imwrite("/home/thermans/Desktop/log_polar_hist.png", hist_img);
00108   ShapeDescriptor sd;
00109   for (int r = 0; r < radius_bins; ++r)
00110   {
00111     for (int t = 0; t < theta_bins; ++t)
00112     {
00113       int s = 0;
00114       if (t == 0)
00115       {
00116         if (r == 4)
00117         {
00118           s = 2;
00119         }
00120         else if (r == 3)
00121         {
00122           s = 10;
00123         }
00124       }
00125       else if (t == 1)
00126       {
00127         if (r == 3)
00128         {
00129           s = 6;
00130         }
00131         if (r == 2)
00132         {
00133           s = 2;
00134         }
00135       }
00136       else if (t == 2)
00137       {
00138         if (r == 2)
00139         {
00140           s = 5;
00141         }
00142         else if (r == 1)
00143         {
00144           s = 4;
00145         }
00146       }
00147       else if (t == 3)
00148       {
00149         if (r == 1)
00150         {
00151           s = 1;
00152         }
00153         if (r == 0)
00154         {
00155           s = 1;
00156         }
00157       }
00158       else if (t == 4)
00159       {
00160         if (r == 0)
00161         {
00162           s = 1;
00163         }
00164       }
00165       else if (t == 9)
00166       {
00167         if (r == 0)
00168         {
00169           s = 2;
00170         }
00171       }
00172       else if (t == 10)
00173       {
00174         if (r == 0)
00175         {
00176           s = 2;
00177         }
00178         else if (r == 1)
00179         {
00180           s = 3;
00181         }
00182         else if (r == 2)
00183         {
00184           s = 4;
00185         }
00186         else if (r == 3)
00187         {
00188           s = 6;
00189         }
00190       }
00191       else if (t == 11)
00192       {
00193         if (r == 3)
00194         {
00195           s = 10;
00196         }
00197         if (r == 4)
00198         {
00199           s = 2;
00200         }
00201       }
00202       sd.push_back(s);
00203       // ROS_INFO_STREAM("(" << r << ", " << t << ") : " << s);
00204     }
00205   }
00206   cv::Mat rect_hist_img = makeHistogramImage(sd, theta_bins, radius_bins, 10);
00207   cv::imshow("rect_hist", rect_hist_img);
00208   cv::imwrite("/home/thermans/Desktop/rect_log_polar_hist.png", rect_hist_img);
00209   cv::waitKey();
00210   return 0;
00211 }


tabletop_pushing
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:59:44