View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Carving a Dinosaur

4.9 | 11 ratings Rate this file 27 Downloads (last 30 days) File Size: 20.1 KB File ID: #26160 Version:
image thumbnail

Carving a Dinosaur


Ben Tordoff (view profile)


21 Dec 2009 (Updated )

This is a demo of reconstructing a 3D shape from multiple images using space-carving.

| Watch this File

File Information

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.


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.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (31)
08 Dec 2016 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.

Comment only
11 May 2016 Jingwei Zhuang

16 Dec 2015 Keith Jurek

06 Mar 2014 Asri Khalid

great info and coding

16 Dec 2013 dongdong

thank you for the source code

Comment only
25 Sep 2013 Siti Syazalina

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 );

Comment only
24 Sep 2013 Ben Tordoff

Ben Tordoff (view profile)

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.

Comment only
24 Sep 2013 Siti Syazalina

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?

Comment only
07 Aug 2013 maryam

maryam (view profile)

26 Jul 2013 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 ?



Comment only
24 Jul 2013 Sam

Sam (view profile)

16 May 2013 wei zhang

very helpful!

21 Apr 2013 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);


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


Comment only
04 Apr 2013 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.


Comment only
15 Mar 2013 kevin

kevin (view profile)

14 Jan 2013 Ben Tordoff

Ben Tordoff (view profile)

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.

Comment only
14 Jan 2013 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') );

Comment only
28 Nov 2012 Hung Nguyen

11 Nov 2012 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.


31 Oct 2012 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


Comment only
20 Sep 2012 salon

salon (view profile)

27 Jul 2012 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.



Comment only
05 Jul 2012 Ben Tordoff

Ben Tordoff (view profile)

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.


Comment only
04 Jul 2012 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 ?



Comment only
03 Jul 2012 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 ?

Comment only
02 Jul 2012 Ben Tordoff

Ben Tordoff (view profile)

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!


Comment only
02 Jul 2012 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

Comment only
22 Jun 2012 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

Comment only
05 Mar 2012 Ben Tordoff

Ben Tordoff (view profile)

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

Comment only
04 Mar 2012 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

Comment only
22 Jan 2010 Mike

Mike (view profile)

22 Jan 2010 1.1

Removed third party data from package

01 Sep 2016

Updated license

Contact us