File Exchange

image thumbnail

Carving a Dinosaur

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


Updated 01 Sep 2016

View Version History

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.


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.

Cite As

Ben Tordoff (2021). Carving a Dinosaur (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (34)

Tetsuto Yamada

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
S = im(:,:,1) > (im(:,:,3)-2);%origin
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.

Jingwei Zhuang

Keith Jurek

Asri Khalid

great info and coding


thank you for the source code

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

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.

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?



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 ?




wei zhang

very helpful!


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



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.



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


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.


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




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.



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.



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 ?




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!



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


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


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


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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!