Skip to content

Tip: Generating CSV for multi-channel histograms #108

Closed

Description

@harukizaemon

I wanted to generate a histogram as a CSV file for single and multi-channel images. The format I needed was each row represents an intensity, each column represents a channel. This differs from the standard VIPS histogram which is a single row with a column for each intensity value for a single channel only.

My initial approach was to take the histogram and iterate over each element of the histogram which worked great on RGBA (about 4.5 seconds) but for a 2-byte greyscale image, this took about 5 minutes.

The following runs in about 0.07 seconds for RGBA and about 5 seconds for the ushort:

image
  .hist_find
  .rot90
  .bandunfold
  .csvsave("histogram.csv", separator: ",")

There may well be a better way, but that worked for me :)

Oh, the only other thing I wish I could do is add column headers but I can live without them.

Activity

  1. jcupitt commented on Apr 27, 2017

    @jcupitt
    Member

    Nice writeup! I've made a tip label. Yup, bandunfold is supposed to make this kind of thing easy.

  2. Nakilon commented on Feb 28, 2019

    @Nakilon
    Contributor

    I have a problem using this bandunfold operation in shell.

    vips crop $filepath_file temp.v $v_left $v_top $v_width $v_height && vips csvsave temp.v temp.csv && cat temp.csv
    
    104	103	103	103
    104	103	103	103
    104	103	103	103
    104	104	103	103
    105	105	104	104
    
    vips crop $filepath_file temp.v $v_left $v_top $v_width $v_height && vips bandunfold temp.v temp.v --factor 3 && vips csvsave temp.v temp.csv && cat temp.csv
    
    0	0	0	0	0	0	0	0	0	0	0	0
    0	0	0	0	0	0	0	0	0	0	0	0
    0	0	0	0	0	0	0	0	0	0	0	0
    0	0	0	0	0	0	0	0	0	0	0	0
    0	0	0	0	0	0	0	0	0	0	0	0
    

    It gives me all zeros.

    $ file $filepath_file 
    .../house.png: PNG image data, 120 x 162, 8-bit/color RGB, non-interlaced
    
  3. Nakilon commented on Feb 28, 2019

    @Nakilon
    Contributor

    I'm using vips-8.6.4 in this image: https://hub.docker.com/r/felixbuenemann/vips-alpine

  4. Nakilon commented on Feb 28, 2019

    @Nakilon
    Contributor

    Oh I made vips bandunfold temp.v temp_.v --factor 3 && vips csvsave temp_.v temp.csv and now it works. Seems like the bandunfold operation does not support the same filename as in and out.

  5. jcupitt commented on Feb 28, 2019

    @jcupitt
    Member

    Hello @Nakilon, yes, bandunfold streams the image, so you have to read and write to different files.

  6. locked and limited conversation to collaborators on Jan 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions