Пример #1
0
    def test_numpy_share_memory(self):
        # TODO: do pytest parametarize once we move to pytest

        np_img = np.arange(4 * 5 * 3, dtype=np.uint8).reshape(4, 5, 3)
        vital_img = Image(np_img)

        assert np.all(np_img == vital_img.asarray()), (
            'must be initially the same')

        np_img += 1
        assert np.all(np_img != vital_img.asarray()), (
            'we do not share memory yet')
Пример #2
0
    def test_numpy_share_memory(self):
        # TODO: do pytest parametarize once we move to pytest

        np_img = np.arange(4 * 5 * 3, dtype=np.uint8).reshape(4, 5, 3)
        vital_img = Image(np_img)

        assert np.all(np_img == vital_img.asarray()), (
            'must be initially the same')

        np_img += 1
        assert np.all(np_img != vital_img.asarray()), (
            'we do not share memory yet')
Пример #3
0
def get_pil_image(img):
    """ Get image in python friendly format
    Assumptions are that the image has byte pixels.
    :return: array containing image
    :rtype: pil image
    """
    def pil_mode_from_image(img):
        """
        Determine image format from pixel properties
        May return None if our current encoding does not map to a PIL image
        mode.
        """
        if img.pixel_type() == img.PIXEL_UNSIGNED and img.pixel_num_bytes() == 1:
            if img.depth() == 3 and img.d_step() == 1 and img.w_step() == 3:
                return "RGB"
            elif img.depth() == 4 and img.d_step() == 1 and img.w_step() == 4:
                return "RGBA"
            elif img.depth() == 1 and img.w_step() == 1:
                return "L"
        elif img.depth() == 1 and img.w_step() == 1:
            if img.pixel_type() == img.PIXEL_BOOL and img.pixel_num_bytes() == 1:
                return "1"
            elif img.pixel_type() == img.PIXEL_SIGNED and img.pixel_num_bytes() == 4:
                return "I"
            elif img.pixel_type() == img.PIXEL_FLOAT and img.pixel_num_bytes() == 4:
                return "F"
        return None

    mode = pil_mode_from_image(img)

    if not mode:
        # make a copy of this image using contiguous memory with interleaved channels
        new_img = Image(img.width(), img.height(), img.depth(),
                        True, img.pixel_type(), img.pixel_num_bytes())
        new_img.copy_from(img)
        img = new_img
        mode = pil_mode_from_image(img)

    if not mode:
        raise RuntimeError("Unsupported image format.")

    # get buffer from image
    if six.PY2:
        img_pixels = buffer(bytearray(img))
    else:
        img_pixels = memoryview(bytearray(img)).tobytes()

    pil_img = _pil_image_from_bytes(mode, (img.width(), img.height()),
                                    img_pixels, "raw", mode,
                                    img.h_step() * img.pixel_num_bytes(), 1)
    return pil_img
Пример #4
0
def get_pil_image(img):
    """ Get image in python friendly format
    Assumptions are that the image has byte pixels.
    :return: array containing image
    :rtype: pil image
    """
    def pil_mode_from_image(img):
        """
        Determine image format from pixel properties
        May return None if our current encoding does not map to a PIL image
        mode.
        """
        if img.pixel_type() == img.PIXEL_UNSIGNED and img.pixel_num_bytes(
        ) == 1:
            if img.depth() == 3 and img.d_step() == 1 and img.w_step() == 3:
                return "RGB"
            elif img.depth() == 4 and img.d_step() == 1 and img.w_step() == 4:
                return "RGBA"
            elif img.depth() == 1 and img.w_step() == 1:
                return "L"
        elif img.depth() == 1 and img.w_step() == 1:
            if img.pixel_type() == img.PIXEL_BOOL and img.pixel_num_bytes(
            ) == 1:
                return "1"
            elif img.pixel_type() == img.PIXEL_SIGNED and img.pixel_num_bytes(
            ) == 4:
                return "I"
            elif img.pixel_type() == img.PIXEL_FLOAT and img.pixel_num_bytes(
            ) == 4:
                return "F"
        return None

    mode = pil_mode_from_image(img)

    if not mode:
        # make a copy of this image using contiguous memory with interleaved channels
        new_img = Image(img.width(), img.height(), img.depth(), True,
                        img.pixel_type(), img.pixel_num_bytes())
        new_img.copy_from(img)
        img = new_img
        mode = pil_mode_from_image(img)

    if not mode:
        raise RuntimeError("Unsupported image format.")

    # get buffer from image
    img_pixels = buffer(bytearray(img))

    return _pil_image_from_bytes(mode, (img.width(), img.height()), img_pixels,
                                 "raw", mode,
                                 img.h_step() * img.pixel_num_bytes(), 1)
