Skip to content

OpenCV 3D module rendering function PR#24065

Closed
nickyu-zhu wants to merge 1762 commits into
opencv:5.xfrom
nickyu-zhu:Ningyu-dev
Closed

OpenCV 3D module rendering function PR#24065
nickyu-zhu wants to merge 1762 commits into
opencv:5.xfrom
nickyu-zhu:Ningyu-dev

Conversation

@nickyu-zhu
Copy link
Copy Markdown

Pull Request for GSoC 2023 Project5 - Simple Triangle Rendering

This is a pull request for OpenCV 3d module in 5.x branch, which is the work of GSoC 2023 project 5. The changes are demonstrated down below.

  1. Create rendering.cpp and rendering.hpp files in opencv/modules/src folder, which can render the input triangles through the triangleRastrize() function. This new function can now perform depth and color rendering successfully
  2. Create test_rendering.cpp file in the opencv/modules/test folder, which test the triangleRastrize() function in 4 different ways, (including empty set, constant color rendering, interpolated color rendering and clipping test rendering)
  3. Add three new files in opencv/sample/opengl folder. The names are opengl_clipping_sample.cpp, opengl_color_sample.cpp and opengl_depth._sample.cpp. These files are set to be the OpenGL ground truth samples for the rendering image.

Harvey and others added 30 commits October 21, 2021 18:13
* bmp specified BI_BITFIELDS should take care RGBA bit mask

* support xrgb bmp file
add test case for "tf/reduce_sum"

* fix output dimension of reduce_sum

* add test case and fix bug

* add more cases

* style:change files name

* fix a little bug
Add regression test for QRcode encoding and decoding

Co-authored-by: APrigarina <ann73617@gmail.com>
Signed-off-by: Crayon-new <1349159541@qq.com>
Onnx conformance tests

* Add ONNX conformance tests

* dnn(onnx): make conformance stubs a part of repository

Co-authored-by: Alexander Alekhin <alexander.a.alekhin@gmail.com>
Signed-off-by: Crayon-new <1349159541@qq.com>
Copy link
Copy Markdown
Contributor

@savuor savuor left a comment

Choose a reason for hiding this comment

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

This is a good progress. There's a couple of things to fix:

  1. My comments
  2. Broken CI tests

Comment thread modules/3d/include/opencv2/3d.hpp Outdated
*/
CV_EXPORTS_W void saveMesh(const String &filename, InputArray vertices, InputArray normals, InputArrayOfArrays indices);

/*
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 comment does not render to docs.
You can always check the generated documentation for the PR here (Builders -> default -> PR number -> Docs -> Upload docs -> Preview).

Comment thread modules/3d/src/rendering.hpp Outdated
@@ -0,0 +1 @@

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.

We don't need empty files, please remove

Comment thread modules/3d/test/test_rendering.cpp Outdated
{
if (debugLevel > 0)
{
Mat groundTruth = imread("../../../opencv_extra/opencv_extra/testdata/rendering/example_image_depth_1.png");
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 file path will work on your machine only.
We have a special function to obtain a path:

// The file path is OPENCV_TEST_DATA_PATH + "/rendering/example_image_depth_1.png"
std::string dataPath = cvtest::TS::ptr()->get_data_path() + "/rendering/example_image_depth_1.png";

The test executables should be run with env variable OPENCV_TEST_DATA_PATH set to testdata folder

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.

findDataFile() should be used.

Comment thread modules/3d/src/rendering.cpp Outdated

std::vector<Vec3f> verticesVector = vertices.getMat();
std::vector<Vec3f> indicesVector = indices.getMat();
std::vector<Vec3f> colorsVector = colors.getMat();
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.

We should allow an empty vertex colors array. In that case all the triangles will be drawn by the same color (let it be full white).

Comment thread modules/3d/include/opencv2/3d.hpp Outdated
* */
CV_EXPORTS void triangleRasterize(InputArray vertices, InputArray indices,
InputArray colors, InputArray cameraMatrix, int width, int height, bool shadingMode,
OutputArray depth_buf, OutputArray color_buf);
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.

