Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Histogram of LCH image #2519

Closed
schmijos opened this issue Nov 6, 2021 · 3 comments
Closed

Histogram of LCH image #2519

schmijos opened this issue Nov 6, 2021 · 3 comments

Comments

@schmijos
Copy link

schmijos commented Nov 6, 2021

I'm trying to get an ndim histogram of the CH part of an image:

(here's the image: sample)

img = Vips::Image.new_from_file('spec/fixtures/files/sample.png', access: :sequential).colourspace(:lch)

img[1..2].to_a
=> [[[102.3130874633789, 39.18145751953125],
  [101.67424774169922, 39.16696548461914],
  [0.4031951427459717, 110.17615509033203],
  [118.2603988647461, 135.60243225097656]],
 [[102.3130874633789, 39.18145751953125],
  [102.67890930175781, 39.159664154052734],
  [118.54254913330078, 135.6748809814453],
  [0.00013431724801193923, 326.3099365234375]],
 [[133.49240112304688, 306.25054931640625],
  [0.33749398589134216, 201.97052001953125],
  [115.21880340576172, 328.395263671875],
  [95.71614074707031, 102.8958511352539]],
 [[0.019027704373002052, 326.1108093261719],
  [132.93775939941406, 306.2183532714844],
  [95.6482925415039, 102.39655303955078],
  [96.01411437988281, 102.91535186767578]]]

img[1..2].hist_find_ndim(bins: 5).to_a
=> [[[16], [0], [0], [0], [0]],
 [[0], [0], [0], [0], [0]],
 [[0], [0], [0], [0], [0]],
 [[0], [0], [0], [0], [0]],
 [[0], [0], [0], [0], [0]]]

Why are all values counted into 0,0,0?

The documentation says something about casting (hist_find_ndim), but still most of the values should be in range, shouldn't they?

@jcupitt
Copy link
Member

jcupitt commented Nov 7, 2021

Hi @schmijos,

You are taking the histogram of a float image, so it's casting it to uint16 before scanning. Five bins on a range of 0 - 65535 means each bin is more than 10,000 wide, so all your values fall in bin 0. If you cast to uint8 (uchar) yourself, you should see something more expected.

It's best to fix the range, of course. C is roughly -128 to 128, and h is 0 - 360, so you can move them to 0 - 255 like this:

#!/usr/bin/ruby

require "vips"

x = Vips::Image.new_from_file ARGV[0]
x = x.colourspace("lch")[1..2]
x = (x * [1, 256 / 360] + [128, 0]).cast("uchar")
arr = x.hist_find_ndim(bins: 5).to_a
puts "histogram: #{arr}"

I see:

$ ./schmijos.rb ~/pics/tiny.png
histogram: [[[0], [0], [4], [0], [12]], [[0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0]], [[0], [0], [0], [0], [0]]]

@jcupitt
Copy link
Member

jcupitt commented Nov 7, 2021

(perhaps the histogram functions should always cast to uchar, with ushort as an option you have to turn on explcitly? It might be less surprising ... though it's too late to change the behaviour now, unfortunately)

jcupitt added a commit that referenced this issue Nov 7, 2021
Say when u8 or u16 are picked.

See #2519
@jcupitt
Copy link
Member

jcupitt commented Nov 7, 2021

I've tried to clarify the doc comments.

@libvips libvips locked and limited conversation to collaborators Nov 7, 2021
@jcupitt jcupitt closed this as completed Nov 7, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants