\r
#include "precomp.hpp"\r
\r
-using namespace std;\r
-\r
namespace cv {\r
namespace detail {\r
\r
-Ptr<SeamFinder> SeamFinder::createDefault(int type)\r
-{\r
- if (type == NO)\r
- return new NoSeamFinder();\r
- if (type == VORONOI)\r
- return new VoronoiSeamFinder();\r
- if (type == GC_COLOR)\r
- return new GraphCutSeamFinder(GraphCutSeamFinder::COST_COLOR);\r
- if (type == GC_COLOR_GRAD)\r
- return new GraphCutSeamFinder(GraphCutSeamFinder::COST_COLOR_GRAD);\r
- CV_Error(CV_StsBadArg, "unsupported seam finding method");\r
- return NULL;\r
-}\r
-\r
-\r
void PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,\r
- vector<Mat> &masks)\r
+ vector<Mat> &masks)\r
{\r
LOGLN("Finding seams...");\r
int64 t = getTickCount();\r
\r
\r
void GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Point> &corners,\r
- vector<Mat> &masks)\r
+ vector<Mat> &masks)\r
{\r
// Compute gradients\r
dx_.resize(src.size());\r
\r
\r
void GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2,\r
- const Mat &mask1, const Mat &mask2, GCGraph<float> &graph)\r
+ const Mat &mask1, const Mat &mask2, GCGraph<float> &graph)\r
{\r
const Size img_size = img1.size();\r
\r
impl_->find(src, corners, masks);\r
}\r
\r
+\r
+#ifndef ANDROID\r
+void GraphCutSeamFinderGpu::find(const vector<Mat> &src, const vector<Point> &corners,\r
+ vector<Mat> &masks)\r
+{\r
+ // Compute gradients\r
+ dx_.resize(src.size());\r
+ dy_.resize(src.size());\r
+ Mat dx, dy;\r
+ for (size_t i = 0; i < src.size(); ++i)\r
+ {\r
+ CV_Assert(src[i].channels() == 3);\r
+ Sobel(src[i], dx, CV_32F, 1, 0);\r
+ Sobel(src[i], dy, CV_32F, 0, 1);\r
+ dx_[i].create(src[i].size(), CV_32F);\r
+ dy_[i].create(src[i].size(), CV_32F);\r
+ for (int y = 0; y < src[i].rows; ++y)\r
+ {\r
+ const Point3f* dx_row = dx.ptr<Point3f>(y);\r
+ const Point3f* dy_row = dy.ptr<Point3f>(y);\r
+ float* dx_row_ = dx_[i].ptr<float>(y);\r
+ float* dy_row_ = dy_[i].ptr<float>(y);\r
+ for (int x = 0; x < src[i].cols; ++x)\r
+ {\r
+ dx_row_[x] = normL2(dx_row[x]);\r
+ dy_row_[x] = normL2(dy_row[x]);\r
+ }\r
+ }\r
+ }\r
+ PairwiseSeamFinder::find(src, corners, masks);\r
+}\r
+\r
+\r
+void GraphCutSeamFinderGpu::findInPair(size_t first, size_t second, Rect roi)\r
+{\r
+ Mat img1 = images_[first], img2 = images_[second];\r
+ Mat dx1 = dx_[first], dx2 = dx_[second];\r
+ Mat dy1 = dy_[first], dy2 = dy_[second];\r
+ Mat mask1 = masks_[first], mask2 = masks_[second];\r
+ Point tl1 = corners_[first], tl2 = corners_[second];\r
+\r
+ const int gap = 10;\r
+ Mat subimg1(roi.height + 2 * gap, roi.width + 2 * gap, CV_32FC3);\r
+ Mat subimg2(roi.height + 2 * gap, roi.width + 2 * gap, CV_32FC3);\r
+ Mat submask1(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U);\r
+ Mat submask2(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U);\r
+ Mat subdx1(roi.height + 2 * gap, roi.width + 2 * gap, CV_32F);\r
+ Mat subdy1(roi.height + 2 * gap, roi.width + 2 * gap, CV_32F);\r
+ Mat subdx2(roi.height + 2 * gap, roi.width + 2 * gap, CV_32F);\r
+ Mat subdy2(roi.height + 2 * gap, roi.width + 2 * gap, CV_32F);\r
+\r
+ // Cut subimages and submasks with some gap\r
+ for (int y = -gap; y < roi.height + gap; ++y)\r
+ {\r
+ for (int x = -gap; x < roi.width + gap; ++x)\r
+ {\r
+ int y1 = roi.y - tl1.y + y;\r
+ int x1 = roi.x - tl1.x + x;\r
+ if (y1 >= 0 && x1 >= 0 && y1 < img1.rows && x1 < img1.cols)\r
+ {\r
+ subimg1.at<Point3f>(y + gap, x + gap) = img1.at<Point3f>(y1, x1);\r
+ submask1.at<uchar>(y + gap, x + gap) = mask1.at<uchar>(y1, x1);\r
+ subdx1.at<float>(y + gap, x + gap) = dx1.at<float>(y1, x1);\r
+ subdy1.at<float>(y + gap, x + gap) = dy1.at<float>(y1, x1);\r
+ }\r
+ else\r
+ {\r
+ subimg1.at<Point3f>(y + gap, x + gap) = Point3f(0, 0, 0);\r
+ submask1.at<uchar>(y + gap, x + gap) = 0;\r
+ subdx1.at<float>(y + gap, x + gap) = 0.f;\r
+ subdy1.at<float>(y + gap, x + gap) = 0.f;\r
+ }\r
+\r
+ int y2 = roi.y - tl2.y + y;\r
+ int x2 = roi.x - tl2.x + x;\r
+ if (y2 >= 0 && x2 >= 0 && y2 < img2.rows && x2 < img2.cols)\r
+ {\r
+ subimg2.at<Point3f>(y + gap, x + gap) = img2.at<Point3f>(y2, x2);\r
+ submask2.at<uchar>(y + gap, x + gap) = mask2.at<uchar>(y2, x2);\r
+ subdx2.at<float>(y + gap, x + gap) = dx2.at<float>(y2, x2);\r
+ subdy2.at<float>(y + gap, x + gap) = dy2.at<float>(y2, x2);\r
+ }\r
+ else\r
+ {\r
+ subimg2.at<Point3f>(y + gap, x + gap) = Point3f(0, 0, 0);\r
+ submask2.at<uchar>(y + gap, x + gap) = 0;\r
+ subdx2.at<float>(y + gap, x + gap) = 0.f;\r
+ subdy2.at<float>(y + gap, x + gap) = 0.f;\r
+ }\r
+ }\r
+ }\r
+ \r
+ Mat terminals, leftT, rightT, top, bottom;\r
+\r
+ switch (cost_type_)\r
+ {\r
+ case GraphCutSeamFinder::COST_COLOR:\r
+ setGraphWeightsColor(subimg1, subimg2, submask1, submask2, \r
+ terminals, leftT, rightT, top, bottom);\r
+ break;\r
+ case GraphCutSeamFinder::COST_COLOR_GRAD:\r
+ setGraphWeightsColorGrad(subimg1, subimg2, subdx1, subdx2, subdy1, subdy2, \r
+ submask1, submask2, terminals, leftT, rightT, top, bottom);\r
+ break;\r
+ default:\r
+ CV_Error(CV_StsBadArg, "unsupported pixel similarity measure");\r
+ }\r
+\r
+ gpu::GpuMat terminals_d(terminals);\r
+ gpu::GpuMat leftT_d(leftT);\r
+ gpu::GpuMat rightT_d(rightT);\r
+ gpu::GpuMat top_d(top);\r
+ gpu::GpuMat bottom_d(bottom);\r
+ gpu::GpuMat labels_d, buf_d;\r
+\r
+ gpu::graphcut(terminals_d, leftT_d, rightT_d, top_d, bottom_d, labels_d, buf_d);\r
+\r
+ Mat_<uchar> labels = labels_d;\r
+ for (int y = 0; y < roi.height; ++y)\r
+ {\r
+ for (int x = 0; x < roi.width; ++x)\r
+ {\r
+ if (labels(y + gap, x + gap))\r
+ {\r
+ if (mask1.at<uchar>(roi.y - tl1.y + y, roi.x - tl1.x + x))\r
+ mask2.at<uchar>(roi.y - tl2.y + y, roi.x - tl2.x + x) = 0;\r
+ }\r
+ else\r
+ {\r
+ if (mask2.at<uchar>(roi.y - tl2.y + y, roi.x - tl2.x + x))\r
+ mask1.at<uchar>(roi.y - tl1.y + y, roi.x - tl1.x + x) = 0;\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+\r
+void GraphCutSeamFinderGpu::setGraphWeightsColor(const Mat &img1, const Mat &img2, const Mat &mask1, const Mat &mask2, \r
+ Mat &terminals, Mat &leftT, Mat &rightT, Mat &top, Mat &bottom)\r
+{\r
+ const Size img_size = img1.size();\r
+\r
+ terminals.create(img_size, CV_32S);\r
+ leftT.create(Size(img_size.height, img_size.width), CV_32S);\r
+ rightT.create(Size(img_size.height, img_size.width), CV_32S);\r
+ top.create(img_size, CV_32S);\r
+ bottom.create(img_size, CV_32S);\r
+\r
+ Mat_<int> terminals_(terminals);\r
+ Mat_<int> leftT_(leftT);\r
+ Mat_<int> rightT_(rightT);\r
+ Mat_<int> top_(top);\r
+ Mat_<int> bottom_(bottom);\r
+\r
+ // Set terminal weights\r
+ for (int y = 0; y < img_size.height; ++y)\r
+ {\r
+ for (int x = 0; x < img_size.width; ++x)\r
+ {\r
+ float source = mask1.at<uchar>(y, x) ? terminal_cost_ : 0.f;\r
+ float sink = mask2.at<uchar>(y, x) ? terminal_cost_ : 0.f;\r
+ terminals_(y, x) = saturate_cast<int>((source - sink) * 255.f);\r
+ }\r
+ }\r
+\r
+ // Set regular edge weights\r
+ const float weight_eps = 1.f;\r
+ for (int y = 0; y < img_size.height; ++y)\r
+ {\r
+ for (int x = 0; x < img_size.width; ++x)\r
+ {\r
+ if (x > 0)\r
+ {\r
+ float weight = normL2(img1.at<Point3f>(y, x - 1), img2.at<Point3f>(y, x - 1)) +\r
+ normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x)) +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y, x - 1) || !mask1.at<uchar>(y, x) ||\r
+ !mask2.at<uchar>(y, x - 1) || !mask2.at<uchar>(y, x))\r
+ weight += bad_region_penalty_;\r
+ leftT_(x, y) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ leftT_(x, y) = 0;\r
+\r
+ if (x < img_size.width - 1)\r
+ {\r
+ float weight = normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x)) +\r
+ normL2(img1.at<Point3f>(y, x + 1), img2.at<Point3f>(y, x + 1)) +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y, x) || !mask1.at<uchar>(y, x + 1) ||\r
+ !mask2.at<uchar>(y, x) || !mask2.at<uchar>(y, x + 1))\r
+ weight += bad_region_penalty_;\r
+ rightT_(x, y) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ rightT_(x, y) = 0;\r
+\r
+ if (y > 0)\r
+ {\r
+ float weight = normL2(img1.at<Point3f>(y - 1, x), img2.at<Point3f>(y - 1, x)) +\r
+ normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x)) +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y - 1, x) || !mask1.at<uchar>(y, x) ||\r
+ !mask2.at<uchar>(y - 1, x) || !mask2.at<uchar>(y, x))\r
+ weight += bad_region_penalty_;\r
+ top_(y, x) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ top_(y, x) = 0;\r
+\r
+ if (y < img_size.height - 1)\r
+ {\r
+ float weight = normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x)) +\r
+ normL2(img1.at<Point3f>(y + 1, x), img2.at<Point3f>(y + 1, x)) +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y, x) || !mask1.at<uchar>(y + 1, x) ||\r
+ !mask2.at<uchar>(y, x) || !mask2.at<uchar>(y + 1, x))\r
+ weight += bad_region_penalty_;\r
+ bottom_(y, x) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ bottom_(y, x) = 0;\r
+ }\r
+ }\r
+}\r
+\r
+\r
+void GraphCutSeamFinderGpu::setGraphWeightsColorGrad(\r
+ const Mat &img1, const Mat &img2, const Mat &dx1, const Mat &dx2,\r
+ const Mat &dy1, const Mat &dy2, const Mat &mask1, const Mat &mask2, \r
+ Mat &terminals, Mat &leftT, Mat &rightT, Mat &top, Mat &bottom)\r
+{\r
+ const Size img_size = img1.size();\r
+\r
+ terminals.create(img_size, CV_32S);\r
+ leftT.create(Size(img_size.height, img_size.width), CV_32S);\r
+ rightT.create(Size(img_size.height, img_size.width), CV_32S);\r
+ top.create(img_size, CV_32S);\r
+ bottom.create(img_size, CV_32S);\r
+\r
+ Mat_<int> terminals_(terminals);\r
+ Mat_<int> leftT_(leftT);\r
+ Mat_<int> rightT_(rightT);\r
+ Mat_<int> top_(top);\r
+ Mat_<int> bottom_(bottom);\r
+\r
+ // Set terminal weights\r
+ for (int y = 0; y < img_size.height; ++y)\r
+ {\r
+ for (int x = 0; x < img_size.width; ++x)\r
+ {\r
+ float source = mask1.at<uchar>(y, x) ? terminal_cost_ : 0.f;\r
+ float sink = mask2.at<uchar>(y, x) ? terminal_cost_ : 0.f;\r
+ terminals_(y, x) = saturate_cast<int>((source - sink) * 255.f);\r
+ }\r
+ }\r
+\r
+ // Set regular edge weights\r
+ const float weight_eps = 1.f;\r
+ for (int y = 0; y < img_size.height; ++y)\r
+ {\r
+ for (int x = 0; x < img_size.width; ++x)\r
+ {\r
+ if (x > 0)\r
+ {\r
+ float grad = dx1.at<float>(y, x - 1) + dx1.at<float>(y, x) +\r
+ dx2.at<float>(y, x - 1) + dx2.at<float>(y, x) + weight_eps;\r
+ float weight = (normL2(img1.at<Point3f>(y, x - 1), img2.at<Point3f>(y, x - 1)) +\r
+ normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x))) / grad +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y, x - 1) || !mask1.at<uchar>(y, x) ||\r
+ !mask2.at<uchar>(y, x - 1) || !mask2.at<uchar>(y, x))\r
+ weight += bad_region_penalty_;\r
+ leftT_(x, y) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ leftT_(x, y) = 0;\r
+\r
+ if (x < img_size.width - 1)\r
+ {\r
+ float grad = dx1.at<float>(y, x) + dx1.at<float>(y, x + 1) +\r
+ dx2.at<float>(y, x) + dx2.at<float>(y, x + 1) + weight_eps;\r
+ float weight = (normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x)) +\r
+ normL2(img1.at<Point3f>(y, x + 1), img2.at<Point3f>(y, x + 1))) / grad +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y, x) || !mask1.at<uchar>(y, x + 1) ||\r
+ !mask2.at<uchar>(y, x) || !mask2.at<uchar>(y, x + 1))\r
+ weight += bad_region_penalty_;\r
+ rightT_(x, y) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ rightT_(x, y) = 0;\r
+\r
+ if (y > 0)\r
+ {\r
+ float grad = dy1.at<float>(y - 1, x) + dy1.at<float>(y, x) +\r
+ dy2.at<float>(y - 1, x) + dy2.at<float>(y, x) + weight_eps;\r
+ float weight = (normL2(img1.at<Point3f>(y - 1, x), img2.at<Point3f>(y - 1, x)) +\r
+ normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x))) / grad +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y - 1, x) || !mask1.at<uchar>(y, x) ||\r
+ !mask2.at<uchar>(y - 1, x) || !mask2.at<uchar>(y, x))\r
+ weight += bad_region_penalty_;\r
+ top_(y, x) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ top_(y, x) = 0;\r
+\r
+ if (y < img_size.height - 1)\r
+ {\r
+ float grad = dy1.at<float>(y, x) + dy1.at<float>(y + 1, x) +\r
+ dy2.at<float>(y, x) + dy2.at<float>(y + 1, x) + weight_eps;\r
+ float weight = (normL2(img1.at<Point3f>(y, x), img2.at<Point3f>(y, x)) +\r
+ normL2(img1.at<Point3f>(y + 1, x), img2.at<Point3f>(y + 1, x))) / grad +\r
+ weight_eps;\r
+ if (!mask1.at<uchar>(y, x) || !mask1.at<uchar>(y + 1, x) ||\r
+ !mask2.at<uchar>(y, x) || !mask2.at<uchar>(y + 1, x))\r
+ weight += bad_region_penalty_;\r
+ bottom_(y, x) = saturate_cast<int>(weight * 255.f);\r
+ }\r
+ else\r
+ bottom_(y, x) = 0;\r
+ }\r
+ }\r
+}\r
+#endif\r
+\r
} // namespace detail\r
} // namespace cv\r
\r