Skip to content

Commit ddb96cd

Browse files
committed
Extend cudaimgproc::demosaicing for f32
1 parent 1fffe35 commit ddb96cd

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

modules/cudaimgproc/src/color.cpp

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

7373
template <int cn, typename Depth>
7474
void MHCdemosaic(PtrStepSz<Depth> src, int2 sourceOffset, PtrStepSz<Depth> 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 || depth == CV_16U);
2141+
CV_Assert( depth == CV_8U || depth == CV_16U || (depth == CV_32F && dcn == 3) );
21402142
CV_Assert( src.channels() == 1 );
21412143
CV_Assert( dcn == 3 || dcn == 4 );
21422144

@@ -2156,6 +2158,9 @@ void cv::cuda::demosaicing(InputArray _src, OutputArray _dst, int code, int dcn,
21562158
if (depth == CV_8U) {
21572159
PtrStepSzb srcWhole(wholeSize.height, wholeSize.width, src.datastart, src.step);
21582160
cv::cuda::device::MHCdemosaic<3, uchar>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2161+
} else if (depth === CV_32F) {
2162+
PtrStepSzf srcWhole(wholeSize.height, wholeSize.width, (float*)src.datastart, src.step);
2163+
cv::cuda::device::MHCdemosaic_float3(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
21592164
} else {
21602165
PtrStepSz<ushort> srcWhole(wholeSize.height, wholeSize.width, src.ptr<ushort>(), src.step);
21612166
cv::cuda::device::MHCdemosaic<3, ushort>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));

modules/cudaimgproc/src/cuda/debayer.cu

+38-10
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ 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 DstType> __device__ __forceinline__ DstType toDstColor(const float3& pix){
386+
typedef typename VecTraits<DstType>::elem_type SrcElemType;
387+
return toDst<D>(make_3<SrcElemType>(saturate_cast<SrcElemType>(pix.x), saturate_cast<SrcElemType>(pix.y), saturate_cast<SrcElemType>(pix.z)));
388+
}
389+
template <> __device__ __forceinline__ float3 toDstColor<float3>(const float3& pix)
390+
{
391+
return pix;
392+
}
393+
385394
//////////////////////////////////////////////////////////////
386395
// Bayer Demosaicing (Malvar, He, and Cutler)
387396
//
@@ -517,19 +526,15 @@ namespace cv { namespace cuda { namespace device
517526
alternate.x = (x + firstRed.x) % 2;
518527
alternate.y = (y + firstRed.y) % 2;
519528

520-
typedef typename VecTraits<DstType>::elem_type SrcElemType;
521-
typedef typename TypeVec<SrcElemType, 3>::vec_type SrcType;
522-
523-
SrcType pixelColor =
529+
float3 pixelColor =
524530
(alternate.y == 0) ?
525531
((alternate.x == 0) ?
526-
make_3<SrcElemType>(saturate_cast<SrcElemType>(PATTERN.y), saturate_cast<SrcElemType>(PATTERN.x), saturate_cast<SrcElemType>(C)) :
527-
make_3<SrcElemType>(saturate_cast<SrcElemType>(PATTERN.w), saturate_cast<SrcElemType>(C), saturate_cast<SrcElemType>(PATTERN.z))) :
532+
make_float3(PATTERN.y, PATTERN.x, C) :
533+
make_float3(PATTERN.w, C, PATTERN.z)) :
528534
((alternate.x == 0) ?
529-
make_3<SrcElemType>(saturate_cast<SrcElemType>(PATTERN.z), saturate_cast<SrcElemType>(C), saturate_cast<SrcElemType>(PATTERN.w)) :
530-
make_3<SrcElemType>(saturate_cast<SrcElemType>(C), saturate_cast<SrcElemType>(PATTERN.x), saturate_cast<SrcElemType>(PATTERN.y)));
531-
532-
dst(y, x) = toDst<DstType>(pixelColor);
535+
make_float3(PATTERN.z, C, PATTERN.w) :
536+
make_float3(C, PATTERN.x, PATTERN.y));
537+
dst(y, x) = toDstColor<DstType>(pixelColor);
533538
}
534539

535540
template <int cn, typename Depth>
@@ -561,6 +566,29 @@ namespace cv { namespace cuda { namespace device
561566
template void MHCdemosaic<1, ushort>(PtrStepSz<ushort> src, int2 sourceOffset, PtrStepSz<ushort> dst, int2 firstRed, cudaStream_t stream);
562567
template void MHCdemosaic<3, ushort>(PtrStepSz<ushort> src, int2 sourceOffset, PtrStepSz<ushort> dst, int2 firstRed, cudaStream_t stream);
563568
template void MHCdemosaic<4, ushort>(PtrStepSz<ushort> src, int2 sourceOffset, PtrStepSz<ushort> dst, int2 firstRed, cudaStream_t stream);
569+
570+
// Implement MHCdemosaic for float and with a result of 3 channels
571+
void MHCdemosaic_float3(PtrStepSzf src, int2 sourceOffset, PtrStepSzf dst, int2 firstRed, cudaStream_t stream)
572+
{
573+
typedef typename TypeVec<float, 3>::vec_type dst_t;
574+
575+
const dim3 block(32, 8);
576+
const dim3 grid(divUp(src.cols, block.x), divUp(src.rows, block.y));
577+
578+
if (sourceOffset.x || sourceOffset.y) {
579+
cv::cudev::TextureOff<float> texSrc(src, sourceOffset.y, sourceOffset.x);
580+
MHCdemosaic<dst_t, cv::cudev::TextureOffPtr<float>><<<grid, block, 0, stream>>>((PtrStepSz<dst_t>)dst, texSrc, firstRed);
581+
}
582+
else {
583+
cv::cudev::Texture<float> texSrc(src);
584+
MHCdemosaic<dst_t, cv::cudev::TexturePtr<float>><<<grid, block, 0, stream>>>((PtrStepSz<dst_t>)dst, texSrc, firstRed);
585+
}
586+
587+
cudaSafeCall( cudaGetLastError() );
588+
589+
if (stream == 0)
590+
cudaSafeCall( cudaDeviceSynchronize() );
591+
}
564592
}}}
565593

566594
#endif /* CUDA_DISABLER */

0 commit comments

Comments
 (0)