diff --git a/modules/imgproc/src/demosaicing.cpp b/modules/imgproc/src/demosaicing.cpp index b268df753c..ac95b3eb73 100644 --- a/modules/imgproc/src/demosaicing.cpp +++ b/modules/imgproc/src/demosaicing.cpp @@ -1612,12 +1612,9 @@ public: int dcn = dst.channels(); int dcn2 = dcn<<1; int start_with_green = Start_with_green, blue = Blue; - int sstep = int(src.step / src.elemSize1()), dstep = int(dst.step / dst.elemSize1()); + int sstep = int(src.step / src.elemSize1()); SIMDInterpolator vecOp; - const T* S = src.ptr(range.start + 1) + 1; - T* D = reinterpret_cast(dst.data + (range.start + 1) * dst.step) + dcn; - if (range.start % 2) { start_with_green ^= 1; @@ -1627,6 +1624,10 @@ public: // to BGR for (int y = range.start; y < range.end; ++y) { + // rows are not necessarily contiguous (src or dst can be a sub-matrix) + const T* S = src.ptr(y + 1) + 1; + T* D = reinterpret_cast(dst.data + (y + 1) * dst.step) + dcn; + int x = 1; if (start_with_green) { @@ -1675,16 +1676,15 @@ public: ++S; } + T* Dfirst = reinterpret_cast(dst.data + (y + 1) * dst.step); for (int i = 0; i < dcn; ++i) { D[i] = D[-dcn + i]; - D[-dstep+dcn+i] = D[-dstep+(dcn<<1)+i]; + Dfirst[i] = Dfirst[dcn + i]; } start_with_green ^= 1; blue ^= 1; - S += 2; - D += dcn2; } } diff --git a/modules/imgproc/test/test_color.cpp b/modules/imgproc/test/test_color.cpp index b843448035..e64f970e25 100644 --- a/modules/imgproc/test/test_color.cpp +++ b/modules/imgproc/test/test_color.cpp @@ -3141,6 +3141,54 @@ TEST(ImgProc_BayerEdgeAwareDemosaicing, accuracy) } } +// The edge-aware demosaicing walked the source and destination rows with a fixed pointer step, +// which is only correct for continuous matrices. A source or destination that is a region of a +// larger image gave wrong pixels in most rows. +typedef testing::TestWithParam< tuple > ImgProc_BayerEdgeAware_SubMatrix; + +TEST_P(ImgProc_BayerEdgeAware_SubMatrix, source_and_destination_regions) +{ + const int code = get<0>(GetParam()); + const int depth = get<1>(GetParam()); + const Size sz = get<2>(GetParam()); + RNG& rng = cvtest::TS::ptr()->get_rng(); + + Mat bayer(sz, CV_MAKETYPE(depth, 1)); + rng.fill(bayer, RNG::UNIFORM, 0, depth == CV_8U ? 256 : 65536); + + Mat expected; + cv::cvtColor(bayer, expected, code); + ASSERT_TRUE(expected.isContinuous()); + + // source: a region of a larger image whose other pixels are unrelated + Mat parentSrc(sz.height + 6, sz.width + 8, bayer.type()); + rng.fill(parentSrc, RNG::UNIFORM, 0, depth == CV_8U ? 256 : 65536); + Mat srcRegion = parentSrc(Rect(3, 2, sz.width, sz.height)); + bayer.copyTo(srcRegion); + ASSERT_FALSE(srcRegion.isContinuous()); + + Mat actual; + cv::cvtColor(srcRegion, actual, code); + EXPECT_EQ(0, cvtest::norm(expected, actual, NORM_INF)) << "source region"; + + // destination: a region of a larger image, the pixels around it must stay as they are + Mat parentDst(sz.height + 4, sz.width + 6, expected.type(), Scalar::all(77)); + Mat dstRegion = parentDst(Rect(2, 1, sz.width, sz.height)); + cv::cvtColor(bayer, dstRegion, code); + ASSERT_EQ(parentDst.data + parentDst.step + 2 * expected.elemSize(), dstRegion.data); + + Mat parentExpected(parentDst.size(), parentDst.type(), Scalar::all(77)); + expected.copyTo(parentExpected(Rect(2, 1, sz.width, sz.height))); + EXPECT_EQ(0, cvtest::norm(parentExpected, parentDst, NORM_INF)) << "destination region"; +} + +INSTANTIATE_TEST_CASE_P(/**/, ImgProc_BayerEdgeAware_SubMatrix, + testing::Combine( + testing::Values((int)COLOR_BayerBG2BGR_EA, (int)COLOR_BayerGB2BGR_EA, + (int)COLOR_BayerRG2BGR_EA, (int)COLOR_BayerGR2BGR_EA), + testing::Values((int)CV_8U, (int)CV_16U), + testing::Values(Size(23, 19), Size(16, 14)))); + TEST(ImgProc_Bayer2RGBA, accuracy) { cvtest::TS* ts = cvtest::TS::ptr();