def read_images_via_bioformats(self, filenames_or_urls):
        '''Uses Bioformats to load images.

        filenames_or_urls -- list
        returns a list of channels as numpy float32 arrays

        '''
        channels = []
        for i, filename_or_url in enumerate(filenames_or_urls):
            image = self._read_image_via_bioformats(filename_or_url)
            channels += self._extract_channels(filename_or_url, image, 
                                               p.image_names[i],
                                               int(p.channels_per_image[i]))
                
        # Check if any images need to be rescaled, and if they are the same
        # aspect ratio. If so, do the scaling.
        from imagetools import check_image_shape_compatibility
        check_image_shape_compatibility(channels)
        if p.image_rescale:
            from imagetools import rescale
            for i in range(len(channels)):
                if channels[i].shape != p.image_rescale:
                    channels[i] = rescale(channels[i], (p.image_rescale[1], p.image_rescale[0]))

        return channels
    def read_images_via_bioformats(self, filenames_or_urls):
        '''Uses Bioformats to load images.

        filenames_or_urls -- list
        returns a list of channels as numpy float32 arrays

        '''
        channels = []
        for i, filename_or_url in enumerate(filenames_or_urls):
            image = self._read_image_via_bioformats(filename_or_url)

            channels += self._extract_channels(filename_or_url, image,
                                               p.image_names[i],
                                               int(p.channels_per_image[i]))

        # Check if any images need to be rescaled, and if they are the same
        # aspect ratio. If so, do the scaling.
        from imagetools import check_image_shape_compatibility
        check_image_shape_compatibility(channels)
        if p.image_rescale:
            from imagetools import rescale
            for i in range(len(channels)):
                if channels[i].shape != p.image_rescale:
                    channels[i] = rescale(
                        channels[i], (p.image_rescale[1], p.image_rescale[0]))

        return channels
