Skip to content

Commit ec24b9c

Browse files
committed
Extend cudaimgproc::demosaicing for f32
1 parent 5409e01 commit ec24b9c

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

modules/cudaimgproc/src/color.cpp

+24-9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ namespace cv { namespace cuda {
7272

7373
template <int cn>
7474
void MHCdemosaic(PtrStepSzb src, int2 sourceOffset, PtrStepSzb dst, int2 firstRed, cudaStream_t stream);
75+
76+
void MHCdemosaic_float3(PtrStepSzf src, int2 sourceOffset, PtrStepSzf dst, int2 firstRed, cudaStream_t stream);
7577
}
7678
}}
7779

@@ -2136,7 +2138,7 @@ void cv::cuda::demosaicing(InputArray _src, OutputArray _dst, int code, int dcn,
21362138
GpuMat src = _src.getGpuMat();
21372139
const int depth = _src.depth();
21382140

2139-
CV_Assert( depth == CV_8U );
2141+
CV_Assert( depth == CV_8U || (depth == CV_32F && dcn == 3) );
21402142
CV_Assert( src.channels() == 1 );
21412143
CV_Assert( dcn == 3 || dcn == 4 );
21422144

@@ -2145,18 +2147,31 @@ void cv::cuda::demosaicing(InputArray _src, OutputArray _dst, int code, int dcn,
21452147

21462148
dst.setTo(Scalar::all(0), stream);
21472149

2148-
Size wholeSize;
2149-
Point ofs;
2150-
src.locateROI(wholeSize, ofs);
2151-
PtrStepSzb srcWhole(wholeSize.height, wholeSize.width, src.datastart, src.step);
2152-
21532150
const int2 firstRed = make_int2(code == COLOR_BayerRG2BGR_MHT || code == COLOR_BayerGB2BGR_MHT ? 0 : 1,
21542151
code == COLOR_BayerRG2BGR_MHT || code == COLOR_BayerGR2BGR_MHT ? 0 : 1);
21552152

2156-
if (dcn == 3)
2157-
cv::cuda::device::MHCdemosaic<3>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2153+
if (depth == CV_8U)
2154+
{
2155+
Size wholeSize;
2156+
Point ofs;
2157+
src.locateROI(wholeSize, ofs);
2158+
PtrStepSzb srcWhole(wholeSize.height, wholeSize.width, src.datastart, src.step);
2159+
2160+
if (dcn == 3)
2161+
cv::cuda::device::MHCdemosaic<3>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2162+
else
2163+
cv::cuda::device::MHCdemosaic<4>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2164+
}
2165+
// depth == CV_32F && dcn == 3
21582166
else
2159-
cv::cuda::device::MHCdemosaic<4>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2167+
{
2168+
Size wholeSize;
2169+
Point ofs;
2170+
src.locateROI(wholeSize, ofs);
2171+
PtrStepSzf srcWhole(wholeSize.height, wholeSize.width, (float*)src.datastart, src.step);
2172+
2173+
cv::cuda::device::MHCdemosaic_float3(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2174+
}
21602175

21612176
break;
21622177
}

modules/cudaimgproc/src/cuda/debayer.cu

+37-7
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ namespace cv { namespace cuda { namespace device
382382
template void Bayer2BGR_16u_gpu<3>(PtrStepSzb src, PtrStepSzb dst, bool blue_last, bool start_with_green, cudaStream_t stream);
383383
template void Bayer2BGR_16u_gpu<4>(PtrStepSzb src, PtrStepSzb dst, bool blue_last, bool start_with_green, cudaStream_t stream);
384384

385+
template <typename D> __device__ __forceinline__ D toDstColor(const float3& pix){
386+
return toDst<D>(make_uchar3(saturate_cast<uchar>(pix.x), saturate_cast<uchar>(pix.y), saturate_cast<uchar>(pix.z)));
387+
}
388+
template <> __device__ __forceinline__ float3 toDstColor<float3>(const float3& pix)
389+
{
390+
return pix;
391+
}
392+
385393
//////////////////////////////////////////////////////////////
386394
// Bayer Demosaicing (Malvar, He, and Cutler)
387395
//
@@ -507,16 +515,15 @@ namespace cv { namespace cuda { namespace device
507515
alternate.y = (y + firstRed.y) % 2;
508516

509517
// in BGR sequence;
510-
uchar3 pixelColor =
518+
float3 pixelColor =
511519
(alternate.y == 0) ?
512520
((alternate.x == 0) ?
513-
make_uchar3(saturate_cast<uchar>(PATTERN.y), saturate_cast<uchar>(PATTERN.x), saturate_cast<uchar>(C)) :
514-
make_uchar3(saturate_cast<uchar>(PATTERN.w), saturate_cast<uchar>(C), saturate_cast<uchar>(PATTERN.z))) :
521+
make_float3(PATTERN.y, PATTERN.x, C) :
522+
make_float3(PATTERN.w, C, PATTERN.z)) :
515523
((alternate.x == 0) ?
516-
make_uchar3(saturate_cast<uchar>(PATTERN.z), saturate_cast<uchar>(C), saturate_cast<uchar>(PATTERN.w)) :
517-
make_uchar3(saturate_cast<uchar>(C), saturate_cast<uchar>(PATTERN.x), saturate_cast<uchar>(PATTERN.y)));
518-
519-
dst(y, x) = toDst<DstType>(pixelColor);
524+
make_float3(PATTERN.z, C, PATTERN.w) :
525+
make_float3(C, PATTERN.x, PATTERN.y));
526+
dst(y, x) = toDstColor<DstType>(pixelColor);
520527
}
521528

522529
template <int cn>
@@ -545,6 +552,29 @@ namespace cv { namespace cuda { namespace device
545552
template void MHCdemosaic<1>(PtrStepSzb src, int2 sourceOffset, PtrStepSzb dst, int2 firstRed, cudaStream_t stream);
546553
template void MHCdemosaic<3>(PtrStepSzb src, int2 sourceOffset, PtrStepSzb dst, int2 firstRed, cudaStream_t stream);
547554
template void MHCdemosaic<4>(PtrStepSzb src, int2 sourceOffset, PtrStepSzb dst, int2 firstRed, cudaStream_t stream);
555+
556+
// Implement MHCdemosaic for float and with a result of 3 channels
557+
void MHCdemosaic_float3(PtrStepSzf src, int2 sourceOffset, PtrStepSzf dst, int2 firstRed, cudaStream_t stream)
558+
{
559+
typedef typename TypeVec<float, 3>::vec_type dst_t;
560+
561+
const dim3 block(32, 8);
562+
const dim3 grid(divUp(src.cols, block.x), divUp(src.rows, block.y));
563+
564+
if (sourceOffset.x || sourceOffset.y) {
565+
cv::cudev::TextureOff<float> texSrc(src, sourceOffset.y, sourceOffset.x);
566+
MHCdemosaic<dst_t, cv::cudev::TextureOffPtr<float>><<<grid, block, 0, stream>>>((PtrStepSz<dst_t>)dst, texSrc, firstRed);
567+
}
568+
else {
569+
cv::cudev::Texture<float> texSrc(src);
570+
MHCdemosaic<dst_t, cv::cudev::TexturePtr<float>><<<grid, block, 0, stream>>>((PtrStepSz<dst_t>)dst, texSrc, firstRed);
571+
}
572+
573+
cudaSafeCall( cudaGetLastError() );
574+
575+
if (stream == 0)
576+
cudaSafeCall( cudaDeviceSynchronize() );
577+
}
548578
}}}
549579

550580
#endif /* CUDA_DISABLER */

0 commit comments

Comments
 (0)