Python OpenGL.GL.GL_RGB Examples

The following are 8 code examples of OpenGL.GL.GL_RGB(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module OpenGL.GL , or try the search function .
Example #1
Source File: renderer.py    From Pix2Pose with MIT License 5 votes vote down vote up
def finish(self):

        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_RGB, gl.GL_FLOAT)
        rgb = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(3,))[::-1, :]  # Read buffer and flip Y
        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        dep = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(1,))[::-1, :]  # Read buffer and flip Y

        # Convert z-buffer to depth map
        mult = (self.clip_near*self.clip_far)/(self.clip_near-self.clip_far)
        addi = self.clip_far/(self.clip_near-self.clip_far)
        bg = dep == 1
        dep = mult/(dep + addi)
        dep[bg] = 0
        return rgb, np.squeeze(dep) 
Example #2
Source File: renderer_xyz.py    From Pix2Pose with MIT License 5 votes vote down vote up
def finish(self):

        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_RGB, gl.GL_FLOAT)
        rgb = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(3,))[::-1, :]  # Read buffer and flip Y
        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        dep = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(1,))[::-1, :]  # Read buffer and flip Y

        # Convert z-buffer to depth map
        mult = (self.clip_near*self.clip_far)/(self.clip_near-self.clip_far)
        addi = self.clip_far/(self.clip_near-self.clip_far)
        bg = dep == 1
        dep = mult/(dep + addi)
        dep[bg] = 0
        return rgb, np.squeeze(dep) 
Example #3
Source File: renderer.py    From ssd-6d with MIT License 5 votes vote down vote up
def finish(self):

        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_RGB, gl.GL_FLOAT)
        rgb = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(3,))[::-1, :]  # Read buffer and flip Y
        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        dep = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(1,))[::-1, :]  # Read buffer and flip Y

        # Convert z-buffer to depth map
        mult = (self.clip_near*self.clip_far)/(self.clip_near-self.clip_far)
        addi = self.clip_far/(self.clip_near-self.clip_far)
        bg = dep == 1
        dep = mult/(dep + addi)
        dep[bg] = 0
        return rgb, np.squeeze(dep) 
Example #4
Source File: renderer.py    From eccv18-rgb_pose_refinement with MIT License 5 votes vote down vote up
def finish(self):

        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_RGB, gl.GL_FLOAT)
        rgb = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(3,))[::-1, :]  # Read buffer and flip Y
        im = gl.glReadPixels(0, 0, self.size[0], self.size[1], gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        dep = np.copy(np.frombuffer(im, np.float32)).reshape(self.shape+(1,))[::-1, :]  # Read buffer and flip Y

        # Convert z-buffer to depth map
        mult = (self.clip_near*self.clip_far)/(self.clip_near-self.clip_far)
        addi = self.clip_far/(self.clip_near-self.clip_far)
        bg = dep == 1
        dep = mult/(dep + addi)
        dep[bg] = 0
        return rgb, np.squeeze(dep) 
Example #5
Source File: fullscreen_quad.py    From dm_control with Apache License 2.0 5 votes vote down vote up
def render(self, pixmap, viewport_shape):
    """Renders the pixmap on a fullscreen quad.

    Args:
      pixmap: A 3D numpy array of bytes (np.uint8), with dimensions
        (width, height, 3).
      viewport_shape: A tuple of two elements, (width, height).
    """
    GL.glClear(GL.GL_COLOR_BUFFER_BIT)
    GL.glViewport(0, 0, *viewport_shape)
    GL.glUseProgram(self._shader)
    GL.glActiveTexture(GL.GL_TEXTURE0)
    GL.glBindTexture(GL.GL_TEXTURE_2D, self._texture)
    GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, pixmap.shape[1],
                    pixmap.shape[0], 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE,
                    pixmap)
    GL.glUniform1i(self._var_texture_sampler, 0)
    GL.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4) 
Example #6
Source File: gui_utils.py    From spimagine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def fillTexture2d(data,tex = None, interp=True):
    """ data.shape == (Ny,Nx)
          file texture with GL_RED
        data.shape == (Ny,Nx,3)
          file texture with GL_RGB

        if tex == None, returns a new created texture
    """

    if tex is None:
        tex = GL.glGenTextures(1)

    GL.glBindTexture(GL.GL_TEXTURE_2D, tex)
    GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT,1)
    GL.glTexParameterf (GL.GL_TEXTURE_2D,
                     GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
    GL.glTexParameterf (GL.GL_TEXTURE_2D,
                     GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR if interp else GL.GL_NEAREST)

    GL.glTexParameterf (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE)
    GL.glTexParameterf (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE)
    # GL.glTexParameterf (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP)
    # GL.glTexParameterf (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP)

    if data.ndim == 2:
        Ny,Nx = data.shape
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, Nx, Ny,
                     0, GL.GL_RED, GL.GL_FLOAT, data.astype(np.float32))

    elif data.ndim == 3 and data.shape[2]==3:
        Ny,Nx = data.shape[:2]
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, Nx, Ny,
                         0, GL.GL_RGB, GL.GL_FLOAT, data.astype(np.float32))

    elif data.ndim == 3 and data.shape[2]==4:
        Ny,Nx = data.shape[:2]
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, Nx, Ny,
                         0, GL.GL_RGBA, GL.GL_FLOAT, data.astype(np.float32))

    else:
        raise Exception("data format not supported! \ndata.shape should be either (Ny,Nx) or (Ny,Nx,3)")
    return tex 