OK, the thing happened to cameraMatrix is not what I was talking about. It should be changed:

  • The way of passing camera pose by lookAt, position and up vector is not an OpenCV-style, we use camera pose matrix for that (in your code this is called lookAtMatrix
  • The intrinsic camera parameters are also passed in different way, by 3x3 intrinsic matrix
    I'll rewrite the doc string, and you are to change the function signature according to it.
Renders a set of triangles on a depth and/or RGB image. The output images are not cleared before the rendering and can be used for masking or with pre-filled Z-buffer.
Triangles can be flat-shaded or have interpolated colors between vertices. In flat-shaded mode a color of 1st triangle vertex is used.
If no colors are given, the triangles are drawn in white (1.0, 1.0, 1.0).

@param vertices vertices coordinates array. Should contain values of CV_32FC3 type or a compatible one (e.g. cv::Vec3f, etc.)
@param indices triangle vertices index array, 3 per triangle. Each index indicates a vertex in a vertices array. Should contain CV_32SC3 values
@param colors per-vertex colors of CV_32FC3 type, can be empty.
@param cameraPose a 4x3 or 4x4 float matrix containing camera pose
@param fovY field of view in vertical direction, given in degrees
@param zNear minimum Z value to render, everything closer is clipped
@param zFar maximum Z value to render, everything farther is clipped
@param width Frame width
@param height Frame height
@param useFlatShading Enables or disables flat shading 
*@param depthBuf a width x height array of floats containing resulting Z buffer. Reused if not empty. Created and filled by zFar values if a user-provided array is empty. To disable Z buffer output, pass cv::noArray() here.
*@param colorBuf a width x height array of CV_32FC3 representing the final rendered image. Reused if not empty. Created and filled by zeroes if a user-provided array is empty. To disable color output, pass cv::noArray() here.
CV_EXPORTS  void triangleRasterize(InputArray vertices, InputArray indices,
    InputArray colors, InputArray cameraPose, float fovY, float zNear, float zFar,
int width, int height, bool useFlatShading,
    OutputArray depthBuf=noArray(), OutputArray colorBuf=noArray());

Comment thread modules/3d/src/rendering.cpp Outdated
Vec3f position = camera.row(0);
Vec3f lookat = camera.row(1);
Vec3f upVector = camera.row(2);
float fovy = camera.at<float>(3, 0), zNear = camera.at<float>(3, 1), zFar = camera.at<float>(3, 2);
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 change this according to new function signature

Comment thread modules/3d/src/rendering.cpp Outdated
Matx44f mvpMatrix = perspectMatrix * lookAtMatrix;

Mat depth_buf_mat = depth_buf.getMat();
Mat color_buf_mat = color_buf.getMat();
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.

  • Both depthBuf and colorBuf OutputArray arguments should be checked for size, type, empty() and needed()
  • If they are empty, they should be created with given width and height
  • If color buffer is not needed then the color rendering should be disabled
  • If depth buffer is not needed then we still need it during rendering, just don't pass it outside

Comment thread modules/3d/src/rendering.cpp Outdated
return Vec3f(a[0] / length, a[1] / length, a[2] / length);
}

Matx44f lookAtMatrixCal(const Vec3f& position, const Vec3f& lookat, const Vec3f& upVector)
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.

As I said earlier, this function makes more sense in test/perf files rather than here.
As an option, we can expose it to public interface

