mirror of
https://github.com/opencv/opencv.git
synced 2024-11-24 19:20:28 +08:00
Merge pull request #24569 from Abdurrahheem:ash/padding_value_fix
Add support for custom padding in DNN preprocessing #24569 This PR add functionality for specifying value in padding. It is required in many preprocessing pipelines in DNNs such as Yolox object detection model ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
parent
e9f35610a5
commit
5278560252
@ -1212,7 +1212,7 @@ CV__DNN_INLINE_NS_BEGIN
|
|||||||
CV_WRAP Image2BlobParams();
|
CV_WRAP Image2BlobParams();
|
||||||
CV_WRAP Image2BlobParams(const Scalar& scalefactor, const Size& size = Size(), const Scalar& mean = Scalar(),
|
CV_WRAP Image2BlobParams(const Scalar& scalefactor, const Size& size = Size(), const Scalar& mean = Scalar(),
|
||||||
bool swapRB = false, int ddepth = CV_32F, DataLayout datalayout = DNN_LAYOUT_NCHW,
|
bool swapRB = false, int ddepth = CV_32F, DataLayout datalayout = DNN_LAYOUT_NCHW,
|
||||||
ImagePaddingMode mode = DNN_PMODE_NULL);
|
ImagePaddingMode mode = DNN_PMODE_NULL, Scalar borderValue = 0.0);
|
||||||
|
|
||||||
CV_PROP_RW Scalar scalefactor; //!< scalefactor multiplier for input image values.
|
CV_PROP_RW Scalar scalefactor; //!< scalefactor multiplier for input image values.
|
||||||
CV_PROP_RW Size size; //!< Spatial size for output image.
|
CV_PROP_RW Size size; //!< Spatial size for output image.
|
||||||
@ -1221,6 +1221,7 @@ CV__DNN_INLINE_NS_BEGIN
|
|||||||
CV_PROP_RW int ddepth; //!< Depth of output blob. Choose CV_32F or CV_8U.
|
CV_PROP_RW int ddepth; //!< Depth of output blob. Choose CV_32F or CV_8U.
|
||||||
CV_PROP_RW DataLayout datalayout; //!< Order of output dimensions. Choose DNN_LAYOUT_NCHW or DNN_LAYOUT_NHWC.
|
CV_PROP_RW DataLayout datalayout; //!< Order of output dimensions. Choose DNN_LAYOUT_NCHW or DNN_LAYOUT_NHWC.
|
||||||
CV_PROP_RW ImagePaddingMode paddingmode; //!< Image padding mode. @see ImagePaddingMode.
|
CV_PROP_RW ImagePaddingMode paddingmode; //!< Image padding mode. @see ImagePaddingMode.
|
||||||
|
CV_PROP_RW Scalar borderValue; //!< Value used in padding mode for padding.
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief Creates 4-dimensional blob from image with given params.
|
/** @brief Creates 4-dimensional blob from image with given params.
|
||||||
|
@ -17,9 +17,9 @@ Image2BlobParams::Image2BlobParams():scalefactor(Scalar::all(1.0)), size(Size())
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
Image2BlobParams::Image2BlobParams(const Scalar& scalefactor_, const Size& size_, const Scalar& mean_, bool swapRB_,
|
Image2BlobParams::Image2BlobParams(const Scalar& scalefactor_, const Size& size_, const Scalar& mean_, bool swapRB_,
|
||||||
int ddepth_, DataLayout datalayout_, ImagePaddingMode mode_):
|
int ddepth_, DataLayout datalayout_, ImagePaddingMode mode_, Scalar borderValue_):
|
||||||
scalefactor(scalefactor_), size(size_), mean(mean_), swapRB(swapRB_), ddepth(ddepth_),
|
scalefactor(scalefactor_), size(size_), mean(mean_), swapRB(swapRB_), ddepth(ddepth_),
|
||||||
datalayout(datalayout_), paddingmode(mode_)
|
datalayout(datalayout_), paddingmode(mode_), borderValue(borderValue_)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void getVector(InputArrayOfArrays images_, std::vector<Mat>& images) {
|
void getVector(InputArrayOfArrays images_, std::vector<Mat>& images) {
|
||||||
@ -196,7 +196,7 @@ void blobFromImagesWithParamsImpl(InputArrayOfArrays images_, Tmat& blob_, const
|
|||||||
int bottom = size.height - top - rh;
|
int bottom = size.height - top - rh;
|
||||||
int left = (size.width - rw)/2;
|
int left = (size.width - rw)/2;
|
||||||
int right = size.width - left - rw;
|
int right = size.width - left - rw;
|
||||||
copyMakeBorder(images[i], images[i], top, bottom, left, right, BORDER_CONSTANT);
|
copyMakeBorder(images[i], images[i], top, bottom, left, right, BORDER_CONSTANT, param.borderValue);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -93,6 +93,39 @@ TEST(blobFromImageWithParams_4ch, NHWC_scalar_scale)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(blobFromImageWithParams_CustomPadding, letter_box)
|
||||||
|
{
|
||||||
|
Mat img(40, 20, CV_8UC4, Scalar(0, 1, 2, 3));
|
||||||
|
|
||||||
|
// Custom padding value that you have added
|
||||||
|
Scalar customPaddingValue(5, 6, 7, 8); // Example padding value
|
||||||
|
|
||||||
|
Size targetSize(20, 20);
|
||||||
|
|
||||||
|
Mat targetImg = img.clone();
|
||||||
|
|
||||||
|
cv::copyMakeBorder(
|
||||||
|
targetImg, targetImg, 0, 0,
|
||||||
|
targetSize.width / 2,
|
||||||
|
targetSize.width / 2,
|
||||||
|
BORDER_CONSTANT,
|
||||||
|
customPaddingValue);
|
||||||
|
|
||||||
|
// Set up Image2BlobParams with your new functionality
|
||||||
|
Image2BlobParams param;
|
||||||
|
param.size = targetSize;
|
||||||
|
param.paddingmode = DNN_PMODE_LETTERBOX;
|
||||||
|
param.borderValue = customPaddingValue; // Use your new feature here
|
||||||
|
|
||||||
|
// Create blob with custom padding
|
||||||
|
Mat blob = dnn::blobFromImageWithParams(img, param);
|
||||||
|
|
||||||
|
// Create target blob for comparison
|
||||||
|
Mat targetBlob = dnn::blobFromImage(targetImg, 1.0, targetSize);
|
||||||
|
|
||||||
|
EXPECT_EQ(0, cvtest::norm(targetBlob, blob, NORM_INF));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(blobFromImageWithParams_4ch, letter_box)
|
TEST(blobFromImageWithParams_4ch, letter_box)
|
||||||
{
|
{
|
||||||
Mat img(40, 20, CV_8UC4, cv::Scalar(0,1,2,3));
|
Mat img(40, 20, CV_8UC4, cv::Scalar(0,1,2,3));
|
||||||
|
Loading…
Reference in New Issue
Block a user