BerkeleyAutomation/perception

Resizing binary images results in all zero image

mjd3 opened this issue · 3 comments

mjd3 commented

During the conversion to scikit-image from scipy.misc (#18), it seems like the resize function for binary images has broken.

Here's an example:

  1. A depth image, with some zero values (type: float32)
    image
  2. Using to_binary() to make a binary image (type: uint8)
    image
  3. Scaling up by 4x (using .resize(4.0)) with perception<=0.0.7 (type: uint8)
    image
  4. Scaling up by 4x (using .resize(4.0)) with perception==0.0.8 (type: uint8)
    image

With the newest version, scaling binary images (up or down) gives an image of all zeros. From doing some initial inspection, it seems like the scikit-image resize function converts to float values somewhere, so when the returned value from that function comes in, the cast creates an array of all zeros.

@mjd3 Have you tried casting to float before resize? Seems like a straightforward fix. Thanks!

mjd3 commented

Yup either casting to float beforehand or using the preserve_range keyword would work. I'll submit a PR to fix this so that upgrading doesn't break stuff.

I also encountered this problem while trying to resize a ColorImage (See the code example below). I created a fix for this based on the solution of @mjd3 (see #31).

Example

import perception
import cv2
import matplotlib.pyplot as plt
from visualization import Visualizer2D as vis

## Create camera object ##
kinect_camera = perception.Kinect2Sensor()
kinect_camera.start()

## Get frames ##
color_im, depth_im, _ = kinect_camera.frames()

## Visualize resized frame
vis.figure()
vis.imshow(color_im)
vis.show()

## Resize color frame ##
color_im_small = color_im.resize(0.5)

## Visualize resized frame
vis.figure()
vis.imshow(color_im_small)
vis.show()

## Use the impaint function
color_im_inpaint = color_im.inpaint(
    rescale_factor=0.5)

## Visualize resized frame
vis.figure()
vis.imshow(color_im_inpaint)
vis.show()

# Close camera connection
kinect_camera.stop()