Comment thread modules/3d/src/rendering.cpp Outdated
if (ACcrossAB.dot(ACcrossAP) >= 0 && ABcrossAC.dot(ABcrossAP) >= 0)
{
float beta = norm(ACcrossAP) / norm(ACcrossAB);
float gamma = norm(ABcrossAP) / norm(ABcrossAC);
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.

norm(ACcrossAB) == norm(ABcrossAC), this can be optimized

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 the triangle is degenerate or close to, we'll get division by zero problem, this should be avoided

Comment thread modules/3d/src/rendering.cpp Outdated
if (ACcrossAB.dot(ACcrossAP) >= 0 && ABcrossAC.dot(ABcrossAP) >= 0)
{
float beta = norm(ACcrossAP) / norm(ACcrossAB);
float gamma = norm(ABcrossAP) / norm(ABcrossAC);
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.

The same about div by zero

fengyuentau and others added 4 commits September 6, 2023 17:03
VIT track(gsoc realtime object tracking model) opencv#1088

add an onnx model for opencv vttrack PR opencv#24201
* add converted conformance gemm tests

* update to modern googlenet with new outputs

* correct filename

* replace model link with the one from org gdrive

* drop googlenet 2023
Comment thread samples/opengl/opengl.cpp Outdated
#include <windows.h>
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1
#include <windows.h>
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.

avoid unnecessary changes

Comment thread modules/3d/src/rendering.cpp Outdated
@@ -0,0 +1,212 @@
#include "precomp.hpp"
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.

Missing license header

Comment thread modules/3d/src/rendering.cpp Outdated
Comment on lines +3 to +8
namespace cv {

struct Triangle
{
Vec4f vertices[3];
Vec3f color[3];
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.

avoid indentation in namespaces (all files)

Comment thread modules/3d/test/test_rendering.cpp Outdated
{
if (debugLevel > 0)
{
Mat groundTruth = imread("../../../opencv_extra/opencv_extra/testdata/rendering/example_image_depth_1.png");
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.

findDataFile() should be used.

Comment thread samples/opengl/opengl_color_sample.cpp Outdated
struct DrawData
{
ogl::Arrays arr;
ogl::Buffer indices;
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.

Don't use tabs in source code.
Follow OpenCV coding style guidelines.

Abdurrahheem and others added 18 commits September 22, 2023 13:05
* add expand conformance tests

* fix clip-vit-base-head model and its output
Model and test data for the fix of shared const inputs
dnn: download_models script improvements opencv#1084

Added new features:
* Use argument parser and new arguments:
  * `--cache` directory will be tried before downloading from internet
  * `--dst` change destination directory
  * `--list` list all model names
  * `--cleanup` remove all `tar.gz` files afterwards
* Add hierarchy to model structure, now there are regular models which can be downloaded directly and archived models which should be downloaded and then required files will be extracted. Archived files will be checked for existence before downloading whole archive - it allows to populate them from cache and avoid storing archives on disk.
* Filters are now case-insensitive and use _contains_ rule for matching, multiple filters can be used in the command line, results will be united.
* GDrive object simplified to separate method - allows to reduce repeated download code. Better solution would be to extract all download-store-verify functionality to separate class(es), but I decided to postpone this step for the future improvements.
* Use `pathlib.Path` for most paths.

I have doubts regarding filter arguments: should we keep them positional arguments or change to keyword arguments (e.g. `--filter`)?

Examples:
```.sh
# Populate cache and remove archives afterwards (~4Gb)
./download_models.py --dst /data/dnn --cleanup
# Download models to the specified folder using cache
./download_models.py --cache /data/dnn --dst $HOME/opencv_extra/testdata/dnn
# List all models having face in their name
./download_models.py --list face
```
Updated performance test sanity data for PR opencv/opencv:24333.
Merge with opencv#24386

Also noted that random seed is added in this pr, so previous data is regenerated to keep consistency.
@savuor
Copy link
Copy Markdown
Contributor

savuor commented Oct 27, 2023

Reopened as #24459

@savuor savuor mentioned this pull request Oct 27, 2023
6 tasks
asmorkalov pushed a commit that referenced this pull request Feb 19, 2024
Triangle rasterization function #24459

#24065 reopened since the previous one was automatically closed after rebase
Connected PR with ground truth data: [#1113@extra](opencv/opencv_extra#1113)

### 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
- [x] 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
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.