Python OpenGL.GL.glReadPixels() Examples

The following are 8 code examples of OpenGL.GL.glReadPixels(). 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 patch_linemod with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def read_fbo_color_rgba32f(fbo):
        """
        Read the color attachment from a FBO, assuming it is GL_RGBA_32F.
        # Ref: https://github.com/julienr/vertex_visibility/blob/master/depth.py
        """
        h, w = fbo.color_buffer.shape[:2]
        x, y = 0, 0
        im = gl.glReadPixels(x, y, w, h, gl.GL_RGBA, gl.GL_FLOAT)
        im = np.frombuffer(im, np.float32)
        im.shape = h, w, 4
        im = im[::-1, :]

        return im

#-------------------------------------------------------------------------------
# Ref: https://github.com/vispy/vispy/blob/master/examples/demo/gloo/offscreen.py 
Example #2
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 #3
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 #4
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 #5
Source File: camera.py    From GDMC with ISC License 5 votes vote down vote up
def _blockUnderCursor(self, center=False):
        """
            returns a point in 3d space that was determined by
         reading the depth buffer value
        """
        try:
            GL.glReadBuffer(GL.GL_BACK)
        except Exception:
            logging.exception('Exception during glReadBuffer')
        ws = self.root.size
        if center:
            x, y = ws
            x //= 2
            y //= 2
        else:
            x, y = mouse.get_pos()
        if (x < 0 or y < 0 or x >= ws[0] or
                    y >= ws[1]):
            return 0, 0, 0

        y = ws[1] - y

        try:
            pixel = GL.glReadPixels(x, y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT)
            newpoint = unproject(x, y, pixel[0])
        except Exception:
            return 0, 0, 0

        return newpoint 
Example #6
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 #7
Source File: camera.py    From MCEdit-Unified with ISC License 5 votes vote down vote up
def _blockUnderCursor(self, center=False):
        """
            returns a point in 3d space that was determined by
         reading the depth buffer value
        """
        try:
            GL.glReadBuffer(GL.GL_BACK)
        except Exception:
            logging.exception('Exception during glReadBuffer')
        ws = self.root.size
        if center:
            x, y = ws
            x //= 2
            y //= 2
        else:
            x, y = mouse.get_pos()
        if (x < 0 or y < 0 or x >= ws[0] or
                    y >= ws[1]):
            return 0, 0, 0

        y = ws[1] - y

        try:
            pixel = GL.glReadPixels(x, y, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT)
            newpoint = unproject(x, y, pixel[0])
        except Exception:
            return 0, 0, 0

        return newpoint 
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