File size: 8,360 Bytes
be94e5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
#include <opencv2/imgproc.hpp>
#include <opencv2/gapi/render/render.hpp> // Kernel API's
#include "api/render_ocv.hpp"
#include "backends/render/ft_render.hpp"
namespace cv
{
namespace gapi
{
namespace wip
{
namespace draw
{
// FIXME Support `decim` mosaic parameter
inline void mosaic(cv::Mat& mat, const cv::Rect &rect, int cellSz)
{
cv::Rect mat_rect(0, 0, mat.cols, mat.rows);
auto intersection = mat_rect & rect;
cv::Mat msc_roi = mat(intersection);
bool has_crop_x = false;
bool has_crop_y = false;
int cols = msc_roi.cols;
int rows = msc_roi.rows;
if (msc_roi.cols % cellSz != 0)
{
has_crop_x = true;
cols -= msc_roi.cols % cellSz;
}
if (msc_roi.rows % cellSz != 0)
{
has_crop_y = true;
rows -= msc_roi.rows % cellSz;
}
cv::Mat cell_roi;
for(int i = 0; i < rows; i += cellSz )
{
for(int j = 0; j < cols; j += cellSz)
{
cell_roi = msc_roi(cv::Rect(j, i, cellSz, cellSz));
cell_roi = cv::mean(cell_roi);
}
if (has_crop_x)
{
cell_roi = msc_roi(cv::Rect(cols, i, msc_roi.cols - cols, cellSz));
cell_roi = cv::mean(cell_roi);
}
}
if (has_crop_y)
{
for(int j = 0; j < cols; j += cellSz)
{
cell_roi = msc_roi(cv::Rect(j, rows, cellSz, msc_roi.rows - rows));
cell_roi = cv::mean(cell_roi);
}
if (has_crop_x)
{
cell_roi = msc_roi(cv::Rect(cols, rows, msc_roi.cols - cols, msc_roi.rows - rows));
cell_roi = cv::mean(cell_roi);
}
}
}
inline void blendImage(const cv::Mat& img,
const cv::Mat& alpha,
const cv::Point& org,
cv::Mat background)
{
GAPI_Assert(alpha.type() == CV_32FC1);
GAPI_Assert(background.channels() == 3u);
cv::Mat roi = background(cv::Rect(org, img.size()));
cv::Mat img32f_w;
cv::merge(std::vector<cv::Mat>(3, alpha), img32f_w);
cv::Mat roi32f_w(roi.size(), CV_32FC3, cv::Scalar::all(1.0));
roi32f_w -= img32f_w;
cv::Mat img32f, roi32f;
if (img.type() == CV_32FC3) {
img.copyTo(img32f);
} else {
img.convertTo(img32f, CV_32F, 1.0/255);
}
roi.convertTo(roi32f, CV_32F, 1.0/255);
cv::multiply(img32f, img32f_w, img32f);
cv::multiply(roi32f, roi32f_w, roi32f);
roi32f += img32f;
roi32f.convertTo(roi, CV_8U, 255.0);
}
inline void blendTextMask(cv::Mat& img,
cv::Mat& mask,
const cv::Point& tl,
const cv::Scalar& color)
{
mask.convertTo(mask, CV_32FC1, 1 / 255.0);
cv::Mat color_mask;
cv::merge(std::vector<cv::Mat>(3, mask), color_mask);
cv::Scalar color32f = color / 255.0;
cv::multiply(color_mask, color32f, color_mask);
blendImage(color_mask, mask, tl, img);
}
inline void poly(cv::Mat& mat,
const cv::gapi::wip::draw::Poly& pp)
{
std::vector<std::vector<cv::Point>> points{pp.points};
cv::fillPoly(mat, points, pp.color, pp.lt, pp.shift);
}
struct BGR2YUVConverter
{
cv::Scalar cvtColor(const cv::Scalar& bgr) const
{
double y = bgr[2] * 0.299000 + bgr[1] * 0.587000 + bgr[0] * 0.114000;
double u = bgr[2] * -0.168736 + bgr[1] * -0.331264 + bgr[0] * 0.500000 + 128;
double v = bgr[2] * 0.500000 + bgr[1] * -0.418688 + bgr[0] * -0.081312 + 128;
return {y, u, v};
}
void cvtImg(const cv::Mat& in, cv::Mat& out) { cv::cvtColor(in, out, cv::COLOR_BGR2YUV); }
};
struct EmptyConverter
{
cv::Scalar cvtColor(const cv::Scalar& bgr) const { return bgr; }
void cvtImg(const cv::Mat& in, cv::Mat& out) const { out = in; }
};
// FIXME util::visitor ?
template <typename ColorConverter>
void drawPrimitivesOCV(cv::Mat& in,
const cv::gapi::wip::draw::Prims& prims,
std::shared_ptr<cv::gapi::wip::draw::FTTextRender>& ftpr)
{
using namespace cv::gapi::wip::draw;
ColorConverter converter;
for (const auto &p : prims)
{
switch (p.index())
{
case Prim::index_of<Rect>():
{
const auto& rp = cv::util::get<Rect>(p);
const auto color = converter.cvtColor(rp.color);
cv::rectangle(in, rp.rect, color, rp.thick, rp.lt, rp.shift);
break;
}
case Prim::index_of<Text>():
{
auto tp = cv::util::get<Text>(p);
tp.color = converter.cvtColor(tp.color);
cv::putText(in, tp.text, tp.org, tp.ff, tp.fs, tp.color, tp.thick, tp.lt, tp.bottom_left_origin);
break;
}
case Prim::index_of<FText>():
{
const auto& ftp = cv::util::get<FText>(p);
const auto color = converter.cvtColor(ftp.color);
GAPI_Assert(ftpr && "You must pass cv::gapi::wip::draw::freetype_font"
" to the graph compile arguments");
int baseline = 0;
auto size = ftpr->getTextSize(ftp.text, ftp.fh, &baseline);
// Allocate mask outside
cv::Mat mask(size, CV_8UC1, cv::Scalar::all(0));
// Org it's bottom left position for baseline
cv::Point org(0, mask.rows - baseline);
ftpr->putText(mask, ftp.text, org, ftp.fh);
// Org is bottom left point, transform it to top left point for blendImage
cv::Point tl(ftp.org.x, ftp.org.y - mask.size().height + baseline);
blendTextMask(in, mask, tl, color);
break;
}
case Prim::index_of<Circle>():
{
const auto& cp = cv::util::get<Circle>(p);
const auto color = converter.cvtColor(cp.color);
cv::circle(in, cp.center, cp.radius, color, cp.thick, cp.lt, cp.shift);
break;
}
case Prim::index_of<Line>():
{
const auto& lp = cv::util::get<Line>(p);
const auto color = converter.cvtColor(lp.color);
cv::line(in, lp.pt1, lp.pt2, color, lp.thick, lp.lt, lp.shift);
break;
}
case Prim::index_of<Mosaic>():
{
const auto& mp = cv::util::get<Mosaic>(p);
GAPI_Assert(mp.decim == 0 && "Only decim = 0 supported now");
mosaic(in, mp.mos, mp.cellSz);
break;
}
case Prim::index_of<Image>():
{
const auto& ip = cv::util::get<Image>(p);
cv::Mat img;
converter.cvtImg(ip.img, img);
img.convertTo(img, CV_32FC1, 1.0 / 255);
blendImage(img, ip.alpha, ip.org, in);
break;
}
case Prim::index_of<Poly>():
{
auto pp = cv::util::get<Poly>(p);
pp.color = converter.cvtColor(pp.color);
poly(in, pp);
break;
}
default: cv::util::throw_error(std::logic_error("Unsupported draw operation"));
}
}
}
void drawPrimitivesOCVBGR(cv::Mat &in,
const cv::gapi::wip::draw::Prims &prims,
std::shared_ptr<cv::gapi::wip::draw::FTTextRender> &ftpr)
{
drawPrimitivesOCV<EmptyConverter>(in, prims, ftpr);
}
void drawPrimitivesOCVYUV(cv::Mat &in,
const cv::gapi::wip::draw::Prims &prims,
std::shared_ptr<cv::gapi::wip::draw::FTTextRender> &ftpr)
{
drawPrimitivesOCV<BGR2YUVConverter>(in, prims, ftpr);
}
} // namespace draw
} // namespace wip
} // namespace gapi
} // namespace cv
|