Exemple #3
0
    def read_images_old_way(self, fds):
        '''fds -- list of file descriptors (filenames or urls)
        returns a list of channels as numpy float32 arrays
        '''
        channels = []
        for i, fd in enumerate(fds):
            format = fd.split('.')[-1]
            if format.upper() in [
                    'TIF', 'TIFF', 'BMP', 'JPG', 'JPEG', 'PNG', 'GIF', 'C01'
            ]:
                planes = self.ReadBitmap(fd)
            elif format.upper() in ['DIB']:
                planes = [self.ReadDIB(fd)]
            else:
                logging.error(
                    'Image format (%s) not supported. Skipping image "%s".' %
                    (format, fds))

            # Got fewer channels than expected
            if len(planes) < int(p.channels_per_image[i]):
                raise Exception('CPA found %d channels in the "%s" image at '
                                '%s, but it was expecting %s as specified '
                                'by properties field channels_per_image. '
                                'Please update the channels_per_image field '
                                'in your properties file, or make sure your '
                                'images are in the right format.' %
                                (len(planes), p.image_names[i], fd,
                                 p.channels_per_image[i]))

            # Got more channels than expected (load the first ones & warn)
            elif len(planes) > int(p.channels_per_image[i]):
                logging.warn(
                    'WARNING: CPA found %d channels in the "%s" image '
                    'at %s, but it will only load the first %s as '
                    'specified by properties field channels_per_image.' %
                    (len(planes), p.image_names[i], fd,
                     p.channels_per_image[i]))
                channels += planes[:int(p.channels_per_image[i])]

            # Got as many channels as expected
            else:
                channels += planes

        # Check if any images need to be rescaled, and if they are the same
        # aspect ratio. If so, do the scaling.

        from imagetools import check_image_shape_compatibility
        check_image_shape_compatibility(channels)
        if p.image_rescale:
            from imagetools import rescale
            for i in range(len(channels)):
                if channels[i].shape != map(int, p.image_rescale):
                    channels[i] = rescale(
                        channels[i],
                        (int(p.image_rescale[1]), int(p.image_rescale[0])))

        return channels
    def read_images_old_way(self, fds):
        '''fds -- list of file descriptors (filenames or urls)
        returns a list of channels as numpy float32 arrays
        '''
        channels = []
        for i, fd in enumerate(fds):
            format = fd.split('.')[-1]
            if format.upper() in ['TIF', 'TIFF', 'BMP', 'JPG', 'JPEG', 'PNG', 'GIF', 'C01']:
                planes = self.ReadBitmap(fd)
            elif format.upper() in ['DIB']:
                planes = [self.ReadDIB(fd)]
            else:
                logging.error('Image format (%s) not supported. Skipping image "%s".'%(format, fds))
            
            # Got fewer channels than expected
            if len(planes) < int(p.channels_per_image[i]):
                raise Exception('CPA found %d channels in the "%s" image at '
                                '%s, but it was expecting %s as specified '
                                'by properties field channels_per_image. '
                                'Please update the channels_per_image field '
                                'in your properties file, or make sure your '
                                'images are in the right format.'
                                %(len(planes), 
                                  p.image_names[i],
                                  fd,
                                  p.channels_per_image[i]))
            
            # Got more channels than expected (load the first ones & warn)
            elif len(planes) > int(p.channels_per_image[i]):
                logging.warn('WARNING: CPA found %d channels in the "%s" image '
                             'at %s, but it will only load the first %s as '
                             'specified by properties field channels_per_image.'
                             %(len(planes), 
                               p.image_names[i],
                               fd,
                               p.channels_per_image[i]))
                channels += planes[:int(p.channels_per_image[i])]
                
            # Got as many channels as expected
            else:
                channels += planes

        # Check if any images need to be rescaled, and if they are the same
        # aspect ratio. If so, do the scaling.

        from imagetools import check_image_shape_compatibility
        check_image_shape_compatibility(channels)
        if p.image_rescale:
            from imagetools import rescale
            for i in range(len(channels)):
                if channels[i].shape != map(int, p.image_rescale):
                    channels[i] = rescale(channels[i], (int(p.image_rescale[1]), int(p.image_rescale[0])))

        return channels