Пример #5
0
        def _test_numpy(dtype_name, nchannels, order='c'):
            if nchannels is None:
                shape = (5, 4)
            else:
                shape = (5, 4, nchannels)
            size = np.prod(shape)

            dtype = np.dtype(dtype_name)

            if dtype_name == 'bool':
                np_img = np.zeros(size, dtype=dtype).reshape(shape)
                np_img[0::2] = 1
            else:
                np_img = np.arange(size, dtype=dtype).reshape(shape)

            if order.startswith('c'):
                np_img = np.ascontiguousarray(np_img)
            elif order.startswith('fortran'):
                np_img = np.asfortranarray(np_img)
            else:
                raise KeyError(order)
            if order.endswith('-reverse'):
                np_img = np_img[::-1, ::-1]

            vital_img = Image(np_img)
            recast = vital_img.asarray()

            if nchannels is None:
                # asarray always returns 3 channels
                np_img = np_img[..., None]

            pixel_type_name = vital_img.pixel_type_name()

            if dtype_name == 'float16':
                want = 'float16'
            if dtype_name == 'float32':
                want = 'float'
            elif dtype_name == 'float64':
                want = 'double'
            else:
                want = dtype_name

            assert pixel_type_name == want, 'want={} but got={}'.format(
                want, pixel_type_name)

            if not np.all(np_img == recast):
                raise AssertionError(
                    'Failed dtype={}, nchannels={}, order={}'.format(
                        dtype_name, nchannels, order))
Пример #6
0
        def _test_numpy(dtype_name, nchannels, order='c'):
            if nchannels is None:
                shape = (5, 4)
            else:
                shape = (5, 4, nchannels)
            size = np.prod(shape)

            dtype = np.dtype(dtype_name)

            if dtype_name == 'bool':
                np_img = np.zeros(size, dtype=dtype).reshape(shape)
                np_img[0::2] = 1
            else:
                np_img = np.arange(size, dtype=dtype).reshape(shape)

            if order.startswith('c'):
                np_img = np.ascontiguousarray(np_img)
            elif order.startswith('fortran'):
                np_img = np.asfortranarray(np_img)
            else:
                raise KeyError(order)
            if order.endswith('-reverse'):
                np_img = np_img[::-1, ::-1]

            vital_img = Image(np_img)
            recast = vital_img.asarray()

            if nchannels is None:
                # asarray always returns 3 channels
                np_img = np_img[..., None]

            pixel_type_name = vital_img.pixel_type_name()

            if dtype_name == 'float16':
                want = 'float16'
            if dtype_name == 'float32':
                want = 'float'
            elif dtype_name == 'float64':
                want = 'double'
            else:
                want = dtype_name

            assert pixel_type_name == want, 'want={} but got={}'.format(
                want, pixel_type_name)

            if not np.all(np_img == recast):
                raise AssertionError(
                    'Failed dtype={}, nchannels={}, order={}'.format(
                        dtype_name, nchannels, order))
Пример #7
0
    def _step(self):
        print "[DEBUG] ----- start step"
        # grab image container from port using traits
        in_img_c = self.grab_input_using_trait('image')

        # Get image from conatiner
        in_img = in_img_c.get_image()

        # convert generic image to PIL image
        pil_image = in_img.get_pil_image()

        # draw on the image to prove we can do it
        num = 37
        import ImageDraw
        draw = ImageDraw.Draw(pil_image)
        draw.line((0, 0) + pil_image.size, fill=128, width=5)
        draw.line((0, pil_image.size[1], pil_image.size[0], 0), fill=32768, width=5)
        #                 x0   y0   x1       y1
        draw.rectangle( [num, num, num+100, num+100], outline=125 )
        del draw

        new_image = Image.from_pil( pil_image )  # get new image handle
        new_ic = ImageContainer( new_image )

        # push object to output port
        self.push_to_port_using_trait( 'out_image', new_ic )

        self._base_step()
