Skip to content

G-API Stereo operation and tests#19732

Merged
alalek merged 1 commit intoopencv:masterfrom
aDanPin:danpin/gapi/stereo_operation_and_test
Mar 31, 2021
Merged

G-API Stereo operation and tests#19732
alalek merged 1 commit intoopencv:masterfrom
aDanPin:danpin/gapi/stereo_operation_and_test

Conversation

@aDanPin
Copy link
Copy Markdown
Contributor

@aDanPin aDanPin commented Mar 16, 2021

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
force_builders=Custom,Custom Win,Custom Mac
build_gapi_standalone:Linux x64=ade-0.1.1f
build_gapi_standalone:Win64=ade-0.1.1f
build_gapi_standalone:Mac=ade-0.1.1f
build_gapi_standalone:Linux x64 Debug=ade-0.1.1f

build_image:Custom=centos:7
buildworker:Custom=linux-1
build_gapi_standalone:Custom=ade-0.1.1f

build_image:Custom Win=openvino-2021.2.0
build_image:Custom Mac=openvino-2021.2.0

test_modules:Custom=gapi,python2,python3,java
test_modules:Custom Win=gapi,python2,python3,java
test_modules:Custom Mac=gapi,python2,python3,java

@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from 1878cdc to d19f4c4 Compare March 19, 2021 04:48
@aDanPin
Copy link
Copy Markdown
Contributor Author

aDanPin commented Mar 19, 2021

@AsyaPronina @TolyaTalamanov Please review