Exemple #5
0
    def read_images_via_cp(self, fds):
        '''Uses CellProfiler's LoadImagesImageProvider to load images.
        fds -- list of file descriptors (filenames or urls)
        returns a list of channels as numpy float32 arrays
        '''
        assert self.load_using_bioformats is not None
        channels = []
        for i, fd in enumerate(fds):
            if p.image_url_prepend and p.image_url_prepend.lower().startswith(
                    'http://'):
                url, ignored_headers = urllib.urlretrieve(
                    'http://' + urllib2.quote(p.image_url_prepend[7:]) +
                    urllib2.quote(fd))
            else:
                url = fd
            logging.info('Loading image from "%s"' % (url))
            image = self.load_using_bioformats(url)

            # Got 1 channel, expected more
            if image.ndim == 2 and p.channels_per_image[i] != '1':
                raise Exception(
                    'CPA found %d channels in the "%s" image at '
                    '%s, but it was expecting %s as specified '
                    'by properties field channels_per_image. '
                    'Please update the channels_per_image field '
                    'in your properties file, or make sure your '
                    'images are in the right format.' %
                    (1, p.image_names[i], fd, p.channels_per_image[i]))

            # Got fewer channels than expected
            elif image.ndim > 2 and image.shape[2] < int(
                    p.channels_per_image[i]):
                raise Exception('CPA found %d channels in the "%s" image at '
                                '%s, but it was expecting %s as specified '
                                'by properties field channels_per_image. '
                                'Please update the channels_per_image field '
                                'in your properties file, or make sure your '
                                'images are in the right format.' %
                                (image.shape[2], p.image_names[i], fd,
                                 p.channels_per_image[i]))

            # Got more channels than expected (load the first ones & warn)
            elif image.ndim > 2 and image.shape[2] > int(
                    p.channels_per_image[i]):
                logging.warn(
                    'WARNING: CPA found %d channels in the "%s" image '
                    'at %s, but it will only load the first %s as '
                    'specified by properties field channels_per_image.' %
                    (image.shape[2], p.image_names[i], fd,
                     p.channels_per_image[i]))
                channels += [
                    image[:, :, i] for i in range(int(p.channels_per_image[i]))
                ]

            # Got as many channels as expected
            else:
                if image.ndim == 2:
                    channels += [image]
                else:
                    channels += [image[:, :, i] for i in range(image.shape[2])]

        # Check if any images need to be rescaled, and if they are the same
        # aspect ratio. If so, do the scaling.
        from imagetools import check_image_shape_compatibility
        check_image_shape_compatibility(channels)
        if p.image_rescale:
            from imagetools import rescale
            for i in range(len(channels)):
                if channels[i].shape != p.image_rescale:
                    channels[i] = rescale(
                        channels[i], (p.image_rescale[1], p.image_rescale[0]))

        return channels
    def read_images_via_cp(self, fds):
        '''Uses CellProfiler's LoadImagesImageProvider to load images.
        fds -- list of file descriptors (filenames or urls)
        returns a list of channels as numpy float32 arrays
        '''
        assert self.load_using_bioformats is not None
        channels = []
        for i, fd in enumerate(fds):
            if p.image_url_prepend and p.image_url_prepend.lower().startswith('http://'):
                url, ignored_headers = urllib.urlretrieve('http://' + urllib2.quote(p.image_url_prepend[7:]) + urllib2.quote(fd))
            else:
                url = fd
            logging.info('Loading image from "%s"'%(url))
            image = self.load_using_bioformats(url)

            # Got 1 channel, expected more
            if image.ndim == 2 and p.channels_per_image[i] != '1':
                raise Exception('CPA found %d channels in the "%s" image at '
                                '%s, but it was expecting %s as specified '
                                'by properties field channels_per_image. '
                                'Please update the channels_per_image field '
                                'in your properties file, or make sure your '
                                'images are in the right format.'
                                %(1, 
                                  p.image_names[i],
                                  fd,
                                  p.channels_per_image[i]))
            
            # Got fewer channels than expected
            elif image.ndim > 2 and image.shape[2] < int(p.channels_per_image[i]):
                raise Exception('CPA found %d channels in the "%s" image at '
                                '%s, but it was expecting %s as specified '
                                'by properties field channels_per_image. '
                                'Please update the channels_per_image field '
                                'in your properties file, or make sure your '
                                'images are in the right format.'
                                %(image.shape[2], 
                                  p.image_names[i],
                                  fd,
                                  p.channels_per_image[i]))
            
            # Got more channels than expected (load the first ones & warn)
            elif image.ndim > 2 and image.shape[2] > int(p.channels_per_image[i]):
                logging.warn('WARNING: CPA found %d channels in the "%s" image '
                             'at %s, but it will only load the first %s as '
                             'specified by properties field channels_per_image.'
                             %(image.shape[2], 
                               p.image_names[i],
                               fd,
                               p.channels_per_image[i]))
                channels += [image[:,:,i] for i in range(int(p.channels_per_image[i]))]

            # Got as many channels as expected
            else:
                if image.ndim == 2:
                    channels += [image]
                else:
                    channels += [image[:,:,i] for i in range(image.shape[2])]
                
        # Check if any images need to be rescaled, and if they are the same
        # aspect ratio. If so, do the scaling.
        from imagetools import check_image_shape_compatibility
        check_image_shape_compatibility(channels)
        if p.image_rescale:
            from imagetools import rescale
            for i in range(len(channels)):
                if channels[i].shape != p.image_rescale:
                    channels[i] = rescale(channels[i], (p.image_rescale[1], p.image_rescale[0]))

        return channels