Пример #8
0
    def _step(self):
        print "[DEBUG] ----- start step"
        # grab image container from port using traits
        in_img_c = self.grab_input_using_trait('image')

        # Get image from conatiner
        in_img = in_img_c.get_image()

        # convert generic image to PIL image
        pil_image = in_img.get_pil_image()

        # draw on the image to prove we can do it
        num = 37
        import ImageDraw
        draw = ImageDraw.Draw(pil_image)
        draw.line((0, 0) + pil_image.size, fill=128, width=5)
        draw.line((0, pil_image.size[1], pil_image.size[0], 0),
                  fill=32768,
                  width=5)
        #                 x0   y0   x1       y1
        draw.rectangle([num, num, num + 100, num + 100], outline=125)
        del draw

        new_image = Image.from_pil(pil_image)  # get new image handle
        new_ic = ImageContainer(new_image)

        # push object to output port
        self.push_to_port_using_trait('out_image', new_ic)

        self._base_step()
Пример #9
0
        def _test_numpy(dtype_name, nchannels, order='c'):
            np_img = create_numpy_image(dtype_name, nchannels, order)
            vital_img = Image(np_img)
            recast = vital_img.asarray()
            # asarray always returns 3 channels
            np_img = np.atleast_3d(np_img)
            pixel_type_name = vital_img.pixel_type_name()
            want = map_dtype_name_to_pixel_type(dtype_name)

            assert pixel_type_name == want, 'want={} but got={}'.format(
                want, pixel_type_name)

            if not np.all(np_img == recast):
                raise AssertionError(
                    'Failed dtype={}, nchannels={}, order={}'.format(
                        dtype_name, nchannels, order))
Пример #10
0
 def test_detect(self):
     modules.load_known_modules()
     detector = ImageObjectDetector.create("example_detector")
     image = Image()
     image_container = ImageContainer(image)
     detections = detector.detect(image_container)
     nose.tools.ok_(detections is not None, "Unexpected empty detections")
     nose.tools.assert_equal(len(detections), 1)
Пример #11
0
    def _step(self):
        print("[DEBUG] ----- start step")
        # grab image container from port using traits
        in_img_c = self.grab_input_using_trait("image")

        imageHeight = in_img_c.height()
        imageWidth = in_img_c.width()

        if (self.normImageType):
            print("Normalize image")

            in_img = in_img_c.image().asarray().astype("uint16")

            bottom, top = self.get_scaling_values(self.normImageType,
                                                  imageHeight)
            in_img = self.lin_normalize_image(in_img, bottom, top)

            in_img = np.tile(in_img, (1, 1, 3))
        else:
            in_img = np.array(get_pil_image(in_img_c.image()).convert("RGB"))

        self.push_to_port_using_trait("image_norm",
                                      ImageContainer(Image(in_img)))

        startTime = time.time()
        boxes, scores, classes = self.generate_detection(
            self.detection_graph, in_img)
        elapsed = time.time() - startTime
        print("Done running detector in {}".format(
            humanfriendly.format_timespan(elapsed)))

        goodBoxes = []
        detections = DetectedObjectSet()

        for i in range(0, len(scores)):
            if (scores[i] >= self.confidenceThresh):
                bbox = boxes[i]
                goodBoxes.append(bbox)

                topRel = bbox[0]
                leftRel = bbox[1]
                bottomRel = bbox[2]
                rightRel = bbox[3]

                xmin = leftRel * imageWidth
                ymin = topRel * imageHeight
                xmax = rightRel * imageWidth
                ymax = bottomRel * imageHeight

                obj = DetectedObject(BoundingBox(xmin, ymin, xmax, ymax),
                                     scores[i])
                detections.add(obj)

        print("Detected {}".format(len(goodBoxes)))

        self.push_to_port_using_trait("detected_object_set", detections)

        self._base_step()
