Go to the documentation of this file.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 #include <ros/ros.h>
00031 #include <swri_image_util/blend_images_util.h>
00032
00033 namespace swri_image_util
00034 {
00035 void blendImages(
00036 const cv::Mat& base_image,
00037 const cv::Mat& top_image,
00038 const double alpha,
00039 cv::Mat& dest_image)
00040 {
00041
00042
00043 if ((base_image.rows != top_image.rows)
00044 || (base_image.cols != top_image.cols)
00045 || (base_image.rows != dest_image.rows)
00046 || (base_image.cols != dest_image.cols))
00047 {
00048 ROS_ERROR("Images to blend had incorrect shapes");
00049 return;
00050 }
00051
00052
00053 if ((base_image.type() != top_image.type())
00054 || (base_image.type() != dest_image.type()))
00055 {
00056 ROS_ERROR("Images to blend must have the same type");
00057 return;
00058 }
00059
00060
00061
00062 if ((alpha < 0.0) || (alpha > 1.0))
00063 {
00064 ROS_ERROR("Alpha value must be in the range [0, 1]");
00065 return;
00066 }
00067
00068 cv::addWeighted(base_image, 1.0 - alpha, top_image, alpha, 0, dest_image);
00069 }
00070
00071 void blendImages(
00072 const cv::Mat& base_image,
00073 const cv::Mat& top_image,
00074 const double alpha,
00075 const cv::Scalar mask_color,
00076 cv::Mat& dest_image)
00077 {
00078
00079
00080 if ((base_image.rows != top_image.rows)
00081 || (base_image.cols != top_image.cols)
00082 || (base_image.rows != dest_image.rows)
00083 || (base_image.cols != dest_image.cols))
00084 {
00085 ROS_ERROR("Images to blend had incorrect shapes");
00086 return;
00087 }
00088
00089
00090 if ((base_image.type() != top_image.type())
00091 || (base_image.type() != dest_image.type()))
00092 {
00093 ROS_ERROR("Images to blend must have the same type");
00094 return;
00095 }
00096
00097
00098
00099 if ((alpha < 0.0) || (alpha > 1.0))
00100 {
00101 ROS_ERROR("Alpha value must be in the range [0, 1]");
00102 return;
00103 }
00104
00105
00106
00107 cv::Size size = top_image.size();
00108 int type = top_image.type();
00109
00110
00111 cv::Mat mask = cv::Mat::zeros(size, type);
00112 cv::inRange(top_image, mask_color, mask_color, mask);
00113
00114
00115 cv::Mat mask_inverse = cv::Mat::zeros(size, type);
00116 cv::bitwise_not(mask, mask_inverse);
00117
00118
00119 cv::Mat blended_top = cv::Mat::zeros(size, type);
00120 cv::addWeighted(base_image, 1.0 - alpha, top_image, alpha, 0, blended_top);
00121
00122
00123
00124 cv::Mat masked_top = cv::Mat::zeros(size, type);
00125 cv::bitwise_and(blended_top, blended_top, masked_top, mask_inverse);
00126
00127
00128 cv::Mat masked_base = cv::Mat::zeros(size, type);
00129 cv::bitwise_and(base_image, base_image, masked_base, mask);
00130
00131
00132 cv::add(masked_base, masked_top, dest_image);
00133 }
00134 }