VR Photo Creation with Normal Camera Thoughts

In fact, this is one relatively old draft from 20171119. Since I have sometime to review my legacy code during my journey back home to spend spring festival. I decide to spend sometime to rewrite this draft.

Thought 1

Since we would usually approximate ball as regular polyhedron and normal camera can just shot plane, therefore, one possible method would be taking photos corresponding to regular polyhedron surface.

Thought 2

Some time, I would wonder, why VR photo looks like it is.

As I thought, one pixel on ball surface should still be one pixel when the ball get flatten.

As we know, the perimeter of a circle is 2pir, if the r is set to 1, then the perimeter would be 2 * pi. To make stuff easier, cut the ball into 2 semi-balls.

With the help of programming, the ball surface can be flatten as following.

ball into 2 surface planes

And if we don't split the ball into 2 semi-balls, the surface would be.

2ball surface in 2d v2

Python Codes

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import math

from PIL import Image
from PIL import ImageFont, ImageDraw

import matplotlib.pyplot as plt


def draw_ball_surface_in_2d(num=2):
    SCALE_UNIT = 100

    img = Image.new('RGB', (math.ceil(2 * math.pi * SCALE_UNIT), math.ceil(math.pi * SCALE_UNIT)), color=(255, 255, 255, 255))
    draw = ImageDraw.Draw(img)

    if num == 3:
        for y in range(0, math.ceil(math.pi * SCALE_UNIT), 1):
            x1 = math.floor((math.pi / 3 - math.pi / 3 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x2 = math.floor((math.pi / 3 + math.pi / 3 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x3 = math.floor((3 * math.pi / 3 - math.pi / 3 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x4 = math.floor((3 * math.pi / 3 + math.pi / 3 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x5 = math.floor((3 * math.pi / 3 - math.pi / 3 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x6 = math.floor((3 * math.pi / 3 + math.pi / 3 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)


            draw.line([(x1, y), (x2, y)], fill=(0, 0, 0, 0), width=1)
            draw.line([(x3, y), (x4, y)], fill=(0, 0, 0, 0), width=1)
    elif num == 2:
        for y in range(0, math.ceil(math.pi * SCALE_UNIT), 1):
            x1 = math.floor((math.pi / 2 - math.pi / 2 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x2 = math.floor((math.pi / 2 + math.pi / 2 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            draw.line([(x1, y), (x2, y)], fill=(0, 0, 0, 0), width=1)

            x3 = math.floor((3 * math.pi / 2 - math.pi / 2 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x4 = math.floor((3 * math.pi / 2 + math.pi / 2 * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            draw.line([(x3, y), (x4, y)], fill=(0, 0, 0, 0), width=1)
    else:
        for y in range(0, math.ceil(math.pi * SCALE_UNIT), 1):
            x1 = math.floor((math.pi - math.pi * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)
            x2 = math.floor((math.pi + math.pi * math.sin(y / SCALE_UNIT)) * SCALE_UNIT + 0.5)

            draw.line([(x1, y), (x2, y)], fill=(0, 0, 0, 0), width=1)

    del draw

    ## preview in matplotlib
    plt.imshow(img)
    plt.show()

    ## save image
    vr_images_dir = os.path.join('images', 'vr')
    if not os.path.exists(vr_images_dir):
        os.makedirs(vr_images_dir)

    img.save(os.path.join(vr_images_dir, '0.png'))


if __name__ == '__main__':
    draw_ball_surface_in_2d()

My Hand Drafts for Surface Unfolding Calculation

ball surface in 2d handdraft

20171119-hand draft 2


* cached version, generated at 2018-12-31 03:13:41 UTC.

Subscribe by RSS