Example #7
Source File: simpleDisplayImage.py    From pyslam with GNU General Public License v3.0 4 votes vote down vote up
def main():
    # Create OpenGL window in single line
    pangolin.CreateWindowAndBind('Main', 640, 480)

    # 3D Mouse handler requires depth testing to be enabled
    gl.glEnable(gl.GL_DEPTH_TEST)


    scam = pangolin.OpenGlRenderState(
        pangolin.ProjectionMatrix(640, 480, 420, 420, 320, 240, 0.1, 1000),
        pangolin.ModelViewLookAt(-1, 1, -1, 0, 0, 0, pangolin.AxisDirection.AxisY))

    # Aspect ratio allows us to constrain width and height whilst fitting within specified
    # bounds. A positive aspect ratio makes a view 'shrink to fit' (introducing empty bars),
    # whilst a negative ratio makes the view 'grow to fit' (cropping the view).
    dcam = pangolin.CreateDisplay()
    dcam.SetBounds(0.0, 1.0, 0.0, 1.0, -640.0/480.0)
    dcam.SetHandler(pangolin.Handler3D(scam))

    # This view will take up no more than a third of the windows width or height, and it
    # will have a fixed aspect ratio to match the image that it will display. When fitting
    # within the specified bounds, push to the top-left (as specified by SetLock).
    dimg = pangolin.Display('image')
    dimg.SetBounds(2./3, 1.0, 0.0, 1./3, 640./480)
    dimg.SetLock(pangolin.Lock.LockLeft, pangolin.Lock.LockTop)


    w, h = 64, 48
    texture = pangolin.GlTexture(w, h, gl.GL_RGB, False, 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)

    # Default hooks for exiting (Esc) and fullscreen (tab).
    while not pangolin.ShouldQuit():
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glClearColor(0.95, 0.95, 0.95, 1.0)

        dcam.Activate(scam)
        gl.glColor3f(1.0, 1.0, 1.0)
        pangolin.glDrawColouredCube()

        # Set some random image data and upload to GPU 
        image = random_image(w, h)
        texture.Upload(image, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)

        # display the image
        dimg.Activate()
        gl.glColor3f(1.0, 1.0, 1.0)
        texture.RenderToViewport()

        pangolin.FinishFrame() 
Example #8
Source File: render_scene.py    From holistic_scene_parsing with MIT License 4 votes vote down vote up
def OnCaptureResult(render_path, img_path, width, height, true_height, if_vis, render_type='rgb'):
    if render_type == 'rgb':
        rgb_img = GL.glReadPixels(0, 0, width, height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, outputType=None)[::-1, :, :][
              height - true_height:, :, :]
        if if_vis:
            plt.imshow(rgb_img)
            plt.axis('off')
            plt.savefig(img_path, bbox_inches='tight')
            plt.close()
        # print render_path
        np.save(render_path, rgb_img)
        return rgb_img
    elif render_type == 'segmentation':
        segment = GL.glReadPixels(0, 0, width, height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, outputType=None)[::-1, :, :][
              height - true_height:, :, 0]
        if if_vis:
            plt.imshow(segment, vmin=0, vmax=38)
            # plt.colorbar()
            plt.axis('off')
            plt.savefig(img_path, bbox_inches='tight')
            plt.close()
        np.save(render_path, segment)
        return segment
    elif render_type == 'normal':
        normal = GL.glReadPixels(0, 0, width, height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, outputType=None)[::-1, :, :][
                 height - true_height:, :, :]
        if if_vis:
            plt.imshow(normal)
            plt.axis('off')
            plt.savefig(img_path, bbox_inches='tight')
            plt.close()
        np.save(render_path, normal)
        return normal
    elif render_type == 'depth':
        data = GL.glReadPixels(0, 0, width, height, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT,
                               outputType=None)  # read projected pixel info
        capturedImage = data
        for i in range(width):
            for j in range(height):
                if capturedImage[i][j] == 1.0:
                    capturedImage[i][j] = 20
                else:
                    far = FAR
                    near = 0.1
                    clip_z = (capturedImage[i][j] - 0.5) * 2.0
                    world_z = 2 * far * near / (clip_z * (far - near) - (far + near))
                    capturedImage[i][j] = -world_z  # -z#
        depth = capturedImage[::-1, :][height - true_height:, :]
        if if_vis:
            fig = plt.figure()
            ii = plt.imshow(depth, interpolation='nearest')
            # fig.colorbar(ii)
            plt.axis('off')
            plt.savefig(img_path, bbox_inches='tight')
            plt.close()
        np.save(render_path, depth)
        scipy.io.savemat(render_path + '.mat', mdict={'depth': depth})
        return depth