# Loading and displaying 3D wavefront (.obj) images

**URL:** <https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987>\
**Category:** Uncategorized\
**Created:** [January 20, 2021, 2:45pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987 "2021-01-20T14:45:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 20, 2021, 2:45pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/1 "2021-01-20T14:45:17Z")

</div>

OpenCV Viz seems to be able to load 3D wavefront (.obj) image as well as to display them. I was searching for some tutorial explaining the basic steps, but without success.  
I have both obj and jpg files which contains all the information to display a 3D colored image, as I obtained by using the simple 3D viewer f3d.  
Someone could be kindly explain me the basic commands for loading my 3D wavefront images and displaying them?

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [January 20, 2021, 3:42pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/2 "2021-01-20T15:42:12Z")

</div>

OpenCV is a computer vision library, not a 3D object viewer. If you’d like to display an obj file, probably there are better solutions.

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 20, 2021, 3:49pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/3 "2021-01-20T15:49:06Z")

</div>

But In static Mesh is possible Loads a mesh from a ply or a obj file.  
Maybe I misunderstood , but it seem quite close to my purpose. For me even only the loading and storing of such data would be great.

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 20, 2021, 3:58pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/5 "2021-01-20T15:58:49Z")

</div>

Thanks for the explanation!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 20, 2021, 3:59pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/6 "2021-01-20T15:59:20Z")

</div>

tutorial for the viz module:

[https://docs.opencv.org/3.4/d7/df9/tutorial\_table\_of\_content\_viz.html](https://docs.opencv.org/3.4/d7/df9/tutorial_table_of_content_viz.html)

mesh class that can load such files:

[https://docs.opencv.org/3.4/dc/d4f/classcv\_1\_1viz\_1\_1Mesh.html](https://docs.opencv.org/3.4/dc/d4f/classcv_1_1viz_1_1Mesh.html)

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 20, 2021, 4:03pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/7 "2021-01-20T16:03:38Z")

</div>

I read those links. For that I believed that opencv could manage 3D images, that is (x,y,z, color). So now I am again confused.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 20, 2021, 5:05pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/8 "2021-01-20T17:05:44Z")

</div>

try sample [opencv\_contrib/histo3D.cpp at master · opencv/opencv\_contrib · GitHub](https://github.com/opencv/opencv_contrib/blob/master/modules/viz/samples/histo3D.cpp)

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 21, 2021, 9:28am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/9 "2021-01-21T09:28:13Z")

</div>

Many thanks Laurent, but that does not solve my problem:

I obtained a wavefront file (.obj) and the related jpg images of an object of my interest by a 3D scanner.

Now I am wondering if I can load it in my C++ code by means of the OpenCV library (as an example by viz::mesh).

More precisely, of the whole obj file I am just interested to the vertex coordinates together with the colour associated at each one vertex.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 21, 2021, 9:49am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/10 "2021-01-21T09:49:04Z")

</div>

Have you try [load](https://docs.opencv.org/master/dc/d4f/classcv_1_1viz_1_1Mesh.html) in viz module? there is an obj format

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 21, 2021, 10:22am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/11 "2021-01-21T10:22:33Z")

</div>

Yes of course. I tried by the following command, but it return error

viz::Mesh Mesh=load (nameFD, LOAD\_OBJ);

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 21, 2021, 10:29am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/12 "2021-01-21T10:29:25Z")

</div>

What’s error message ? It works for me

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/a48d2f2866cf9475b7ba714038bb5f6c1190ee6d.png)

with this code :

```
viz::Viz3d window("Coordinate Frame");
window.setWindowSize(Size(500, 500));
window.setWindowPosition(Point(150, 150));
window.setBackgroundColor(); // black by default
cv::viz::Mesh x;
x = cv::viz::Mesh::load("airboat.obj",cv::viz::Mesh::LOAD_OBJ);
cv::viz::WMesh a(x);
/// Create and show widgets

window.showWidget("point_cloud", a);

/// Wait for key 'q' to close the window
cout << endl << "Press 'q' to close each windows ... " << endl;

window.spin();

```

