Giter VIP home page Giter VIP logo

Comments (5)

jcupitt avatar jcupitt commented on July 19, 2024

The fourth band is the alpha. Just take the first three to remove it:

rgb = alpha[0:3]

from pyvips.

deepakanandece avatar deepakanandece commented on July 19, 2024

How to convert this pyvips image to numpy array of dimension HW x 3? By this I need to vectorize the image. I feel that if I was able to define a pyvios image with alpha channel ignored then I can write it using vips2numpy method where we assign shape as [HW,3]. Am I correct?

from pyvips.

jcupitt avatar jcupitt commented on July 19, 2024

Sorry, I don't understand. vips2numpy should work, shouldn't it?

Post some code I can run and explain the error you see.

from pyvips.

deepakanandece avatar deepakanandece commented on July 19, 2024
import pyvips
import numpy as np
format_to_dtype = {
    'uchar': np.uint8,
    'char': np.int8,
    'ushort': np.uint16,
    'short': np.int16,
    'uint': np.uint32,
    'int': np.int32,
    'float': np.float32,
    'double': np.float64,
    'complex': np.complex64,
    'dpcomplex': np.complex128,
}

**#map np dtypes to vips**
dtype_to_format = {
    'uint8': 'uchar',
    'int8': 'char',
    'uint16': 'ushort',
    'int16': 'short',
    'uint32': 'uint',
    'int32': 'int',
    'float32': 'float',
    'float64': 'double',
    'complex64': 'complex',
    'complex128': 'dpcomplex',
}
def numpy2vips(a):
    height, width, bands = a.shape
    linear = a.reshape(width * height * bands)
    vi = pyvips.Image.new_from_memory(linear.data, width, height, bands,
                                      dtype_to_format[str(a.dtype)])
    return vi
def vips2numpy(vi):
    return np.ndarray(buffer=vi.write_to_memory(),
                      dtype=format_to_dtype[vi.format],
                      shape=[vi.height, vi.width, vi.bands])
filename = '/home/Drive2/deepak/nikhil/her2_negative_svs/TCGA-AR-A24R-01Z-00-DX1.47D79205-63E7-43E6-A513-2CD10DFA53C9.svs'
Ip = pyvips.Image.new_from_file(filename)
Ip
**Out[8]:** <pyvips.Image 106623x65505 uchar, 4 bands, rgb>
**#simple vips2numpy**
def vips2numpy_vect(vi):
    return np.ndarray(buffer=vi.write_to_memory(),
                      dtype=format_to_dtype[vi.format],
                      shape=[vi.height*vi.width, vi.bands])
img = vips2numpy_vect(Ip)
img.shape
**Out[11]:** (6984339615, 4)
del img
**#vips2numpy for just taking first three channels (r,g,b)**
def vips2numpy_vect(vi):
    return np.ndarray(buffer=vi.write_to_memory(),
                      dtype=format_to_dtype[vi.format],
                      shape=[vi.height*vi.width, 3])
img = vips2numpy_vect(Ip)
img.shape
**Out[15]:** (6984339615, 3)

So first time i have 4 channels and vips2numpy satisfies the number of elements. What happens the second time? Are these 3 channels r,g,b or it is some component of the previous numpy array.

from pyvips.

jcupitt avatar jcupitt commented on July 19, 2024

It should just work. Numpy lets you reshape arrays quickly, I think. Use array slicing in pyvips to drop the alpha.

#!/usr/bin/python3

import sys
import pyvips
import numpy as np

format_to_dtype = {
    'uchar': np.uint8,
    'char': np.int8,
    'ushort': np.uint16,
    'short': np.int16,
    'uint': np.uint32,
    'int': np.int32,
    'float': np.float32,
    'double': np.float64,
    'complex': np.complex64,
    'dpcomplex': np.complex128,
}

#map np dtypes to vips
dtype_to_format = {
    'uint8': 'uchar',
    'int8': 'char',
    'uint16': 'ushort',
    'int16': 'short',
    'uint32': 'uint',
    'int32': 'int',
    'float32': 'float',
    'float64': 'double',
    'complex64': 'complex',
    'complex128': 'dpcomplex',
}
def numpy2vips(a):
    height, width, bands = a.shape
    linear = a.reshape(width * height * bands)
    vi = pyvips.Image.new_from_memory(linear.data, width, height, bands,
                                      dtype_to_format[str(a.dtype)])
    return vi
def vips2numpy(vi):
    return np.ndarray(buffer=vi.write_to_memory(),
                      dtype=format_to_dtype[vi.format],
                      shape=[vi.height, vi.width, vi.bands])

image = pyvips.Image.new_from_file(sys.argv[1])

print("loaded", image)

print("as a 4 band numpy image:")
nump = vips2numpy(image)
print(nump.shape)

# drop the alpha channel
image = image[0:3]
print("as a 3 band numpy image:")
nump = vips2numpy(image)
print(nump.shape)

# finally, as a (W * H, 3) image
print("as a 3 band linear image:")
nump = nump.reshape(image.width * image.height, 3)
print(nump.shape)

I see:

$ python3 try283.py ~/pics/openslide/CMU-1-Small-Region.svs
loaded <pyvips.Image 2220x2967 uchar, 4 bands, rgb>
as a 4 band numpy image:
(2967, 2220, 4)
as a 3 band numpy image:
(2967, 2220, 3)
as a 3 band linear image:
(6586740, 3)

from pyvips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.