Thread: Statistical Distribution And When You're Not Looking For An *Exact* Answer

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Statistical Distribution And When You're Not Looking For An *Exact* Answer

    Hello,

    I'm engaging in a spot of signal processing on an image. I'm trying to sample to the Hue, Saturation and Luminance (HSL) of each pixel in the image, determine which hue/saturation is most common and then copy that to each pixel in order to make the entire image look like various shades of one colour (retaining details as I'm not changing the L for each pixel). This is like the "Colorize" effect in Paint Shop Pro if you've seen that.

    What I'm currently doing at the moment is:-

    • Convert each RGB pixel into HSL
    • Put the H and S components into a binary tree ordered by H, incrementing count if a particular H/S combination already exists
    • Afterwards retrieve the tree node with the highest count and use it to "paint" all the pixels

    While this approach does work in an ideal case, in testing I've noticed images that are a mix of what appear to be shades of gray becoming red or blue after processing. I've tracked this down to the following:-

    H, S and L are all floating-point values stored in doubles. Although there are many H/S combinations that are very similar, very few stand up to my primitive "does input H/S == tree node H/S?" comparison, exact matching. This results in a very small number of e.g. blacks which have identical red/blue hue/saturation values having the "highest" count and being used to paint the rest.

    Is there a statistical method I could use to try to group similar values together, so in being less precise I have a more accurate sample?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Say your HSL is between 0.0 and 1.0.

    Multiply by say 100 and take the integer part (0 to 100).

    This generates a finite approximation based on the most significant portion of the floating point value.

    Use this to navigate your tree.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I'm not sure that that would work Salem. Like I said, there are many similar values, but it doesn't look like truncating them is gonna get them any closer to each other. An example:-

    This 12x12 image: http://www.imalarkey.org.uk/images/test.png

    It's a bit small, so here's it zoomed in:-
    http://www.imalarkey.org.uk/images/test2.png

    It's part of a white object that has a shadow cast over it, so it looks a bit grey. You can almost make out the slight blueness to it. There is a single line of dark blue pixels towards the bottom and then lots of black and near-black.

    Here's the HSL readout of all 144 pixels, grouped line by line (default printf precision output). Note that as the program accepts normal BMP input this is from the bottom line of the image upwards:-
    H = 0.625000, S = 1.000000
    H = 0.633333, S = 1.000000
    H = 0.633333, S = 1.000000
    H = 0.055556, S = 1.000000
    H = 0.833333, S = 1.000000
    H = 0.000000, S = 1.000000 *
    H = 0.083333, S = 1.000000
    H = 0.000000, S = 0.000000
    H = 0.000000, S = 1.000000 *
    H = 0.000000, S = 1.000000 *
    H = 0.166667, S = 1.000000
    H = 0.083333, S = 1.000000

    H = 0.615385, S = 0.764706
    H = 0.633333, S = 0.714286
    H = 0.629630, S = 0.900000
    H = 0.633333, S = 0.769231
    H = 0.626984, S = 0.677419
    H = 0.630435, S = 0.560976
    H = 0.611111, S = 1.000000
    H = 0.666667, S = 1.000000
    H = 0.611111, S = 1.000000
    H = 0.611111, S = 0.600000
    H = 0.625000, S = 0.666667
    H = 0.666667, S = 1.000000

    H = 0.651515, S = 0.086614
    H = 0.589744, S = 0.102362
    H = 0.600000, S = 0.130435
    H = 0.614035, S = 0.138686
    H = 0.637681, S = 0.154362
    H = 0.641975, S = 0.183673
    H = 0.637931, S = 0.200000
    H = 0.627778, S = 0.214286
    H = 0.630208, S = 0.210526
    H = 0.629630, S = 0.818182
    H = 0.633333, S = 1.000000
    H = 0.600000, S = 0.833333

    H = 0.731481, S = 0.081081
    H = 0.722222, S = 0.076923
    H = 0.696078, S = 0.072340
    H = 0.733333, S = 0.065502
    H = 0.717949, S = 0.057269
    H = 0.717949, S = 0.056277
    H = 0.722222, S = 0.064935
    H = 0.725490, S = 0.076233
    H = 0.712963, S = 0.080357
    H = 0.710526, S = 0.083700
    H = 0.710526, S = 0.081545
    H = 0.692982, S = 0.080851

    H = 0.735294, S = 0.071730
    H = 0.731481, S = 0.078261
    H = 0.728070, S = 0.082969
    H = 0.725000, S = 0.086957
    H = 0.731481, S = 0.075630
    H = 0.705882, S = 0.074236
    H = 0.739583, S = 0.071429
    H = 0.738095, S = 0.063063
    H = 0.738095, S = 0.063063
    H = 0.739583, S = 0.068376
    H = 0.735294, S = 0.072340
    H = 0.731481, S = 0.078947

    H = 0.739583, S = 0.072072
    H = 0.739583, S = 0.072072
    H = 0.739583, S = 0.070796
    H = 0.731481, S = 0.080357
    H = 0.736842, S = 0.080851
    H = 0.730159, S = 0.093333
    H = 0.730159, S = 0.093333
    H = 0.731481, S = 0.078261
    H = 0.696078, S = 0.073593
    H = 0.739583, S = 0.073394
    H = 0.733333, S = 0.067873
    H = 0.744444, S = 0.065502

    H = 0.727273, S = 0.099099
    H = 0.728070, S = 0.085973
    H = 0.705882, S = 0.076233
    H = 0.744444, S = 0.066079
    H = 0.731481, S = 0.081818
    H = 0.731481, S = 0.081818
    H = 0.736842, S = 0.085202
    H = 0.730159, S = 0.094170
    H = 0.727273, S = 0.097345
    H = 0.727273, S = 0.097345
    H = 0.722222, S = 0.077586
    H = 0.705882, S = 0.073593

    H = 0.740741, S = 0.087379
    H = 0.728070, S = 0.088372
    H = 0.727273, S = 0.100917
    H = 0.730159, S = 0.098592
    H = 0.724638, S = 0.105991
    H = 0.725000, S = 0.095238
    H = 0.733333, S = 0.066079
    H = 0.731481, S = 0.084906
    H = 0.731481, S = 0.084906
    H = 0.730159, S = 0.098592
    H = 0.727273, S = 0.103774
    H = 0.724638, S = 0.105991

    H = 0.740741, S = 0.080357
    H = 0.739583, S = 0.075472
    H = 0.739583, S = 0.076190
    H = 0.735294, S = 0.079812
    H = 0.731481, S = 0.081818
    H = 0.728070, S = 0.087558
    H = 0.730159, S = 0.095023
    H = 0.722222, S = 0.115385
    H = 0.727273, S = 0.106796
    H = 0.754902, S = 0.076233
    H = 0.735294, S = 0.079812
    H = 0.739583, S = 0.076190

    H = 0.704545, S = 0.110000
    H = 0.724638, S = 0.113300
    H = 0.727273, S = 0.102804
    H = 0.725490, S = 0.079070
    H = 0.739583, S = 0.075472
    H = 0.729167, S = 0.077670
    H = 0.722222, S = 0.084906
    H = 0.728070, S = 0.087558
    H = 0.730159, S = 0.099526
    H = 0.730159, S = 0.096774
    H = 0.724638, S = 0.110048
    H = 0.727273, S = 0.103774

    H = 0.700000, S = 0.102041
    H = 0.698413, S = 0.106599
    H = 0.698413, S = 0.108808
    H = 0.696970, S = 0.115789
    H = 0.724638, S = 0.117949
    H = 0.727273, S = 0.105769
    H = 0.705882, S = 0.083744
    H = 0.708333, S = 0.076190
    H = 0.705882, S = 0.086294
    H = 0.710526, S = 0.095477
    H = 0.708333, S = 0.095238
    H = 0.706349, S = 0.107692

    H = 0.666667, S = 0.081633
    H = 0.694444, S = 0.091837
    H = 0.692982, S = 0.097436
    H = 0.690476, S = 0.104478
    H = 0.690476, S = 0.106599
    H = 0.696970, S = 0.108911
    H = 0.695652, S = 0.110048
    H = 0.727273, S = 0.105769
    H = 0.727273, S = 0.110000
    H = 0.696078, S = 0.085427
    H = 0.696078, S = 0.084577
    H = 0.694444, S = 0.090909
    Most popular is H = 0.000000, S = 1.000000

    Note that as I'm currently performing an exact comparison to increment tree nodes, the three values with * next to them are the ones that eventually create the most popular HS value. I think there is at least one other node with 3 values but how do you decide between them?

    It's no surprise that these pixels, the 6th, 9th and 10th on the bottom line are virtually black (R = 1, G = 0, B = 0). Unfortunately the current program rates these above all other and produces this result: http://www.imalarkey.org.uk/images/output.png
    Here it is again zoomed in:-
    http://www.imalarkey.org.uk/images/output2.png

    As you can see, what we could relatively easily perceive as being a blueish hue has become pure red. This only appears to happen in certain circumstances, usually when there's a black/white patch in an image.

    I need to find a way to properly count the large grey patch.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I don't know much about what you are doing, but as far as things I have worked on that I needed a not so exact answer from very exact numbers were AI things, and fuzzy logic provides a really good solution for that, perhaps you should look it up. Once you learn to think using fuzzy logic, it becomes easier to put things in human terms. May not help you for this though, not really sure how what you are doing works lol, but taking numbers and putting them into more human terms fuzzy logic is good at.

Popular pages Recent additions subscribe to a feed