obj file is [here](https://people.sc.fsu.edu/~jburkardt/data/obj/obj.html)

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 21, 2021, 10:37am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/13 "2021-01-21T10:37:03Z")

</div>

GREAT!!!  
I even successfully loaded my file!!!  
Now I have the last question: how to use the jpg file produced by the scanner to set the color to the image?

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 21, 2021, 12:47pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/14 "2021-01-21T12:47:41Z")

</div>

Add colors :

```
viz::Viz3d window("Coordinate Frame");
window.setWindowSize(Size(500, 500));
window.setWindowPosition(Point(150, 150));
window.setBackgroundColor(); // black by default
cv::viz::Mesh x;
x = cv::viz::Mesh::load("C:\\Users\\Laurent\\Desktop\\airboat.obj", cv::viz::Mesh::LOAD_OBJ);
Mat couleur(1,x.cloud.cols,CV_8UC3);
randu(couleur, 0, 255);
x.colors = couleur;
cv::viz::WMesh a(x);
/// Create and show widgets

window.showWidget("point_cloud", a);

/// Wait for key 'q' to close the window
cout << endl << "Press 'q' to close each windows ... " << endl;

window.spin();

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/53f706083e972b0c82311e6f54cd7cf6123cefbc.jpeg)

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 21, 2021, 4:12pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/15 "2021-01-21T16:12:12Z")

</div>

Sorry Laurent, the colors I need are the real ones of the object as taken by the 3 D scanner : as matter of fact its output is composed by 3 files, Model.obj Model.jpg. and Model.mtl.  
The jpg is quite strange because appear as composed by a main photos of the object plus other smaller part of it.  
If I load the obj file with F3D I see the object right colored.  
If I suppress the jpg, I obtain a image with gray level, as the one you shown.  
Unfortunately the upload functionality of this forum does not allow obj. I send you the jpg.

 ![Model](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/38accedbe55049c2179a998739c792305272f298.jpeg)

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 21, 2021, 5:39pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/16 "2021-01-21T17:39:55Z")

</div>

To find a color for each 3d point you need camera pose and calibration matrices relative to jpeg image

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 21, 2021, 5:40pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/17 "2021-01-21T17:40:23Z")

</div>

By googling I found this site where is explained what I need: [UV mapping - Wikipedia](https://en.wikipedia.org/wiki/UV_mapping)

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 21, 2021, 5:42pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/18 "2021-01-21T17:42:19Z")

</div>

Maybe what are you saying is the content of the mtl file, which is following:

newmtl material0  
Ka 1.000000 1.000000 1.000000  
Kd 1.000000 1.000000 1.000000  
Ks 0.000000 0.000000 0.000000  
Tr 1.000000  
illum 1  
Ns 0.000000  
map\_Kd Model.jpg

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 21, 2021, 5:46pm UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/19 "2021-01-21T17:46:59Z")

</div>

How do you get your data (harware)?

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 22, 2021, 8:28am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/20 "2021-01-22T08:28:32Z")

</div>

Good morning Laurent.  
We use the low cost device STRUCTURE by Occipital to be intsalled on an iPad. When the 3D scanning is done, an email is sent with a zip file containing the 3 files obj, mtl and jpg.

---

<div class="post-metadata">

**Author:** ![Marco\_Montecchi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/marco_montecchi/32/640_2.png) [@Marco\_Montecchi](https://forum.opencv.org/u/Marco_Montecchi)\
**Post date:** [January 22, 2021, 9:05am UTC](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987/21 "2021-01-22T09:05:38Z")

</div>

Finally I understood: I attached an image where the plot of the values of the “vt” rows of the obj file (X=vt\_1 ; Y=vt\_2) is compared with the strange jpg image. As you can see the jpg image can be use to assign the proper color to each geometrical vertex. Thais the so called UV mapping above cited.  
I suppose should be some viz function to manage the UV mapping

 ![OBJvt_VS_jpg](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/d5380c85c5446616c58b4a0bf145736ef427f2f7.jpeg)

[Next page](https://forum.opencv.org/t/loading-and-displaying-3d-wavefront-obj-images/987.md?page=2)
