spectralpython / spectral

Python module for hyperspectral image processing
MIT License
571 stars 139 forks source link

Inverse PCT? #104

Closed jason-brown904 closed 3 years ago

jason-brown904 commented 4 years ago

I'm curious if there is a method to do an inverse PCT with Spectral?

I want to do a PCT fusion like this: 1) take the PCT of a multispectral image 2) replace the PC1 with another image 3) inverse PCT 4) display fused image

Is that possible with principal_components() ?

tboggs commented 4 years ago

Just to be clear, when you say "replace the PC1 with another image" you mean to just replace the first PC vector with the first PC vector from another image? That is, you want to replace one of the actual eigenvectors (not an image raster layer in PC space).

jason-brown904 commented 4 years ago

Yes, then inverse transform.

tboggs commented 4 years ago

Well, there's not a "method" to do that per se but I think you could create a hack to do it. The principal_components function just returns a PrincipalComponents object. If you look at the methods in that class, you could fairly easily create a subclass to do it.

Here's some completely untested code, which may do what you want:

from spectral.algorithms.algorithms import PrincipalComponents
from spectral.algorithms.transforms import LinearTransform

class PCFuser(PrincipalComponents):
    def __init__(self, pc):
        super().__init__(pc.eigenvalues, pc.eigenvectors, pc.stats)

    def get_fused_image(self, image, V_mod):
        V = self.eigenvectors
        f = LinearTransform(V_mod.dot(V.T), pre=-self.mean,
                            post=self.mean)
        return f(image)

Then, to use it:

pc = spy.principal_components(X)

# Replace the first eigenvector (column) in a new array
vecs = np.array(pc.eigenvectors)
vecs[:, 0] = some_other_vector

# Compute the fused image
pcf = PCFuser(pc)
fused_image = pcf.get_fused_image(X, vecs)

Let me know if that works for you. One thing I'll point out though...

If you replace one of the eigenvectors without doing some additional voodoo, it is likely that your eigenvectors will no longer be orthogonal. So if you intend to use that PC object for doing any other operations (denoising, etc.), all warranties are voided.

I should also point out that there were no warranties to begin with ;-)

jason-brown904 commented 4 years ago

Thanks! I'll give it a try and let you know how it works out. #nowarranties

tboggs commented 3 years ago

Closing due to lack of activity.