예제 #1
0
def image_height(filename, N=[30, 30], p=[4, 4]):
    """Generate a B-spline surface approximation given by the heightmap in a
    grayscale image.

    :param str filename: Name of image file to read
    :param (int,int) N: Number of controlpoints in u-direction
    :param (int,int) p: Polynomial order (degree+1)
    :return: Normalized (all values between 0 and 1) heightmap approximation
    :rtype: :class:`splipy.Surface`
    """

    import cv2

    im = cv2.imread(filename)

    width = len(im)
    height = len(im[0])

    # initialize image holder
    imGrey = np.zeros((len(im), len(im[0])), np.uint8)

    # convert to greyscale image
    if cv2.__version__[0] == '2':
        warnings.warn(
            FutureWarning(
                'openCV v.2 will eventually be discontinued. Please update your version: \"pip install opencv-python --upgrade\"'
            ))
        cv2.cvtColor(im, cv2.cv.CV_RGB2GRAY, imGrey)
    else:
        cv2.cvtColor(im, cv2.COLOR_RGB2GRAY, imGrey)

    pts = []
    # guess uniform evaluation points and knot vectors
    u = range(width)
    v = range(height)
    knot1 = [0] * 3 + range(N[0] - p[0] + 2) + [N[0] - p[0] + 1] * 3
    knot2 = [0] * 3 + range(N[0] - p[0] + 2) + [N[0] - p[0] + 1] * 3

    # normalize all values to be in range [0, 1]
    u = [float(i) / u[-1] for i in u]
    v = [float(i) / v[-1] for i in v]
    knot1 = [float(i) / knot1[-1] for i in knot1]
    knot2 = [float(i) / knot2[-1] for i in knot2]

    for j in range(height):
        for i in range(width):
            pts.append(
                [v[j], u[i],
                 float(imGrey[width - i - 1][j]) / 255.0 * 1.0])

    basis1 = BSplineBasis(4, knot1)
    basis2 = BSplineBasis(4, knot2)

    return surface_factory.least_square_fit(pts, [basis1, basis2], [u, v])
예제 #2
0
def image_height(filename, N=[30,30], p=[4,4]):
    """Generate a B-spline surface approximation given by the heightmap in a
    grayscale image.

    :param str filename: Name of image file to read
    :param (int,int) N: Number of controlpoints in u-direction
    :param (int,int) p: Polynomial order (degree+1)
    :return: Normalized (all values between 0 and 1) heightmap approximation
    :rtype: :class:`splipy.Surface`
    """

    import cv2

    im = cv2.imread(filename)

    width  = len(im)
    height = len(im[0])

    # initialize image holder
    imGrey = np.zeros((len(im),   len(im[0])),   np.uint8)

    # convert to greyscale image
    if cv2.__version__[0] == '2':
        warnings.warn(FutureWarning('openCV v.2 will eventually be discontinued. Please update your version: \"pip install opencv-python --upgrade\"'))
        cv2.cvtColor(im, cv2.cv.CV_RGB2GRAY, imGrey)
    else:
        cv2.cvtColor(im, cv2.COLOR_RGB2GRAY, imGrey)

    pts = []
    # guess uniform evaluation points and knot vectors
    u = range(width)
    v = range(height)
    knot1 = [0]*3 + range(N[0]-p[0]+2) + [N[0]-p[0]+1]*3
    knot2 = [0]*3 + range(N[0]-p[0]+2) + [N[0]-p[0]+1]*3

    # normalize all values to be in range [0, 1]
    u     = [float(i)/u[-1]     for i in u]
    v     = [float(i)/v[-1]     for i in v]
    knot1 = [float(i)/knot1[-1] for i in knot1]
    knot2 = [float(i)/knot2[-1] for i in knot2]

    for j in range(height):
        for i in range(width):
            pts.append([v[j], u[i], float(imGrey[width-i-1][j])/255.0*1.0])

    basis1 = BSplineBasis(4, knot1)
    basis2 = BSplineBasis(4, knot2)

    return surface_factory.least_square_fit(pts,[basis1, basis2], [u,v])
예제 #3
0
파일: image.py 프로젝트: salbali/Splipy
def image_height(filename, N=[30, 30], p=[4, 4]):
    """Generate a B-spline surface approximation given by the heightmap in a
    grayscale image.

    :param str filename: Name of image file to read
    :param (int,int) N: Number of controlpoints in u-direction
    :param (int,int) p: Polynomial order (degree+1)
    :return: Normalized (all values between 0 and 1) heightmap approximation
    :rtype: :class:`splipy.Surface`
    """

    import cv2

    im = cv2.imread(filename)

    width = len(im[0])
    height = len(im)

    # initialize image holder
    imGrey = np.zeros((height, width), np.uint8)

    # convert to greyscale image
    cv2.cvtColor(im, cv2.COLOR_RGB2GRAY, imGrey)

    # guess uniform evaluation points and knot vectors
    u = list(range(width))
    v = list(range(height))
    knot1 = [0] * (p[0] - 1) + list(
        range(N[0] - p[0] + 2)) + [N[0] - p[0] + 1] * (p[0] - 1)
    knot2 = [0] * (p[1] - 1) + list(
        range(N[1] - p[1] + 2)) + [N[1] - p[1] + 1] * (p[1] - 1)

    # normalize all values to be in range [0, 1]
    u = [float(i) / u[-1] for i in u]
    v = [float(i) / v[-1] for i in v]
    knot1 = [float(i) / knot1[-1] for i in knot1]
    knot2 = [float(i) / knot2[-1] for i in knot2]

    # flip and reverse image so coordinate (0,0) is at lower-left corner
    imGrey = imGrey.T / 255.0
    imGrey = np.flip(imGrey, axis=1)
    x, y = np.meshgrid(u, v, indexing='ij')
    pts = np.stack([x, y, imGrey], axis=2)

    basis1 = BSplineBasis(p[0], knot1)
    basis2 = BSplineBasis(p[1], knot2)

    return surface_factory.least_square_fit(pts, [basis1, basis2], [u, v])
예제 #4
0
    def test_l2(self):
        t = np.linspace(0, 1, 110)
        V,U = np.meshgrid(t,t)
        x = np.zeros((110,110,3))
        x[:,:,0] = U*U + V*V
        x[:,:,1] = U - V
        x[:,:,2] = U*U*V*V*V - U*V + 3
        b1 = BSplineBasis(3, [0,0,0,.33,.66,.7, .8,1,1,1])
        b2 = BSplineBasis(4, [0,0,0,0,.1, .2, .5,1,1,1,1])

        surf = SurfaceFactory.least_square_fit(x, [b1,b2], [t,t])
        t = np.linspace(0, 1, 13)
        V,U = np.meshgrid(t,t)
        x = surf(t,t)

        self.assertTrue(np.allclose(x[:,:,0], U*U + V*V))
        self.assertTrue(np.allclose(x[:,:,1], U   - V))
        self.assertTrue(np.allclose(x[:,:,2], U*U*V*V*V - U*V + 3))
예제 #5
0
    def test_l2(self):
        t = np.linspace(0, 1, 110)
        V, U = np.meshgrid(t, t)
        x = np.zeros((110, 110, 3))
        x[:, :, 0] = U * U + V * V
        x[:, :, 1] = U - V
        x[:, :, 2] = U * U * V * V * V - U * V + 3
        b1 = BSplineBasis(3, [0, 0, 0, .33, .66, .7, .8, 1, 1, 1])
        b2 = BSplineBasis(4, [0, 0, 0, 0, .1, .2, .5, 1, 1, 1, 1])

        surf = sf.least_square_fit(x, [b1, b2], [t, t])
        t = np.linspace(0, 1, 13)
        V, U = np.meshgrid(t, t)
        x = surf(t, t)

        self.assertTrue(np.allclose(x[:, :, 0], U * U + V * V))
        self.assertTrue(np.allclose(x[:, :, 1], U - V))
        self.assertTrue(np.allclose(x[:, :, 2], U * U * V * V * V - U * V + 3))