From 58ac3e09da332cd23126500b9e49059633522c5a Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Fri, 31 Aug 2018 17:27:10 +0300 Subject: [PATCH] Change default value of crop argument of blobFromImage from true to false --- modules/dnn/include/opencv2/dnn/dnn.hpp | 12 +++--- modules/dnn/test/test_caffe_importer.cpp | 4 +- modules/dnn/test/test_tf_importer.cpp | 3 +- modules/dnn/test/test_torch_importer.cpp | 4 +- .../opencv_mobilenet/MainActivity.java | 40 +++++-------------- 5 files changed, 22 insertions(+), 41 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp index e418ae4066..01268d7842 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -46,9 +46,9 @@ #include #if !defined CV_DOXYGEN && !defined CV_DNN_DONT_ADD_EXPERIMENTAL_NS -#define CV__DNN_EXPERIMENTAL_NS_BEGIN namespace experimental_dnn_34_v8 { +#define CV__DNN_EXPERIMENTAL_NS_BEGIN namespace experimental_dnn_34_v9 { #define CV__DNN_EXPERIMENTAL_NS_END } -namespace cv { namespace dnn { namespace experimental_dnn_34_v8 { } using namespace experimental_dnn_34_v8; }} +namespace cv { namespace dnn { namespace experimental_dnn_34_v9 { } using namespace experimental_dnn_34_v9; }} #else #define CV__DNN_EXPERIMENTAL_NS_BEGIN #define CV__DNN_EXPERIMENTAL_NS_END @@ -843,7 +843,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN * @returns 4-dimensional Mat with NCHW dimensions order. */ CV_EXPORTS_W Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size& size = Size(), - const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true, + const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false, int ddepth=CV_32F); /** @brief Creates 4-dimensional blob from image. @@ -852,7 +852,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN */ CV_EXPORTS void blobFromImage(InputArray image, OutputArray blob, double scalefactor=1.0, const Size& size = Size(), const Scalar& mean = Scalar(), - bool swapRB=true, bool crop=true, int ddepth=CV_32F); + bool swapRB=false, bool crop=false, int ddepth=CV_32F); /** @brief Creates 4-dimensional blob from series of images. Optionally resizes and @@ -873,7 +873,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN * @returns 4-dimensional Mat with NCHW dimensions order. */ CV_EXPORTS_W Mat blobFromImages(InputArrayOfArrays images, double scalefactor=1.0, - Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true, + Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false, int ddepth=CV_32F); /** @brief Creates 4-dimensional blob from series of images. @@ -882,7 +882,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN */ CV_EXPORTS void blobFromImages(InputArrayOfArrays images, OutputArray blob, double scalefactor=1.0, Size size = Size(), - const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true, + const Scalar& mean = Scalar(), bool swapRB=false, bool crop=false, int ddepth=CV_32F); /** @brief Parse a 4D blob and output the images it contains as 2D arrays through a simpler data structure diff --git a/modules/dnn/test/test_caffe_importer.cpp b/modules/dnn/test/test_caffe_importer.cpp index 85ff7ace21..b6da2f189c 100644 --- a/modules/dnn/test/test_caffe_importer.cpp +++ b/modules/dnn/test/test_caffe_importer.cpp @@ -307,7 +307,7 @@ TEST_P(Reproducibility_SqueezeNet_v1_1, Accuracy) net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableTarget(targetId); - Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false); + Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false, true); ASSERT_TRUE(!input.empty()); Mat out; @@ -403,7 +403,7 @@ TEST_P(Test_Caffe_nets, DenseNet_121) const string model = findDataFile("dnn/DenseNet_121.caffemodel", false); Mat inp = imread(_tf("dog416.png")); - inp = blobFromImage(inp, 1.0 / 255, Size(224, 224)); + inp = blobFromImage(inp, 1.0 / 255, Size(224, 224), Scalar(), true, true); Mat ref = blobFromNPY(_tf("densenet_121_output.npy")); Net net = readNetFromCaffe(proto, model); diff --git a/modules/dnn/test/test_tf_importer.cpp b/modules/dnn/test/test_tf_importer.cpp index b05d1f5440..b10c1388f3 100644 --- a/modules/dnn/test/test_tf_importer.cpp +++ b/modules/dnn/test/test_tf_importer.cpp @@ -62,8 +62,7 @@ TEST(Test_TensorFlow, inception_accuracy) Mat sample = imread(_tf("grace_hopper_227.png")); ASSERT_TRUE(!sample.empty()); - resize(sample, sample, Size(224, 224)); - Mat inputBlob = blobFromImage(sample); + Mat inputBlob = blobFromImage(sample, 1.0, Size(224, 224), Scalar(), /*swapRB*/true); net.setInput(inputBlob, "input"); Mat out = net.forward("softmax2"); diff --git a/modules/dnn/test/test_torch_importer.cpp b/modules/dnn/test/test_torch_importer.cpp index bd5f11249d..dd7d975af6 100644 --- a/modules/dnn/test/test_torch_importer.cpp +++ b/modules/dnn/test/test_torch_importer.cpp @@ -278,7 +278,7 @@ TEST_P(Test_Torch_nets, OpenFace_accuracy) sampleF32 /= 255; resize(sampleF32, sampleF32, Size(96, 96), 0, 0, INTER_NEAREST); - Mat inputBlob = blobFromImage(sampleF32); + Mat inputBlob = blobFromImage(sampleF32, 1.0, Size(), Scalar(), /*swapRB*/true); net.setInput(inputBlob); Mat out = net.forward(); @@ -305,7 +305,7 @@ TEST_P(Test_Torch_nets, ENet_accuracy) net.setPreferableTarget(target); Mat sample = imread(_tf("street.png", false)); - Mat inputBlob = blobFromImage(sample, 1./255); + Mat inputBlob = blobFromImage(sample, 1./255, Size(), Scalar(), /*swapRB*/true); net.setInput(inputBlob, ""); Mat out = net.forward(); diff --git a/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java b/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java index 31440e2c85..eed419520d 100644 --- a/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java +++ b/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java @@ -86,29 +86,13 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe // Forward image through network. Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR, new Size(IN_WIDTH, IN_HEIGHT), - new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), false); + new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false); net.setInput(blob); Mat detections = net.forward(); int cols = frame.cols(); int rows = frame.rows(); - Size cropSize; - if ((float)cols / rows > WH_RATIO) { - cropSize = new Size(rows * WH_RATIO, rows); - } else { - cropSize = new Size(cols, cols / WH_RATIO); - } - - int y1 = (int)(rows - cropSize.height) / 2; - int y2 = (int)(y1 + cropSize.height); - int x1 = (int)(cols - cropSize.width) / 2; - int x2 = (int)(x1 + cropSize.width); - Mat subFrame = frame.submat(y1, y2, x1, x2); - - cols = subFrame.cols(); - rows = subFrame.rows(); - detections = detections.reshape(1, (int)detections.total() / 7); for (int i = 0; i < detections.rows(); ++i) { @@ -116,26 +100,24 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe if (confidence > THRESHOLD) { int classId = (int)detections.get(i, 1)[0]; - int xLeftBottom = (int)(detections.get(i, 3)[0] * cols); - int yLeftBottom = (int)(detections.get(i, 4)[0] * rows); - int xRightTop = (int)(detections.get(i, 5)[0] * cols); - int yRightTop = (int)(detections.get(i, 6)[0] * rows); + int left = (int)(detections.get(i, 3)[0] * cols); + int top = (int)(detections.get(i, 4)[0] * rows); + int right = (int)(detections.get(i, 5)[0] * cols); + int bottom = (int)(detections.get(i, 6)[0] * rows); // Draw rectangle around detected object. - Imgproc.rectangle(subFrame, new Point(xLeftBottom, yLeftBottom), - new Point(xRightTop, yRightTop), - new Scalar(0, 255, 0)); + Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom), + new Scalar(0, 255, 0)); String label = classNames[classId] + ": " + confidence; int[] baseLine = new int[1]; Size labelSize = Imgproc.getTextSize(label, Core.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine); // Draw background for label. - Imgproc.rectangle(subFrame, new Point(xLeftBottom, yLeftBottom - labelSize.height), - new Point(xLeftBottom + labelSize.width, yLeftBottom + baseLine[0]), - new Scalar(255, 255, 255), Core.FILLED); - + Imgproc.rectangle(frame, new Point(left, top - labelSize.height), + new Point(left + labelSize.width, top + baseLine[0]), + new Scalar(255, 255, 255), Imgproc.FILLED); // Write class name and confidence. - Imgproc.putText(subFrame, label, new Point(xLeftBottom, yLeftBottom), + Imgproc.putText(frame, label, new Point(left, top), Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0)); } }