-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3153 from stal12:4.x
- Loading branch information
Showing
6 changed files
with
985 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@article{Allegretti2019, | ||
title={Optimized block-based algorithms to label connected components on GPUs}, | ||
author={Allegretti, Stefano and Bolelli, Federico and Grana, Costantino}, | ||
journal={IEEE Transactions on Parallel and Distributed Systems}, | ||
volume={31}, | ||
number={2}, | ||
pages={423--438}, | ||
year={2019}, | ||
publisher={IEEE} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <iostream> | ||
|
||
#include <opencv2/core/utility.hpp> | ||
#include "opencv2/imgproc.hpp" | ||
#include "opencv2/imgcodecs.hpp" | ||
#include "opencv2/highgui.hpp" | ||
#include "opencv2/cudaimgproc.hpp" | ||
|
||
using namespace cv; | ||
using namespace std; | ||
using namespace cv::cuda; | ||
|
||
|
||
void colorLabels(const Mat1i& labels, Mat3b& colors) { | ||
colors.create(labels.size()); | ||
for (int r = 0; r < labels.rows; ++r) { | ||
int const* labels_row = labels.ptr<int>(r); | ||
Vec3b* colors_row = colors.ptr<Vec3b>(r); | ||
for (int c = 0; c < labels.cols; ++c) { | ||
colors_row[c] = Vec3b(labels_row[c] * 131 % 255, labels_row[c] * 241 % 255, labels_row[c] * 251 % 255); | ||
} | ||
} | ||
} | ||
|
||
|
||
int main(int argc, const char** argv) | ||
{ | ||
CommandLineParser parser(argc, argv, "{@image|stuff.jpg|image for converting to a grayscale}"); | ||
parser.about("This program finds connected components in a binary image and assign each of them a different color.\n" | ||
"The connected components labeling is performed in GPU.\n"); | ||
parser.printMessage(); | ||
|
||
String inputImage = parser.get<string>(0); | ||
Mat1b img = imread(samples::findFile(inputImage), IMREAD_GRAYSCALE); | ||
Mat1i labels; | ||
|
||
if (img.empty()) | ||
{ | ||
cout << "Could not read input image file: " << inputImage << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
|
||
GpuMat d_img, d_labels; | ||
d_img.upload(img); | ||
|
||
cuda::connectedComponents(d_img, d_labels, 8, CV_32S); | ||
|
||
d_labels.download(labels); | ||
|
||
Mat3b colors; | ||
colorLabels(labels, colors); | ||
|
||
imshow("Labels", colors); | ||
waitKey(0); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#include "precomp.hpp" | ||
|
||
using namespace cv; | ||
using namespace cv::cuda; | ||
|
||
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) | ||
|
||
void cv::cuda::connectedComponents(InputArray img_, OutputArray labels_, int connectivity, | ||
int ltype, ConnectedComponentsAlgorithmsTypes ccltype) { throw_no_cuda(); } | ||
|
||
#else /* !defined (HAVE_CUDA) */ | ||
|
||
namespace cv { namespace cuda { namespace device { namespace imgproc { | ||
void BlockBasedKomuraEquivalence(const cv::cuda::GpuMat& img, cv::cuda::GpuMat& labels); | ||
}}}} | ||
|
||
|
||
void cv::cuda::connectedComponents(InputArray img_, OutputArray labels_, int connectivity, | ||
int ltype, ConnectedComponentsAlgorithmsTypes ccltype) { | ||
const cv::cuda::GpuMat img = img_.getGpuMat(); | ||
cv::cuda::GpuMat& labels = labels_.getGpuMatRef(); | ||
|
||
CV_Assert(img.channels() == 1); | ||
CV_Assert(connectivity == 8); | ||
CV_Assert(ltype == CV_32S); | ||
CV_Assert(ccltype == CCL_BKE || ccltype == CCL_DEFAULT); | ||
|
||
int iDepth = img_.depth(); | ||
CV_Assert(iDepth == CV_8U || iDepth == CV_8S); | ||
|
||
labels.create(img.size(), CV_MAT_DEPTH(ltype)); | ||
|
||
if ((ccltype == CCL_BKE || ccltype == CCL_DEFAULT) && connectivity == 8 && ltype == CV_32S) { | ||
using cv::cuda::device::imgproc::BlockBasedKomuraEquivalence; | ||
BlockBasedKomuraEquivalence(img, labels); | ||
} | ||
|
||
} | ||
|
||
void cv::cuda::connectedComponents(InputArray img_, OutputArray labels_, int connectivity, int ltype) { | ||
cv::cuda::connectedComponents(img_, labels_, connectivity, ltype, CCL_DEFAULT); | ||
} | ||
|
||
|
||
#endif /* !defined (HAVE_CUDA) */ |
Oops, something went wrong.