Creating a 3D relief of an Image?

Everything related to 3D programming
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Creating a 3D relief of an Image?

Post by SMaag »

Did anyone of you tried to create a 3D-relief of an Image.

Using the Pixel coordinate x/y as 3D x/y and the gray color value as Z-cooridante?

My idea is:
1. creating a 3D relief of the gray Image
2. now use the colored Image as texture

I hope this will creates a kind of 3D Picture!
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Creating a 3D relief of an Image?

Post by jacdelad »

I've never heard of "using the gray color value as Z-coordinate". What do you mean by this, calculating the gray value of a pixel (=converting it into gray-scale) and higher value is closer pixel? Afaik, this won't work.
What you can do is using calculation the gradients for each pixel and use this as a "height map". Don't ask me how to exactly do this, but that's how the software of the inspection machines I use do this to visualize PCB parts.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Creating a 3D relief of an Image?

Post by wilbert »

SMaag wrote: Thu Jul 04, 2024 8:01 pm Did anyone of you tried to create a 3D-relief of an Image.
Sort of but not in this way.

I am interested in stereo photography where you take separate pictures for the left and right eye to give the illusion of depth.
When you only have a single image, adding depth (what you are trying to do) can be accomplished by monocular depth estimation .
If you don't need to do the conversion automatically with PureBasic code, you could try this approach.
Some monocular depth estimation models that are used for this task are
MiDAS, ZoeDepth, and Depth Anything
and there are also online tools for this depth estimation that can help you with the conversion.
Windows (x64)
Raspberry Pi OS (Arm64)
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Creating a 3D relief of an Image?

Post by DarkDragon »

wilbert wrote: Fri Jul 05, 2024 5:57 am
SMaag wrote: Thu Jul 04, 2024 8:01 pm Did anyone of you tried to create a 3D-relief of an Image.
Sort of but not in this way.

I am interested in stereo photography where you take separate pictures for the left and right eye to give the illusion of depth.
When you only have a single image, adding depth (what you are trying to do) can be accomplished by monocular depth estimation .
If you don't need to do the conversion automatically with PureBasic code, you could try this approach.
Some monocular depth estimation models that are used for this task are
MiDAS, ZoeDepth, and Depth Anything
and there are also online tools for this depth estimation that can help you with the conversion.
https://www.purebasic.fr/english/viewtopic.php?p=378306

However your first post really just described a terrain
bye,
Daniel
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Creating a 3D relief of an Image?

Post by dige »

/Update: Thx Darkdragon, that's exactly what I meant/

I'm not quite sure what you want to do, or what your initial image is. Maybe you want to create and visualise a depth map?

Then search the forum please for 'Create depth map' from two images (stereo pair). The generated greyscale image is suitable for generating a 3D terrain

Image

or an image with 3D effect for Facebook like this: https://de-de.facebook.com/help/iphone- ... 5416095269
"Daddy, I'll run faster, then it is not so far..."
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Creating a 3D relief of an Image?

Post by SMaag »

Yes, I want to create a deepth map and I'm intereseted in the way to do!
As I see from your answers: just using gray scale value will not work!

I'd like to have a result similar to the Face Wizard Software what you can see here:
https://www.youtube.com/watch?v=AlRpf1Dn9h8

It creates a 3D relief from the foto of a face!

I'd like to see what happens when I use the original Foto as texture for the created 3D-Relief.

The goal is:
1. creating a 3D-relief from a 2D Foto
2. wrap this relief on a 3D-Zylinder (engrave it in a cylinder)
3. put the original foto as texture on this engraved cylinder
4. Rotate the cylinder and do camera moves

So my first things to do are:

1. creating a deepth map with using a deepth map alogrithm.
(This algorithm I have to find an convert it to Purebasic. Or anyone of you know a PB-Version?)

2. creating a Mesh from the Image using the deepth map
(I guess this will be not the biggest problem)

