Tuesday, 31 December 2013

3D scanning with photographs

3D scanning really small things with photographs

While working on one of my many diversions (projects) I needed a 3D model of a very small component - Sharp's GP2S60 photointerruptor.

A quick look at the above PDF link will tell you that this is a very small device - 3.2 x 1.7 x 1.1 mm!

I followed Happy Robot's guide to create a 3D model for Analog Devices ADMP441 mems microphone (now sold to Invisense) for KiCad but the prospect of modeling Sharp's device was daunting.

Then I happened across www.my3dscanner.com and my spirits improved.

Just take some photos, upload them and voilĂ !

Just take some photos

Sounds easy, and after about 10 hours setting things up, well, it was pretty easy.


A PC, a Raspberry Pi, a Raspberry Pi camera, and (optional) a wireless router in bridge mode

I'd previously modified the Raspberry Pi camera to take close-up pictures so I thought I'd try that.

Viewing the video output can be troublesome over WiFi as it has a more limited bandwidth, but it depends on your wifi cards/dongles and your wifi router.

One solution I tried was to use a TP-Links WR740N router in bridge mode and connect the Raspberry Pi and my PC to it, allowing wired ethernet speeds between them, with WiFi speed internet access for both.

Of course I had to reset the router to factory defaults first because I hadn't used it since UPC provided their WiFi modem, and I eventually discovered I needed to update the firmware for bridging to work, and I needed to use the Raspberry Pi to do all this because I couldn't get my laptop to talk Wifi and wired networking at the same time as both routers were set up for

Every time I have to mess around with networking I wish I hadn't, but I was determined, and it paid off.

The wired speeds were 3 times that of WiFi and the video quality was flawless.

Update: 2014-03-28 with the new video scripts wifi video streaming is perfectly acceptable. Maybe it was the router.

The rig

I need a workshop, I know

That's a garden floodlight, all glorious 500W were needed to mostly eliminate image noise as things are zoomed up. Plus I couldn't think of anywhere else to set things up where the floodlight could hang.

I took the Quick Set-Up for Accurate Industrial Quality Scanning as a starting point - my rig needed to be a lot smaller.

I didn't have a picture of an Apple, like the "Quality Scanning" guide had, and I knew that my turntable needed a small, matt, non-repeating image as a back-drop for the item to be scanned.

So I looked around and she caught my eye.

On the front page of the Evening Herald was a picture of Laura Whitmore which looked nice. I figured I'd be looking at the background to the components I was scanning a lot so I chose something I wouldn't tire of soon, or ever (blush).

I cut it into a circle and marked it at approximately every 15 degrees as per the guide.

I used some Pritt Stick to stick it to some card board, and sticky tape to stick a thumb tack to the back, pointy bit outward.

I had taken down a foam+paper 3D puzzle from the attic as my resolution to always-take-down-more-than-you-put-up required, so it was handy for sticking the thumb tack pointy-bit into and I had a turntable.

Laura Whitmore framing the part (I hope that's OK with you, Laura!)
I had set up an NFS share on my PC and mounted it with the 'Pi, so it could then save files right to my PCs hard disk.

To get the video preview I put the following into raspivid-server:

while true; do
  raspivid -w 1280 -h 1024 -t 999999999 -fps 30 -b 5000000 -o - | nc -l -p 5001
...and ran it on the 'pi.

You can make the script executable with
chmod +x raspivid-server
so you can invoke it with
or forget making it executable and run
sh raspivid-server
The following goes into raspivid-client
if test "$1" = ""; then
nc $h 5001 | mplayer -fps 40 -demuxer h264es -
and ran on the PC.

You can make the script executable with
chmod +x raspivid-client
so you can invoke it with
or forget making it executable and run
sh raspivid-client
The client needs to know the IP address of the server (the 'pi) - I configured the router to reserve one IP address for the wifi dongle on the 'pi, and put that as the default for the script.

For trying wired access or another 'pi, you can supply the 'pi's IP address on the command line.

Thanks to this post for how to do low latency streaming.

It seems to require moving your hand in front of the camera to flush some frames through and reduce the latency, which starts out awful: > 3 seconds delay.
After that the delay for me was less than 0.5 seconds!

After getting the 'Pi camera able to focus on really nearby things (the lens nearly fell out I had it unscrewed so much!) using the above scripts, I took a series of pictures with the "raspistill" program in time-lapse mode, of which this is the first:

I poked the turn-table with a piece of wire so that each shot was approximately a 15 degree rotation more/less than the one before, and was so chuffed I started disassembling the rig and putting things away.

It was then I remembered the guides warning about no shadows - d'oh!
I forgot to photograph the back - d'oh number two!

Things were a lot quicker the second time around but I had to use a milk carton to diffuse the light:
The results:

I converted the image sequences to video for your delectation:

 I got those pesky ffmpeg flags from here.
ffmpeg -r 10 -g 300 -i image-fr-%04d.jpg -vcodec mjpeg -qscale:v 1 image-fr.avi
ffmpeg -r 10 -g 300 -i image-bk-%04d.jpg -vcodec mjpeg -qscale:v 1 image-bk.avi
just remember to renumber the frames so they start at 0001 and increase by 1.


I uploaded them to my3dscanner.com and a few days later I got a notification that the results were available for download - the delay was probably due to holidays.

Here are some pictures I took of them with Meshlab:

Now to figure out how to process them!