1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #25221 from savuor:rv/bench_3d

OBJ and PLY loaders extention to support texture coordinates and difused colors #25221

### This PR changes
* Texture coordinates support added to `loadMesh()` and `saveMesh()`
* `loadMesh()` changes its behavior: all vertex attribute arrays (vertex coordinates, colors, normals, texture coordinates) now have the same size and same-index corresponce
  - This makes sense for OBJ files where vertex attribute arrays are independent from each other and are randomly accessed when defining faces
  - Looks like this behavior may also happen in some PLY files; however, it is not implemented until we encounter such files in a wild nature
  - At the same time `loadPointCloud()` keeps its behavior and loads vertex attributes as they are given in the file
* PLY loader supports synonyms for the properties: `diffuse_red`, `diffuse_green` and `diffuse_blue` along with `red`, `green` and `blue`
* `std::vector<cv::Vec3i>` supported as an index array type
* Colors are loaded as [0, 1] floats instead of uchars
  - Since colors are usually saved as floats, internal conversion to uchar at loading significantly drops accuracy
  - Performing uchar conversion does not always makes sense and can be performed by a user if they needs it
* PLY loading fixed: wrong offset ruined x coordinate
* Python tests added for `loadPointCloud` and `loadMesh`

### 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
This commit is contained in:
Rostislav Vasilikhin
2024-04-04 10:04:52 +02:00
committed by GitHub
parent 65074651a4
commit 82038be4cd
12 changed files with 632 additions and 188 deletions
+50 -48
View File
@@ -11,39 +11,40 @@
namespace opencv_test { namespace {
TEST(PointCloud, LoadObj)
TEST(PointCloud, LoadPointCloudObj)
{
std::vector<cv::Point3f> points_gold = {
{-5.93915f, -0.13257f, 2.55837f},
{-5.93915f, 1.86743f, 2.55837f},
{-5.93915f, -0.13257f, 2.55837f},
{-5.93915f, 1.86743f, 2.55837f},
{-5.93915f, -0.13257f, -1.16339f},
{-5.93915f, 1.86743f, -1.16339f},
{0.399941f, -0.13257f, 2.55837f},
{0.399941f, 1.86743f, 2.55837f},
{-5.93915f, 1.86743f, -1.16339f},
{0.399941f, -0.13257f, 2.55837f},
{0.399941f, 1.86743f, 2.55837f},
{0.399941f, -0.13257f, -1.16339f},
{0.399941f, 1.86743f, -1.16339f}};
{0.399941f, 1.86743f, -1.16339f}
};
std::vector<cv::Point3f> normals_gold = {
{-1.0000f, 0.0000f, 0.0000f},
{0.0000f, 0.0000f, -1.0000f},
{1.0000f, 0.0000f, 0.0000f},
{0.0000f, 0.0000f, 1.0000f},
{0.0000f, -1.0000f, 0.0000f},
{0.0000f, 1.0000f, 0.0000f}};
{-1.0000f, 0.0000f, 0.0000f},
{ 0.0000f, 0.0000f, -1.0000f},
{ 1.0000f, 0.0000f, 0.0000f},
{ 0.0000f, 0.0000f, 1.0000f},
{ 0.0000f, -1.0000f, 0.0000f},
{ 0.0000f, 1.0000f, 0.0000f}
};
std::vector<cv::Point3_<uchar>> rgb_gold = {
{19, 144, 149},
{219, 28, 216},
{218, 157, 101},
{11, 161, 78},
{248, 183, 214},
{63, 196, 6},
{165, 190, 153},
{89, 203, 11}};
std::vector<cv::Point3f> rgb_gold = {
{0.0756f, 0.5651f, 0.5829f},
{0.8596f, 0.1105f, 0.8455f},
{0.8534f, 0.6143f, 0.3950f},
{0.0438f, 0.6308f, 0.3065f},
{0.9716f, 0.7170f, 0.8378f},
{0.2472f, 0.7701f, 0.0234f},
{0.6472f, 0.7467f, 0.5981f},
{0.3502f, 0.7954f, 0.0443f}
};
std::vector<cv::Point3f> points;
std::vector<cv::Point3f> normals;
std::vector<cv::Point3_<uchar>> rgb;
std::vector<cv::Point3f> points, normals, rgb;
auto folder = cvtest::TS::ptr()->get_data_path();
cv::loadPointCloud(folder + "pointcloudio/orig.obj", points, normals, rgb);
@@ -65,8 +66,7 @@ TEST(PointCloud, LoadObjNoNormals)
{0.399941f, -0.13257f, -1.16339f},
{0.399941f, 1.86743f, -1.16339f}};
std::vector<cv::Point3f> points;
std::vector<cv::Point3f> normals;
std::vector<cv::Point3f> points, normals;
auto folder = cvtest::TS::ptr()->get_data_path();
cv::loadPointCloud(folder + "pointcloudio/orig_no_norms.obj", points, normals);
@@ -77,9 +77,7 @@ TEST(PointCloud, LoadObjNoNormals)
TEST(PointCloud, SaveObj)
{
std::vector<cv::Point3f> points_gold;
std::vector<cv::Point3f> normals_gold;
std::vector<cv::Point3_<uchar>> rgb_gold;
std::vector<cv::Point3f> points_gold, normals_gold, rgb_gold;
auto folder = cvtest::TS::ptr()->get_data_path();
auto new_path = tempfile("new.obj");
@@ -87,9 +85,7 @@ TEST(PointCloud, SaveObj)
cv::loadPointCloud(folder + "pointcloudio/orig.obj", points_gold, normals_gold, rgb_gold);
cv::savePointCloud(new_path, points_gold, normals_gold, rgb_gold);
std::vector<cv::Point3f> points;
std::vector<cv::Point3f> normals;
std::vector<cv::Point3_<uchar>> rgb;
std::vector<cv::Point3f> points, normals, rgb;
cv::loadPointCloud(new_path, points, normals, rgb);
@@ -101,9 +97,7 @@ TEST(PointCloud, SaveObj)
TEST(PointCloud, LoadSavePly)
{
std::vector<cv::Point3f> points;
std::vector<cv::Point3f> normals;
std::vector<cv::Point3_<uchar>> rgb;
std::vector<cv::Point3f> points, normals, rgb;
auto folder = cvtest::TS::ptr()->get_data_path();
std::string new_path = tempfile("new.ply");
@@ -111,9 +105,7 @@ TEST(PointCloud, LoadSavePly)
cv::loadPointCloud(folder + "pointcloudio/orig.ply", points, normals, rgb);
cv::savePointCloud(new_path, points, normals, rgb);
std::vector<cv::Point3f> points_gold;
std::vector<cv::Point3f> normals_gold;
std::vector<cv::Point3_<uchar>> rgb_gold;
std::vector<cv::Point3f> points_gold, normals_gold, rgb_gold;
cv::loadPointCloud(new_path, points_gold, normals_gold, rgb_gold);
@@ -125,21 +117,31 @@ TEST(PointCloud, LoadSavePly)
TEST(PointCloud, LoadSaveMeshObj)
{
std::vector<cv::Point3f> points;
std::vector<cv::Point3f> normals;
std::vector<cv::Point3f> points, normals, colors;
std::vector<cv::Point2f> texCoords;
std::vector<std::vector<int32_t>> indices;
auto folder = cvtest::TS::ptr()->get_data_path();
std::string new_path = tempfile("new_mesh.obj");
cv::loadMesh(folder + "pointcloudio/orig.obj", points, indices, normals);
cv::saveMesh(new_path, points, indices, normals);
cv::loadMesh(folder + "pointcloudio/orig.obj", points, indices, normals, colors, texCoords);
EXPECT_FALSE(points.empty());
EXPECT_FALSE(indices.empty());
EXPECT_FALSE(normals.empty());
EXPECT_FALSE(colors.empty());
EXPECT_FALSE(texCoords.empty());
cv::saveMesh(new_path, points, indices, normals, colors, texCoords);
std::vector<cv::Point3f> points_gold;
std::vector<cv::Point3f> normals_gold;
std::vector<cv::Point3f> points_gold, normals_gold, colors_gold;
std::vector<cv::Point2f> texCoords_gold;
std::vector<std::vector<int32_t>> indices_gold;
cv::loadMesh(new_path, points_gold, indices_gold, normals_gold);
cv::loadMesh(new_path, points_gold, indices_gold, normals_gold, colors_gold, texCoords_gold);
EXPECT_FALSE(points_gold.empty());
EXPECT_FALSE(indices_gold.empty());
EXPECT_FALSE(normals_gold.empty());
EXPECT_FALSE(colors_gold.empty());
EXPECT_FALSE(texCoords_gold.empty());
EXPECT_EQ(normals_gold, normals);
EXPECT_EQ(points_gold, points);
@@ -156,7 +158,7 @@ TEST_P(PlyTest, LoadSaveMesh)
std::string fname = GetParam();
std::vector<cv::Point3f> points_gold, normals_gold, colors_gold;
std::vector<std::vector<int32_t>> indices_gold;
std::vector<cv::Vec3i> indices_gold;
auto folder = cvtest::TS::ptr()->get_data_path();
std::string new_path = tempfile("new_mesh.ply");
@@ -168,7 +170,7 @@ TEST_P(PlyTest, LoadSaveMesh)
cv::saveMesh(new_path, points_gold, indices_gold, normals_gold, colors_gold);
std::vector<cv::Point3f> points, normals, colors;
std::vector<std::vector<int32_t>> indices;
std::vector<cv::Vec3i> indices;
cv::loadMesh(new_path, points, indices, normals, colors);
if (!normals.empty())