Пример #12
0
    def add_data_from_disk(self, in_img):
        img = in_img.image().asarray().astype("uint16")

        mi = np.percentile(img, 1)
        ma = np.percentile(img, 100)

        normalized = (img - mi) / (ma - mi)
        normalized = normalized * 255
        normalized[normalized < 0] = 0

        output = ImageContainer(Image(normalized.astype("uint8")))
        return
Пример #13
0
    def get_image(self):
        """
        Return a new pointer the to contained image. This instance shares the
        same internal memory as the contained image.

        :return: New ImageContainer instance
        :rtype: ImageContainer

        """
        ic_getimg = self.VITAL_LIB['vital_image_container_get_image']
        ic_getimg.argtypes = [self.C_TYPE_PTR]
        ic_getimg.restype = Image.C_TYPE_PTR
        return Image(from_cptr=ic_getimg(self))
Пример #14
0
    def _step(self):
        # grab image container from port using traits
        img_c = self.grab_input_using_trait('image')

        img = img_c.image().asarray().astype("uint16")

        mi = np.percentile(img, 1)
        ma = np.percentile(img, 100)

        normalized = (img - mi) / (ma - mi)

        normalized = normalized * 255
        normalized[normalized < 0] = 0

        output = ImageContainer(Image(normalized.astype("uint8")))

        # push dummy image object (same as input) to output port
        self.push_to_port_using_trait('image', output)
        self._base_step()
Пример #15
0
    def test_size(self):
        img = Image()
        nose.tools.assert_equal(img.size(), 0)

        img = Image(720, 480)
        nose.tools.assert_equal(img.size(), 720*480)
Пример #16
0
 def test_new_type(self):
     # allocated a uint32_t image
     img = Image(720, 480, 3, True, Image.PIXEL_UNSIGNED, 4)
Пример #17
0
 def test_new_sized(self):
     img = Image(720, 480)
Пример #18
0
 def test_new(self):
     img = Image()
Пример #19
0
 def test_getitem_uint8(self):
     img = Image(720, 480)
     nose.tools.assert_equal(img.pixel_type_name(), "uint8")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #20
0
 def test_pil_RGBA(self):
     # test RGBA image
     img = Image(720, 480, 4, True)
     pil_img = img.get_pil_image()
     img2 = Image.from_pil(pil_img)
     nose.tools.assert_equal(img.equal_content(img2), True)
Пример #21
0
 def test_pil_I(self):
     # test int image
     img = Image(720, 480, 1, True, Image.PIXEL_SIGNED, 4)
     pil_img = img.get_pil_image()
     img2 = Image.from_pil(pil_img)
     nose.tools.assert_equal(img.equal_content(img2), True)
Пример #22
0
 def test_pil_1(self):
     # test bool image
     img = Image(720, 480, 1, True, Image.PIXEL_BOOL, 1)
     pil_img = img.get_pil_image()
     img2 = Image.from_pil(pil_img)
     nose.tools.assert_equal(img.equal_content(img2), True)
Пример #23
0
 def test_copy_from(self):
     img = Image(720, 480, 3, True, Image.PIXEL_FLOAT, 4)
     img2 = Image().copy_from(img)
     nose.tools.assert_equal(img.equal_content(img2), True)
Пример #24
0
 def test_getitem_double(self):
     img = Image(720, 480, 3, True, Image.PIXEL_FLOAT, 8)
     nose.tools.assert_equal(img.pixel_type_name(), "double")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #25
0
 def test_getitem_uint8(self):
     img = Image(720, 480)
     nose.tools.assert_equal(img.pixel_type_name(), "uint8")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #26
0
    def test_size(self):
        img = Image()
        nose.tools.assert_equal(img.size(), 0)

        img = Image(720, 480)
        nose.tools.assert_equal(img.size(), 720*480)
