Skip to content

Commit e356ca2

Browse files
committed
Merge pull request opencv#9835 from sovrasov:blob_from_img_crop_opt
2 parents e955bcb + 47e1133 commit e356ca2

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
695695
* @param scalefactor multiplier for @p image values.
696696
* @param swapRB flag which indicates that swap first and last channels
697697
* in 3-channel image is necessary.
698-
* @details input image is resized so one side after resize is equal to corresponing
698+
* @param crop flag which indicates whether image will be cropped after resize or not
699+
* @details if @p crop is true, input image is resized so one side after resize is equal to corresponing
699700
* dimension in @p size and another one is equal or larger. Then, crop from the center is performed.
701+
* If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.
700702
* @returns 4-dimansional Mat with NCHW dimensions order.
701703
*/
702704
CV_EXPORTS_W Mat blobFromImage(const Mat& image, double scalefactor=1.0, const Size& size = Size(),
703-
const Scalar& mean = Scalar(), bool swapRB=true);
705+
const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true);
704706
/** @brief Creates 4-dimensional blob from series of images. Optionally resizes and
705707
* crops @p images from center, subtract @p mean values, scales values by @p scalefactor,
706708
* swap Blue and Red channels.
@@ -711,12 +713,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
711713
* @param scalefactor multiplier for @p images values.
712714
* @param swapRB flag which indicates that swap first and last channels
713715
* in 3-channel image is necessary.
714-
* @details input image is resized so one side after resize is equal to corresponing
716+
* @param crop flag which indicates whether image will be cropped after resize or not
717+
* @details if @p crop is true, input image is resized so one side after resize is equal to corresponing
715718
* dimension in @p size and another one is equal or larger. Then, crop from the center is performed.
719+
* If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.
716720
* @returns 4-dimansional Mat with NCHW dimensions order.
717721
*/
718722
CV_EXPORTS_W Mat blobFromImages(const std::vector<Mat>& images, double scalefactor=1.0,
719-
Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=true);
723+
Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true);
720724

721725
/** @brief Convert all weights of Caffe network to half precision floating point.
722726
* @param src Path to origin model from Caffe framework contains single

modules/dnn/src/dnn.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ static String toString(const T &v)
8585
}
8686

8787
Mat blobFromImage(const Mat& image, double scalefactor, const Size& size,
88-
const Scalar& mean, bool swapRB)
88+
const Scalar& mean, bool swapRB, bool crop)
8989
{
9090
CV_TRACE_FUNCTION();
9191
std::vector<Mat> images(1, image);
92-
return blobFromImages(images, scalefactor, size, mean, swapRB);
92+
return blobFromImages(images, scalefactor, size, mean, swapRB, crop);
9393
}
9494

9595
Mat blobFromImages(const std::vector<Mat>& images_, double scalefactor, Size size,
96-
const Scalar& mean_, bool swapRB)
96+
const Scalar& mean_, bool swapRB, bool crop)
9797
{
9898
CV_TRACE_FUNCTION();
9999
std::vector<Mat> images = images_;
@@ -104,13 +104,18 @@ Mat blobFromImages(const std::vector<Mat>& images_, double scalefactor, Size siz
104104
size = imgSize;
105105
if (size != imgSize)
106106
{
107-
float resizeFactor = std::max(size.width / (float)imgSize.width,
108-
size.height / (float)imgSize.height);
109-
resize(images[i], images[i], Size(), resizeFactor, resizeFactor);
110-
Rect crop(Point(0.5 * (images[i].cols - size.width),
111-
0.5 * (images[i].rows - size.height)),
112-
size);
113-
images[i] = images[i](crop);
107+
if(crop)
108+
{
109+
float resizeFactor = std::max(size.width / (float)imgSize.width,
110+
size.height / (float)imgSize.height);
111+
resize(images[i], images[i], Size(), resizeFactor, resizeFactor);
112+
Rect crop(Point(0.5 * (images[i].cols - size.width),
113+
0.5 * (images[i].rows - size.height)),
114+
size);
115+
images[i] = images[i](crop);
116+
}
117+
else
118+
resize(images[i], images[i], size);
114119
}
115120
if(images[i].depth() == CV_8U)
116121
images[i].convertTo(images[i], CV_32F);

0 commit comments

Comments
 (0)