Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cv_bridge/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,23 @@ find_package(OpenCV 4 QUIET
opencv_core
opencv_imgproc
opencv_imgcodecs
opencv_cudaimgproc
CONFIG
)

if(OpenCV_FOUND)
add_compile_definitions("HAVE_OPENCV_CUDAIMGPROC")
set(WITH_OPENCV_CUDAIMGPROC TRUE)
else()
find_package(OpenCV 4 QUIET
COMPONENTS
opencv_core
opencv_imgproc
opencv_imgcodecs
CONFIG
)
endif()

if(NOT OpenCV_FOUND)
find_package(OpenCV 3 REQUIRED
COMPONENTS
Expand Down
9 changes: 8 additions & 1 deletion cv_bridge/src/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
${sensor_msgs_TARGETS}
opencv_core
opencv_imgproc

opencv_imgcodecs
rclcpp::rclcpp
)
opencv_imgcodecs)

if(WITH_OPENCV_CUDAIMGPROC)
target_link_libraries(${PROJECT_NAME} PUBLIC
opencv_cudaimgproc)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE
Boost::headers
rcpputils::rcpputils)
Expand Down
42 changes: 41 additions & 1 deletion cv_bridge/src/cv_bridge.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <boost/endian/conversion.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#ifdef HAVE_OPENCV_CUDAIMGPROC
#include <opencv2/cudaimgproc.hpp>
#endif
#include <sensor_msgs/image_encodings.hpp>
#include "rcpputils/endian.hpp"

Expand Down Expand Up @@ -193,6 +196,23 @@ std::map<std::pair<Encoding, Encoding>, std::vector<int>> getConversionCodes()
res[std::make_pair(YUV422_YUY2, BGRA)].push_back(cv::COLOR_YUV2BGRA_YUY2);

// Deal with Bayer
#ifdef HAVE_OPENCV_CUDAIMGPROC
res[std::make_pair(BAYER_RGGB, GRAY)].push_back(cv::cuda::COLOR_BayerBG2GRAY_MHT);
res[std::make_pair(BAYER_RGGB, RGB)].push_back(cv::cuda::COLOR_BayerBG2RGB_MHT);
res[std::make_pair(BAYER_RGGB, BGR)].push_back(cv::cuda::COLOR_BayerBG2BGR_MHT);

res[std::make_pair(BAYER_BGGR, GRAY)].push_back(cv::cuda::COLOR_BayerRG2GRAY_MHT);
res[std::make_pair(BAYER_BGGR, RGB)].push_back(cv::cuda::COLOR_BayerRG2RGB_MHT);
res[std::make_pair(BAYER_BGGR, BGR)].push_back(cv::cuda::COLOR_BayerRG2BGR_MHT);

res[std::make_pair(BAYER_GBRG, GRAY)].push_back(cv::cuda::COLOR_BayerGR2GRAY_MHT);
res[std::make_pair(BAYER_GBRG, RGB)].push_back(cv::cuda::COLOR_BayerGR2RGB_MHT);
res[std::make_pair(BAYER_GBRG, BGR)].push_back(cv::cuda::COLOR_BayerGR2BGR_MHT);

res[std::make_pair(BAYER_GRBG, GRAY)].push_back(cv::cuda::COLOR_BayerGB2GRAY_MHT);
res[std::make_pair(BAYER_GRBG, RGB)].push_back(cv::cuda::COLOR_BayerGB2RGB_MHT);
res[std::make_pair(BAYER_GRBG, BGR)].push_back(cv::cuda::COLOR_BayerGB2BGR_MHT);
#else
res[std::make_pair(BAYER_RGGB, GRAY)].push_back(cv::COLOR_BayerBG2GRAY);
res[std::make_pair(BAYER_RGGB, RGB)].push_back(cv::COLOR_BayerBG2RGB);
res[std::make_pair(BAYER_RGGB, BGR)].push_back(cv::COLOR_BayerBG2BGR);
Expand All @@ -208,7 +228,7 @@ std::map<std::pair<Encoding, Encoding>, std::vector<int>> getConversionCodes()
res[std::make_pair(BAYER_GRBG, GRAY)].push_back(cv::COLOR_BayerGB2GRAY);
res[std::make_pair(BAYER_GRBG, RGB)].push_back(cv::COLOR_BayerGB2RGB);
res[std::make_pair(BAYER_GRBG, BGR)].push_back(cv::COLOR_BayerGB2BGR);

#endif
return res;
}

Expand Down Expand Up @@ -366,7 +386,27 @@ CvImagePtr toCvCopyImpl(
}
} else {
// Perform color conversion
#ifdef HAVE_OPENCV_CUDAIMGPROC
if (cv::cuda::getCudaEnabledDeviceCount() > 0) {
if (src_encoding == enc::BAYER_RGGB8 || src_encoding == enc::BAYER_BGGR8 || src_encoding == enc::BAYER_GBRG8 ||
src_encoding == enc::BAYER_GRBG8)
{
cv::cuda::GpuMat gpu_image1(image1);
cv::cuda::GpuMat gpu_image2;
cv::cuda::demosaicing(gpu_image1, gpu_image2, conversion_code);
gpu_image2.download(image2);
} else {
cv::cuda::GpuMat gpu_image1(image1);
cv::cuda::GpuMat gpu_image2;
cv::cuda::cvtColor(gpu_image1, gpu_image2, conversion_code);
gpu_image2.download(image2);
}
} else {
cv::cvtColor(image1, image2, conversion_code);
}
#else
cv::cvtColor(image1, image2, conversion_code);
#endif
}
image1 = image2;
}
Expand Down