Пример #27
0
def from_pil(pil_image):
    """
    Construct Image from supplied PIL image object.
    :param pil_image: PIL image object
    :type pil_image: PIL.Image.Image
    :raises RuntimeError: If the PIL Image provided is not in a recognized
       mode.
    :returns: New Image instance using the given image's pixels.
    :rtype: Image
    """

    (img_width, img_height) = pil_image.size
    mode = pil_image.mode
    # TODO(paul.tunison): Extract this logic out into a utility function.
    if mode == "1":  # boolean
        img_depth = 1
        img_w_step = 1
        img_h_step = img_width
        img_d_step = 0
        img_pix_num_bytes = 1
        img_pix_type = Image.PIXEL_BOOL
    elif mode == "L":  # 8-bit greyscale
        img_depth = 1
        img_w_step = 1
        img_h_step = img_width
        img_d_step = 0
        img_pix_num_bytes = 1
        img_pix_type = Image.PIXEL_UNSIGNED
    elif mode == "RGB":  # 8-bit RGB
        img_depth = 3
        img_w_step = 3
        img_h_step = img_width * 3
        img_d_step = 1
        img_pix_num_bytes = 1
        img_pix_type = Image.PIXEL_UNSIGNED
    elif mode == "RGBA":  # 8-bit RGB with alpha
        img_depth = 4
        img_w_step = 4
        img_h_step = img_width * 4
        img_d_step = 1
        img_pix_num_bytes = 1
        img_pix_type = Image.PIXEL_UNSIGNED
    elif mode == "I":  # 32-bit signed int greyscale
        img_depth = 1
        img_w_step = 1
        img_h_step = img_width
        img_d_step = 0
        img_pix_num_bytes = 4
        img_pix_type = Image.PIXEL_SIGNED
    elif mode == "F":  # 32-bit float greyscale
        img_depth = 1
        img_w_step = 1
        img_h_step = img_width
        img_d_step = 0
        img_pix_num_bytes = 4
        img_pix_type = Image.PIXEL_FLOAT
    else:
        raise RuntimeError("Unsupported image format.")

    img_data = _pil_image_to_bytes(pil_image)
    vital_img = Image(img_data, img_width, img_height, img_depth, img_w_step,
                      img_h_step, img_d_step, img_pix_type, img_pix_num_bytes)
    return vital_img
Пример #28
0
 def test_getitem_double(self):
     img = Image(720, 480, 3, True, Image.PIXEL_FLOAT, 8)
     nose.tools.assert_equal(img.pixel_type_name(), "double")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #29
0
 def test_getitem_int32(self):
     img = Image(720, 480, 3, True, Image.PIXEL_SIGNED, 4)
     nose.tools.assert_equal(img.pixel_type_name(), "int32")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #30
0
 def test_getitem_int32(self):
     img = Image(720, 480, 3, True, Image.PIXEL_SIGNED, 4)
     nose.tools.assert_equal(img.pixel_type_name(), "int32")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #31
0
 def test_getitem_bool(self):
     img = Image(720, 480, 1, True, Image.PIXEL_BOOL, 1)
     nose.tools.assert_equal(img.pixel_type_name(), "bool")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)
Пример #32
0
 def test_size(self):
     i = Image(720, 480)
     ic = ImageContainer(i)
     nose.tools.assert_equal(ic.size(), 720 * 480)
Пример #33
0
 def test_width(self):
     i = Image(720, 480)
     ic = ImageContainer(i)
     nose.tools.assert_equal(ic.width(), 720)
Пример #34
0
 def test_height(self):
     i = Image(720, 480)
     ic = ImageContainer(i)
     nose.tools.assert_equal(ic.height(), 480)
Пример #35
0
    def test_new(self):
        image = Image()
        img_c = ImageContainer(image)

        image = Image(100, 100)
        img_c = ImageContainer(image)
Пример #36
0
 def test_getitem_bool(self):
     img = Image(720, 480, 1, True, Image.PIXEL_BOOL, 1)
     nose.tools.assert_equal(img.pixel_type_name(), "bool")
     val1 = img[0,0]
     val2 = img[0,0,0]
     nose.tools.assert_equal(val1, val2)