namespace stereo {
namespace cpu {

GAPI_EXPORTS_W GKernelPackage kernels();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it shouldn't be in python now, GAPI_EXPORTS would be enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

namespace stereo {

enum OutputFormat {
disparity,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capital letters are usually used + enum class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capital letters are usually used for enum values and mixed-case style for enum name, @TolyaTalamanov , do you agree?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OUTPUT_FORMAT -> OutputFormat
disparity -> DISPARITY

depth
};

G_TYPED_KERNEL(GStereoBM, <GMat(GMat, GMat,const int , const int, const OutputFormat, const int, const double, const double)>, "org.opencv.stereoBM") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GMat, const int , -> GMat, const int, const ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn`t get
Could you explain?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look on alignment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

};

G_TYPED_KERNEL(GStereoBM, <GMat(GMat, GMat,const int , const int, const OutputFormat, const int, const double, const double)>, "org.opencv.stereoBM") {
static GMatDesc outMeta(GMatDesc ly, GMatDesc ry, const int, const int, const OutputFormat, const int ddtype,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering what is the ddtype ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a type of output cv::Mat
ddtype -> outputType

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it just dtype

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outputType -> dtype

cv::Mat disp;
cv::StereoBM::create(numDisparities, blockSize) -> compute(left, right, disp);

// See https://answers.opencv.org/question/199915/how-to-get-depth-from-disparity-map/?sort=votes#:~:text=Z%20%3D%20B*f%20%2F%20disparity%20is%20the%20correct%20formula%20to,get%20the%20depth%20in%20mm.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe such kind of reference shouldn't be there

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

GAPI_Assert(ry.chan == 1);
GAPI_Assert(ry.depth == CV_8U);

return ddtype == CV_16U ? ly.withDepth(CV_16U) : ly.withDepth(CV_16F);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtype ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dpype -> outputType


cv::gapi::GKernelPackage cv::gapi::stereo::cpu::kernels()
{
static auto pkg = cv::gapi::kernels
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static auto pkg = cv::gapi::kernels<GCPUStereoBM>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed I hope...

#include <opencv2/gapi/cpu/stereo.hpp>
#include <opencv2/gapi/cpu/gcpukernel.hpp>

#include <iostream>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


namespace
{
#define STR_CPU [] () { return cv::compile_args(cv::gapi::use_only{cv::gapi::str::cpu::kernels()}); }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it called in another tests ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a parameterized tests
Now it is used there

@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from 7622eea to fadfe89 Compare March 19, 2021 15:50

namespace cv {
namespace gapi {
namespace ster {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ster?
Where did the stereo go?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case there is name collision of namespace stereo and operation stereo

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please discuss namespace name with the team -- to comply with OpenCV modules names we should create calib3d namespace, but we are focusing on addition of stereoscopic functionality, so may be namespace should be named as stereo or stereoscopic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cv::gapi::ster -> cv::gapi::calib3d

namespace cv {

namespace gapi {
namespace ster {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

namespace stereo {

enum OutputFormat {
disparity,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capital letters are usually used for enum values and mixed-case style for enum name, @TolyaTalamanov , do you agree?


G_TYPED_KERNEL(GStereo, <GMat(GMat, GMat, const cv::gapi::ster::OUTPUT_FORMAT, const int)>, "org.opencv.stereo") {
static GMatDesc outMeta(GMatDesc ly, GMatDesc ry,
const cv::gapi::ster::OUTPUT_FORMAT,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check that OUTPUT_FORMAT is one of existent?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

)
{
cv::Mat disp;
cv::StereoBM::create(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't create new instance each time for cv::StereoBM if you have opportunity to use state...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from 9ed4aa5 to a1a2e73 Compare March 23, 2021 13:30

#include <opencv2/calib3d.hpp>

GAPI_OCV_KERNEL_ST(GCPUStereo, cv::gapi::ster::GStereo, cv::gapi::ster::StereoSetup)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StereoSetup may be just a structure to initialize state and state may be just StereoBM class, don't you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have to keep focus and baseLine to calculate depths from disparity
Easier way to keep it all in StereoSetup structure, I think...
Or what do you mean?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split the StereoSetup structure from cv::StereoBM. Lets cv::StereoBM be implementation details and be hidden from user. Lets cv::StereoBM be the state and StereoSetup be the initialization structure.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can't retrieve focus and baseline from the cv::StereoBM, then create a structure to hold cv::StereoBM and focus + baseline. Lets this structure be the state and StereoSetup be the structure to just initialize the state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


enum class OutputFormat {
DISPARITY,
DEPTHS
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEPTH

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


namespace calib3d {

struct StereoSetup {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this out of calib3d namespace.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also see how the initialization parameters are declared and documented here: https://github.com/opencv/opencv/blob/master/modules/gapi/include/opencv2/gapi/video.hpp#L22
Suggest to declare and document them the same way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done I hope...


#include <opencv2/calib3d.hpp>

GAPI_OCV_KERNEL_ST(GCPUStereo, cv::gapi::calib3d::GStereo, cv::gapi::calib3d::StereoSetup)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my answers in discussion about cv::gapi::calib3d::StereoSetup as a state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done I hope...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why it is stateful?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its stateful for not to create cv::gapi::calib3d::StereoSetup every time kernel is called

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also its needed for keep approved kernel API and also have opportunity to configure blockSize, numDisparity and etc.


@param ly Y plane from left camera: 8-bit unsigned 1-channel matrix of @ref CV_8UC1 type
@param ry Y plane from right camera: 8-bit unsigned 1-channel matrix of @ref CV_8UC1 type
@param oF the enum class specifies do disparity to depth conversion or not
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum class to specify to make disparity to depth conversion or not

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


} // namespace calib3d

/** @brief Extracts disparity or, optionally, depth information from stereo-pair.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally is extra here.
We provide return type exclusively: user can retrieve either disparity or depth

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract disparity/depth information depending on passed StereoOutputFormat argument

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix detailed description the same way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


template<> struct CompileArgTag<cv::gapi::StereoInitParam> {
static const char* tag() {
return "org.opencv.stereoSetup";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stereoInit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

@dmatveev dmatveev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good overall! Some clarification is required though.

Comment on lines +35 to +40
/** @brief Structure for the Stereo operation setup parameters.*/
struct GAPI_EXPORTS StereoSetup {
double baseline;
double focus;
cv::Ptr<cv::StereoBM> stereoBM;
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where it is used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is state in stateful kernel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be hidden if it is not yet

@param ly Y plane from left camera: 8-bit unsigned 1-channel matrix of @ref CV_8UC1 type
@param ry Y plane from right camera: 8-bit unsigned 1-channel matrix of @ref CV_8UC1 type
@param oF the enum class to specify to make disparity to depth conversion or not
@param dtype is a type of output @ref cv::Mat. Can be @ref CV_16U or @ref CV_16F
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this apply to both output types? (Disparity vs. depth)


#include <opencv2/calib3d.hpp>

GAPI_OCV_KERNEL_ST(GCPUStereo, cv::gapi::calib3d::GStereo, cv::gapi::calib3d::StereoSetup)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why it is stateful?

disp = (stereoSetup.focus * stereoSetup.baseline) / disp;
}

disp.convertTo(out_disp, dtype);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so is disp ok with such dtype? does it conform to the ocv behavior?

@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from e0cf9ca to d8ef570 Compare March 30, 2021 06:43
@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from 370e9ea to 53d6089 Compare March 31, 2021 14:18
@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from 214eb27 to fee2816 Compare March 31, 2021 14:48
@aDanPin aDanPin force-pushed the danpin/gapi/stereo_operation_and_test branch from 4b2b505 to e6f52b0 Compare March 31, 2021 17:10
@dmatveev dmatveev self-assigned this Mar 31, 2021
@dmatveev dmatveev added this to the 4.5.2 milestone Mar 31, 2021
Copy link
Copy Markdown
Contributor

@dmatveev dmatveev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 approved, please merge it as-is

Comment on lines +18 to +21
DEPTH_FLOAT16,
DEPTH_FLOAT32,
DISPARITY_FIXED16_11_5,
DISPARITY_FIXED16_12_4
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is questionable, at least I'd give some better names to this enum in the future iteration (after merge).

FLOAT16 -> F16, FLOAT32 -> F32, FIXED16_ -> Q (as for quantized).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you'll do this, please don't break the existing enumerations, simply provides aliases with the same name (cc: @AsyaPronina @rgarnov )

@alalek alalek merged commit 9f13fdb into opencv:master Mar 31, 2021
@dmatveev
Copy link
Copy Markdown
Contributor

Fantastic! Thanks a lot @aDanPin, @AsyaPronina, @rgarnov for your work! And kudos to @alalek for merging this!

DISPARITY_FIXED16_12_4
};

namespace calib3d {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calib3d

In OpenCV 5.0 calib3d is gone (see the "next" branch and PR #18940).
Used functionality is moved to "stereo" namespace.

Perhaps it makes sense to use new names for namespaces and functions instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Alexander,

This namespace here is used only to hold an Operation's type -- the user code is supposed to use cv::gapi::stereo instead so we can move these types around easily with no visible impact

-D

@alalek alalek mentioned this pull request Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants