File Exchange

image thumbnail

Carving a Dinosaur

version 1.1.0.1 (20.1 KB) by Ben Tordoff
This is a demo of reconstructing a 3D shape from multiple images using space-carving.

12 Downloads

Updated 01 Sep 2016

View License

This is a demo of reconstructing a 3D shape from multiple images using a simple space-carving approach. This technique is usually used when you need a 3D model of a small artefact which can be placed on a turntable, allowing dozens, even hundreds of images to be captured from around the object. It has been used pretty successfully by museums and the like to create online virtual galleries.
Each image is just used as a mask or silhouette. A lump of voxel "clay" is placed in the middle of the scene and from each image we simply look and see what is outside the object silhouette. Anything outside is carved away. Obviously, this requires us to know where the camera was relative to the object when the picture was taken, which is a whole separate problem.

This technique has been refined over the last decade and can be done in some computationally and memory efficient ways. My approach is neither of these - I went for simplicity over efficiency since my only aim was to explain the technique and show it in MATLAB.

Acknowledgements

The dinosaur images used here were provided by Wolfgang Niem at the University of Hannover.

The camera data used in this example was provided by Dr A. W. Fitzgibbon and Prof A. Zisserman from the University of Oxford Robotics Research Group.

The images and camera data can both be downloaded from the Visual Geometry Group web-pages at the University of Oxford Robotics Research Group. They are not included with the demo code.

Comments and Ratings (34)

Can you please explain "dino_Ps.mat" code?

Jongorn Pikun

Hi Ben,
I can't set camera same image in your code.
I can't solve problem
'Error using spacecarving.findmodel (line 40)
Nothing left after initial serach! Check your camera matrices.

Error in space_carving_demo (line 106)
[xlim,ylim,zlim] = findmodel( cameras );'

Can you please explain " Error Nothing left after initial serach! Check your camera matrices" for this code?
And Can you please explain "dino_Ps.mat" code?

seonggon lee

Hi all, I have tried with my own data but all failed....
spacecarving.findmodel (line 39)
Nothing left after initial serach! Check your camera matrices.
space_carving_demo (line 106)
[xlim,ylim,zlim] = findmodel( cameras );

I changed the code
from
S = im(:,:,1) > (im(:,:,3)-2);%origin
to
S = im(:,:,1) < 250;%im(10,10,1) ;%mine
to get silhouette in getsilhouette.m

therefore, I succeeded getting silhuoette but can't obtain the result..
does anybody know about solution..?? or anybody having same trouble with me??

Lim Desmond

Has anyone successfully used this code with their own data? There seems to be issues with generating the extrinsic parameters of the camera, as pointed out by Elizabeth in 2013 and a few others here...

I am suspecting the origin of world coordinate system here is defined differently as compared to those you get using Matlab's image processing toolbox, camera calibrator app.

Keith Jurek

Asri Khalid

great info and coding

dongdong

thank you for the source code

Thanks..
I'm trying with my own data, but it has problem on making voxels I guess, & I dont know what to do..

Error using spacecarving.findmodel (line 39)
Nothing left after initial search! Check your camera matrices.

Error in space_carving_demo (line 105)
[xlim,ylim,zlim] = findmodel( cameras );

Ben Tordoff

im(:,:,1) is the red channel of the RGB image, im(:,:,3) is the blue. S is therefore true (1) for pixels whose color contains more red than blue and false (0) otherwise.

Nearly every color contains some red and some blue and this is just a quick and dirty way to find those that are closer to red than blue.

Hi Ben,
Can you please explain what's the meaning for this code?
% Initial segmentation based on more red than blue
S = im(:,:,1) > (im(:,:,3)-2);
More red than blue? If other colors?

maryam

maryam (view profile)

Sam

Sam (view profile)

Hello Ben,
I want to do the same for other object. But for that I want to know Is your provided code is useful for other objects if I provide 36 images of that object and the camera matrix for the particular ?

Thanks

Sam

Sam

Sam (view profile)

wei zhang

very helpful!

Ian

Ian (view profile)

Hello Ben,

I've been trying to upload my own data to this program, but it always says the camera matrices are incorrect at the find model cell block. I thought this may be because I was using only 7 images (10 degrees apart) but when I run this program (with the dinosaur images), but edit the line;

cameras = loadcameradata( datadir);

to

cameras = loadcameradata( datadir, 1:7 );

As you described in the loadcameradata function, it doesnt work with 7 images, and the find model function fails. I was wondering if you could provide any help or suggestions?

Many thanks

Adam

Elizabeth

Hello everyone,

I wish to use this program, but upload my own images. Has anyone successful done this? Im struggling with generating the intrinsic and extrinsic parameters.

Thanks!

kevin

kevin (view profile)

Ben Tordoff

I'm afraid I cannot upload the data since it doesn't belong to me. As mentioned in the description you can download it from the Visual Geometry Group at the University of Oxford.

Hooi Thing

Hi I couldn't find the dino_Ps.mat file and receive the following error. Can you upload the MAT file? Thanks

Error using load
Unable to read file DinosaurData\dino_Ps.mat: No such file or directory.

Error in spacecarving.loadcameradata (line 32)
rawP = load( fullfile( dataDir, 'dino_Ps.mat') );

Hung Nguyen

Andrew

Andrew (view profile)

Great work and very interesting!!

One small thing I noticed just more for the aesthetics of the final result where you colour the surface of your 3D model, you need to have an inverse cosine (or acos in Matlab) to compute the angle between the camera view vector and vertex normals when doing the dot product. At the moment the variable 'angles' is actually not the angle but the cosine of the angle between the two vectors. Therefore putting acos in front of the dot product significantly improves the colouring of the surface.

Thanks for the great submission.

Andy

win kong

Hi Ben,
I'm having problem in getting the 3x4 matrix in dino_ps.mat.
How can I get the calibration parameters?

Thanks in advance

Win

salon

salon (view profile)

mariem123

Hi Ben,

I was wondering why there is the " Get real value" part in your algorithm. I do not really get what is here for.

Cheers,

Marie

Ben Tordoff

I believe you are correct, P{1,1} relates to image 1, however it's been a long time since I've worked on this. If you type

>> edit spacecarving.loadcameradata

you can see how the camera data is read and decomposed and which images are loaded where.

Cheers.
Ben

mariem123

I have an other question for today ^^
Is P{1,1}( cell (1,1) of the file dino_ps.mat ) the Projection matrix relative to the image viff.001.ppm for example ?

Is P{1,2}( cell (1,1) of the file dino_ps.mat ) the Projection matrix relative to the image viff.002.ppm as well, and so on ?

Cheers

Marie

mariem123

Hi Ben, thank for the reply.

How did you do to get the P matrix? Did you write it manually yourseld or is there any toolbox which did it for you ?

Ben Tordoff

Hi Marie, the matrices are "projection matrices", containing both the internal (K) and external (R, t) camera parameters. These are roughly P = K*[R t] where K is the 3x3 matrix of internal (aka intrinsic) parameters, R is a 3x3 rotation matrix giving the camera orientation and t a 3x1 translation vector. Hence the output, P, is 3x4.

However, these are estimated matrices and will contain noise. In order to draw the scenes I do an approximate decomposition back into K, R and t, but I make no claims that I'm doing it the most accurate way!

Cheers
Ben

mariem123

Hi Ben, great job !

I have a question for you about the dino_ps.mat you're using in the demo.
What are the coefficients in the matrix as it is a matrix 3*4 ? ( I guess there are the intrinsic and extrinsic parameters but which ones are there ?)

Cheers. Marie

Habibullah

Hi Ben, thanks for reply

Due to your suggestion.
I replaced the '~' with 'num_voxels' and it works fine.

Thanks for the great work and share

Cheers. Habib

Ben Tordoff

Hi Habibullah, are you by any chance using a version of MATLAB older than R2009a (which is when the '~' syntax was introduced)?

If so, you may still be able to get it to work by replacing the '~' with a real variable name, but bear in mind this hasn't been developed or tested on releases older than R2009b.

Cheers. Ben

Habibullah

This is very interesting!
However I cannot run it because this error:
??? Error: File: space_carving_demo.m Line: 197 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.

The line 197 is below:
[~,mykeep] = carve( myvoxels, cameras(ii) );

Any help thanks

Mike

Mike (view profile)

Updates

1.1.0.1

Updated license

1.1.0.0

Removed third party data from package

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor