plantcv icon indicating copy to clipboard operation
plantcv copied to clipboard

Adapt patch_extract in train_kmeans.py to work with images with alpha channel

Open jksshack opened this issue 4 months ago • 3 comments

Is your feature request related to a problem? Please describe. Segmenting using unsupervised machine learning approach with the train_kmeans and kmeans_classifier, not compatible with images with alpha channel (img.shape[2]=4).

Describe the solution you'd like At least my temporary work around solution was to add line of code that identifies that image contains the alpha channel and then slices it in patch_extract definition

elif len(img.shape) == 3 and img.shape[2] == 4: #rgb with alpha img = img[:,:,:3] #removes alpha channel img_blur = np.round(gaussian(img, sigma=sigma, channel_axis=2)*255).astype(np.uint16)

Describe alternatives you've considered Maybe include a note about it under the img definition in the function description?

jksshack avatar Aug 06 '25 14:08 jksshack

Hi @jksshack thanks for opening this issue. I think the solution you propose should work nicely. Are you willing to make these changes in a Pull Request? If yes then we can add you as a contributor so you can open a PR branch (rather than a fork).

We just very recently updated pcv.predict_kmeans to accept an array directly rather than a filepath, which allows the alpha channel to be dropped with pcv.readimage(filename, mode="rgb"). So I think this means only learn.patch_extract needs updating.

HaleySchuhl avatar Aug 15 '25 15:08 HaleySchuhl

@HaleySchuhl yeah, totally willing to!

jksshack avatar Aug 19 '25 12:08 jksshack

@jksshack awesome! Thanks in advance. Malia has gone ahead and added you as a contributor. Please let me know if you have any questions.

HaleySchuhl avatar Aug 19 '25 15:08 HaleySchuhl