3. Wrapping that Mesh on a clyinder
(I guess there is no PB/OGRE integrated way to do!
So I have to make my own code for that, what might be complicated)

4. Scale the Foto to the correct size and use it as texture
(May be not so easy but I guess, I'm able to do!)
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 385
Joined: Thu Jul 09, 2015 9:07 am

Re: Creating a 3D relief of an Image?

Post by pf shadoko »

maybe you just want this
(the height is determined by the red component)

Code: Select all

Procedure.l cola(col,a=$ff)
  ProcedureReturn col|(a<<24)
EndProcedure

InitEngine3D():InitSprite():InitKeyboard():InitMouse()
OpenWindow(0, 0, 0, 0,0, "[F12]: Wireframe - [Esc]: Quit - MouseWheel: Zoom",#PB_Window_Maximize)
ex=WindowWidth (0,#PB_Window_InnerCoordinate)
ey=WindowHeight(0,#PB_Window_InnerCoordinate)
OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)


CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,100,-150)
CreateLight(0,$888888, 10000, 5000, 2000)
CameraBackColor(0,$ff8888) 
AmbientColor($aaaaaa)

n=256:n1=n-1
;------------------------ image creation
CreateImage(0,n,n,32)
StartVectorDrawing(ImageVectorOutput(0))
For i=0 To n Step 20
  For j=0 To n Step 20
    x=i+Random(20)
    y=j+Random(20)
    r=5+Random(30)
    c=Random($ffffff)
    VectorSourceCircularGradient(x, y, r)
    VectorSourceGradientColor(cola(c,128),0.0)
    VectorSourceGradientColor(cola(c,64),0.8)
    VectorSourceGradientColor(cola(c,0 ),1.0)     
    AddPathCircle(x,y,r)
    FillPath()
  Next
Next    
StopVectorDrawing()

;------------------------ mesh creation
Dim t.MeshVertex(n,n)
StartDrawing(ImageOutput(0))
For j=0 To n
  For i=0 To n
    With t(i,j)      
      color=Point(i & n1,j & n1)
    \x=-(i-n/2)
    \z=(n/2-j)
    \y=Red(color)/5
    \color=color
    EndWith 
  Next
Next
StopDrawing()

CreateDataMesh(0,t(),#PB_Mesh_DiagonalClosestNormal +8)
CreateMaterial(0,0)
SetMaterialColor(0,#PB_Material_AmbientColor|#PB_Material_DiffuseColor,-1)
CreateEntity(0,MeshID(0),MaterialID(0))
;------------------------

dist=150
Repeat
  ExamineMouse()
  ExamineKeyboard()
    dist+(Bool(KeyboardPushed(#PB_Key_Down)<>0)-Bool(KeyboardPushed(#PB_Key_Up   )<>0))*1-MouseWheel()*10
    If KeyboardReleased(#PB_Key_F12):fdf=1-fdf:If fdf:CameraRenderMode(0,#PB_Camera_Wireframe):Else:CameraRenderMode(0,#PB_Camera_Textured):EndIf:EndIf
  a.f+0.002
  MoveCamera(0,Cos(a)*dist,dist*1,Sin(a)*dist,0)
  CameraLookAt(0,0,0,0)
  RenderWorld()
  FlipBuffers()    
  While WindowEvent():Wend
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Creating a 3D relief of an Image?

Post by SMaag »

Tanks for the Code.
Your demo shows what I want to do. Creating a Z coordinate out from the Pixel color.
First ist does not matter which alogrithm I use. In my opinion it was just the grey color value to get the deepth.

But from the answers I learned: deepth Information of an Image is more complex operation.

Generally I was interested if there is any expirience in that matter, so I do not have to figure out all the basics by myselfe.

What maybe you are expert for is:
- how to wrap such a Map on a 3D Cylinder or Sphere Entitiy!
Is there an "easy way" with OGRE like a CmbineEntity() or I have to calculate this point by point in the hard way with 3D Vector geometry?
(I do not need a ready code, just the way how to do!)
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Creating a 3D relief of an Image?

Post by DarkDragon »

Getting depth information from stereo images isn't trivial. Many algorithms exist, some are based on machine learning (a UNet like structure would be appropriate). A simple approach was shown in my post, it simply takes the least resistant path of matching colors between two scanlines of the left/right images. This requires exact positioning though and a calibrated camera would be good to have.

For wrapping the depth map around a sphere or cylinder you need a parametric space, e.g. you apply the depth information to the radius along the polar coordinates on your sphere/cylinder.
bye,
Daniel
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Creating a 3D relief of an Image?

Post by wilbert »

DarkDragon wrote: Fri Jul 05, 2024 1:41 pm Getting depth information from stereo images isn't trivial. Many algorithms exist, some are based on machine learning (a UNet like structure would be appropriate).
As far as I understand in this case it is about getting depth information out of a single image which is pretty complicated.
Here's a page I found with some information about it.
https://medium.com/@patriciogv/the-stat ... 245d51a315
Windows (x64)
Raspberry Pi OS (Arm64)
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Creating a 3D relief of an Image?

Post by DarkDragon »

Then you need machine learning .. I'd assume a UNet architecture would be possible. Convolutional layers and inverse convolutional layers with residual connections.

However I somehow don't understand this whole topic because of the names. Is OP now SMaag or wilbert or dige or are they all the same? The original poster wanted a terrain, the next one stereoscopy, the next wrapping around a cylinder, the next monocular depth estimation, .. I guess the next one wants structure from motion. Is this topic cut from somewhere else with more context?
bye,
Daniel
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Creating a 3D relief of an Image?

Post by wilbert »

DarkDragon wrote: Fri Jul 05, 2024 2:55 pm Is OP now SMaag or wilbert or dige or are they all the same? The original poster wanted a terrain, the next one stereoscopy, the next wrapping around a cylinder, the next monocular depth estimation
I don't have multiple identities on this forum; only one name.
I also didn't ask for stereoscopy.
I just pointed out it is something I am interested in and that monocular depth estimation can be used to get depth information if you only have a single image.
For some old photos from decades ago, I have obtained a depth map that was estimated using monocular depth estimation and converted the source image + depth map into a stereoscopic image using a free software application called "StereoPhoto Maker".
It is however still hit and miss. Sometimes it works pretty well and other times the depth estimation contains a lot of flaws.
Windows (x64)
Raspberry Pi OS (Arm64)
SMaag
Enthusiast
Enthusiast
Posts: 302
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Creating a 3D relief of an Image?

Post by SMaag »

Many thanks to all!
I see now, my original idea is not possible!


I will play arroaund a little with the colors to Z and see what will happen when I use the original foto as texture for this 3D Structure!
It's just for fun!
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Creating a 3D relief of an Image?

Post by #NULL »

SMaag wrote: Sat Jul 06, 2024 1:46 pm I will play arroaund a little with the colors to Z
I made a toy once that does something like this. isometric wireframe with pixel brightness as z coordinate, or something like that. So no actual 3D, no depthmap.

german forum: 3D FunktionsPlotting - 3d darstellung aus bildern generieren

a couple of problems though:
- download link give browser warning. you can ignore it or edit the link url from http to https before downloading.
- the code does not run right away with newer PB versions.
- the compiled exe in the download is from Window XP times, so not sure if its runs on newer Windows versions.
good luck :)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Creating a 3D relief of an Image?

Post by wilbert »

SMaag wrote: Sat Jul 06, 2024 1:46 pm I will play arroaund a little with the colors to Z and see what will happen when I use the original foto as texture for this 3D Structure!
It's just for fun!
You mentioned before that you want it to look like engraved in a cylinder.
Maybe you could apply an emboss filter to a photo and use that as a depth map.
I don't know if it would work but the result of an emboss filter looks a bit like an engraving.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply