edge_estimation_2d.hpp
Go to the documentation of this file.
00001 
00063 #ifndef __IMPL_EDGE_ESTIMATION_2D_H__
00064 #define __IMPL_EDGE_ESTIMATION_2D_H__
00065 
00066 #include "cob_3d_features/edge_estimation_2d.h"
00067 //#include "highgui.h"
00068 
00069 template <typename PointInT, typename PointOutT> void
00070 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::getColorImage(
00071   cv::Mat &color_image)
00072 {
00073   color_image.create(input_->height, input_->width, CV_8UC3);
00074   unsigned char* c_ptr = 0;
00075   int pc_pt_idx=0;
00076   for (int row = 0; row < color_image.rows; row++)
00077   {
00078     c_ptr = color_image.ptr<unsigned char>(row);
00079     for (int col = 0; col < color_image.cols; col++, pc_pt_idx++)
00080     {
00081       memcpy(&c_ptr[3*col], &input_->points[pc_pt_idx].rgb, 3*sizeof(unsigned char));
00082     }
00083   }
00084 }
00085 
00086 template <typename PointInT, typename PointOutT> void
00087 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::getRangeImage(
00088   cv::Mat &range_image, const float &th_min=0.0f, const float &th_max=0.0f)
00089 {
00090   range_image.create(input_->height, input_->width, CV_32FC1);
00091   float* r_ptr = 0;
00092   int pc_pt_idx=0;
00093   for (int row = 0; row < range_image.rows; row++)
00094   {
00095     r_ptr = range_image.ptr<float>(row);
00096     for (int col = 0; col < range_image.cols; col++, pc_pt_idx++)
00097     {
00098       memcpy(&r_ptr[col], &input_->points[pc_pt_idx].z, sizeof(float));
00099     }
00100   }
00101   if(th_max != 0.0)
00102     cv::threshold(range_image, range_image, th_max, th_max, cv::THRESH_TRUNC);
00103   if(th_min != 0.0)
00104   {
00105     range_image = range_image - th_min;
00106     cv::threshold(range_image, range_image, 0, 0, cv::THRESH_TOZERO);
00107   }
00108   cv::normalize(range_image, range_image, 0, 1, cv::NORM_MINMAX);
00109 }
00110 
00111 template <typename PointInT, typename PointOutT> void
00112 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::extractEdgesSobel(
00113   cv::Mat &input_image,
00114   cv::Mat &sobel_image)
00115 {
00116   cv::Mat sobel_res;
00117   if( input_image.type() == CV_8UC3)
00118   {
00119     std::vector<cv::Mat> image_channels;
00120     cv::split(input_image, image_channels);
00121     std::vector<cv::Mat> tmp(3);
00122     for (int i = 0; i<3; i++)
00123     {
00124       cv::Mat tmp_x, tmp_y;
00125       cv::Sobel(image_channels[i], tmp_x, CV_32FC1, 1, 0, 3);
00126       tmp_x = abs(tmp_x);
00127       cv::Sobel(image_channels[i], tmp_y, CV_32FC1, 0, 1, 3);
00128       tmp_y = abs(tmp_y);
00129       tmp[i] = tmp_x + tmp_y;
00130     }
00131     sobel_res = tmp[0] + tmp[1] + tmp[2];
00132   }
00133   else if(input_image.type() == CV_8UC1 || input_image.type() == CV_32FC1)
00134   {
00135     cv::Mat tmp_x, tmp_y;
00136     cv::Sobel(input_image, tmp_x, CV_32FC1, 1, 0, 3);
00137     tmp_x = abs(tmp_x);
00138     cv::Sobel(input_image, tmp_y, CV_32FC1, 0, 1, 3);
00139     tmp_y = abs(tmp_y);
00140     sobel_res = tmp_x + tmp_y;
00141   }
00142   /*cv::Mat test2(cv::Size(640,480),CV_8UC1);
00143   cv::convertScaleAbs(sobel_res, test2);
00144   cv::imshow("test", test2);
00145   cv::waitKey();*/
00146 
00147   cv::normalize(sobel_res, sobel_image, 0, 1, cv::NORM_MINMAX);
00148 }
00149 
00150 template <typename PointInT, typename PointOutT> void
00151 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::extractEdgesLaPlace(
00152   cv::Mat &input_image,
00153   cv::Mat &laplace_image)
00154 {
00155   cv::Mat laplace_res;
00156   if( input_image.type() == CV_8UC3)
00157   {
00158     std::vector<cv::Mat> image_channels;
00159     cv::split(input_image, image_channels);
00160     std::vector<cv::Mat> laplace_tmp(3);
00161     for (int i=0; i<3; i++)
00162     {
00163       cv::Laplacian(image_channels[i], laplace_tmp[i], CV_32FC1, 1);
00164       laplace_tmp[i] = abs(laplace_tmp[i]);
00165     }
00166     laplace_res = laplace_tmp[0] + laplace_tmp[1] + laplace_tmp[2];
00167   }
00168   else if( input_image.type() == CV_8UC1 || input_image.type() == CV_32FC1)
00169   {
00170     cv::Laplacian(input_image, laplace_res, CV_32FC1, 1);
00171     laplace_res = abs(laplace_res);
00172   }
00173   cv::normalize(laplace_res, laplace_res, 0, 1, cv::NORM_MINMAX);
00174   /*cv::threshold(laplace_res, laplace_res, 0.1, 1, cv::THRESH_TOZERO);
00175   laplace_res *= 2;
00176   cv::threshold(laplace_res, laplace_res, 1, 1, cv::THRESH_TRUNC);*/
00177   laplace_image = laplace_res;
00178 }
00179 
00180 template <typename PointInT, typename PointOutT> void
00181 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::computeEdges(
00182   PointCloudOut &output)
00183 {
00184   cv::Mat color_image(input_->height, input_->width, CV_8UC3);
00185   getColorImage(color_image);
00186   cv::Mat img_sobel, img_laplace, img_combined;
00187   extractEdgesSobel(color_image, img_sobel);
00188   //cv::Mat test2(cv::Size(640,480),CV_8UC1);
00189   //cv::convertScaleAbs(img_sobel, test2);
00190   //cv::imshow("test", img_sobel);
00191   //cv::waitKey();
00192   extractEdgesLaPlace(color_image, img_laplace);
00193 
00194   img_combined = img_sobel + img_laplace;
00195   cv::normalize(img_combined, img_combined, 0, 1, cv::NORM_MINMAX);
00196   //cv::threshold(img_combined, img_combined, 1, 1, cv::THRESH_TRUNC);
00197 
00198   output.height = input_->height;
00199   output.width = input_->width;
00200   output.points.resize(output.height * output.width);
00201 
00202   for (unsigned int i=0; i < output.height; i++)
00203   {
00204     for (unsigned int j=0; j < output.width; j++)
00205     {
00206       output.points[i*output.width+j].strength = img_combined.at<float>(i,j);
00207     }
00208   }
00209 }
00210 
00211 template <typename PointInT, typename PointOutT> void
00212 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::computeEdges(
00213   cv::Mat &sobel_out, cv::Mat &laplace_out, cv::Mat &combined_out)
00214 {
00215   cv::Mat color_image;
00216   getColorImage(color_image);
00217   //std::vector<cv::Mat> img_channels(3);
00218   //cv::split(color_image, img_channels);
00219   cv::Mat img_sobel, img_laplace, img_combined;
00220   extractEdgesSobel(color_image, img_sobel);
00221   extractEdgesLaPlace(color_image, img_laplace);
00222 
00223   img_combined = img_sobel + img_laplace;
00224   cv::normalize(img_combined, img_combined, 0, 1, cv::NORM_MINMAX);
00225   //cv::threshold(img_combined, img_combined, 1, 1, cv::THRESH_TRUNC);
00226 
00227   sobel_out = img_sobel;
00228   laplace_out = img_laplace;
00229   combined_out = img_combined;
00230 }
00231 
00232 template <typename PointInT, typename PointOutT> void
00233 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::computeEdgesFromRange(
00234   PointCloudOut &output)
00235 {
00236   cv::Mat range_image, img_sobel, img_laplace, img_combined;
00237   getRangeImage(range_image);
00238   extractEdgesSobel(range_image, img_sobel);
00239   extractEdgesLaPlace(range_image, img_laplace);
00240 
00241   img_combined = img_sobel + img_laplace;
00242   cv::threshold(img_combined, img_combined, 1, 1, cv::THRESH_TRUNC);
00243 
00244   output.height = input_->height;
00245   output.width = input_->width;
00246   output.points.resize(output.height * output.width);
00247 
00248   for (unsigned int i=0; i < output.height; i++)
00249   {
00250     for (unsigned int j=0; j < output.width; j++)
00251     {
00252       output.points[i*output.width+j].strength = img_combined.at<float>(i,j);
00253     }
00254   }
00255 }
00256 
00257 template <typename PointInT, typename PointOutT> void
00258 cob_3d_features::EdgeEstimation2D<PointInT,PointOutT>::computeEdgesFromRange(
00259   cv::Mat &sobel_out, cv::Mat &laplace_out, cv::Mat &combined_out)
00260 {
00261   cv::Mat range_image, img_sobel, img_laplace, img_combined;
00262   getRangeImage(range_image);
00263   extractEdgesSobel(range_image, img_sobel);
00264   extractEdgesLaPlace(range_image, img_laplace);
00265 
00266   img_combined = img_sobel + img_laplace;
00267   cv::threshold(img_combined, img_combined, 1, 1, cv::THRESH_TRUNC);
00268 
00269   sobel_out = img_sobel;
00270   laplace_out = img_laplace;
00271   combined_out = img_combined;
00272 }
00273 
00274 #define PCL_INSTANTIATE_EdgeEstimation2D(T,OutT) template class PCL_EXPORTS cob_3d_features::EdgeEstimation2D<T,OutT>;
00275 
00276 #endif


cob_3d_features
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:02:26