dcsimg
Results 1 to 10 of 10

Thread: Reverse of this Color calculation

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Question Reverse of this Color calculation

    I'm struggling to find the reverse of this Color calculation...

    Code:
    Public Shared Function getGrayscaleImage(ByVal input As Bitmap) As Bitmap
        Dim imgwidth As Integer = input.Width
        Dim imgheight As Integer = input.Height
    
        For y As Integer = 0 To imgheight - 1
    
            For x As Integer = 0 To imgwidth - 1
    
                Dim c As Color = input.GetPixel(x, y)
                Dim red As Integer = CInt(c.R * 0.299)
                Dim green As Integer = CInt(c.G * 0.587)
                Dim blue As Integer = CInt(c.B * 0.114)
                Dim newColor As Color = Color.FromArgb(red + green + blue, red + green + blue, red + green + blue)
    
                input.SetPixel(x, y, newColor)
            Next
        Next
    
        Return input
    
    End Function
    If it was a simple reverse of the multiplier, I would've already worked it out, but the Color.FromArgb using red + green + blue complicates the calculation...

    Code:
    Public Shared Function getColorImage(ByVal input As Bitmap) As Bitmap
        Dim imgwidth As Integer = input.Width
        Dim imgheight As Integer = input.Height
    
        For y As Integer = 0 To imgheight - 1
    
            For x As Integer = 0 To imgwidth - 1
    
                Dim c As Color = input.GetPixel(x, y)
                Dim red As Integer = ?
                Dim green As Integer = ?
                Dim blue As Integer = ?
                Dim newColor As Color = Color.FromArgb(red, green, blue)
    
                input.SetPixel(x, y, newColor)
            Next
        Next
    
        Return input
    
    End Function

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    409

    Re: Reverse of this Color calculation

    Unless I'm missing something, if all you have to go on after the fact is the grayscale RGB value where R=G=B, then you can't.

    It would be like me telling you I've added three whole numbers together and their sum is 97. What are the three numbers? You can determine all the possibilities, but you have no way to determine what the three numbers were. You don't have enough information.

    Examples:

    R=4, G=0, B=0 -> (1,1,1)
    R=0, G=2, B=0 -> (1,1,1)
    R=0, G=0, B=9 -> (1,1,1)

    So, given a grayscale value of (1,1,1), what were the original RGB values? No way to know.

  3. #3
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    409

    Re: Reverse of this Color calculation

    Another way to look at it is that since distinct RGB values result in Grayscale R=G=B, it can't be a mathematically reversible function since all the possible inputs I are limited to a maximum total possible number of outputs of I^(1/3)

    If the range of R, G, and B are 0-255 inclusive, that results in 16,777,216 possible distinct colors, but only 256 grayscale values where R=G=B.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,621

    Re: Reverse of this Color calculation

    I was just getting ready to point that out as another way of looking at it. You're reducing 16+million values down to 256. There is no way to convert those 256 values back to the original 16+ million values, those values are lost.

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Re: Reverse of this Color calculation

    Ok.. I've realized that now. Is there another reversible calculation for converting to grayscale and back, or is it just not possible?

  6. #6
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    409

    Re: Reverse of this Color calculation

    Quote Originally Posted by .paul. View Post
    Ok.. I've realized that now. Is there another reversible calculation for converting to grayscale and back, or is it just not possible?
    If you're still asking this question, then I'm confused as to what you say you've realized. It can't be done if all you are examining after the fact is the grayscale value of a pixel.

    I pick a card from a deck. That card is ran through a "ToSuit" function that stores the suit of the card that I picked. What function can that suit then be ran through to get the original card? None, it can't be done given only the suit.
    Last edited by OptionBase1; Jul 4th, 2018 at 09:01 PM.

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Re: Reverse of this Color calculation

    I've been looking at neural net conversions. The results are excellent. Does anyone have experience with that? Ideally it will be a web service i'm looking for, which would save a lot of work...

  8. #8

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,864

    Re: Reverse of this Color calculation

    Quote Originally Posted by OptionBase1 View Post
    If you're still asking this question, then I'm confused as to what you say you've realized. It can't be done if all you are examining after the fact is the grayscale value of a pixel.

    I pick a card from a deck. That card is ran through a "ToSuit" function that stores the suit of the card that I picked. What function can that suit then be ran through to get the original card? None, it can't be done given only the suit.
    I realized it was converting to 256 colors after yourself and passel pointed it out. There are ways of successfully colorizing grayscale images without photoshopping them manually. It's not a black and white answer...

  9. #9
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    409

    Re: Reverse of this Color calculation

    From a purely math standpoint, it is absolutely black and white. Taking 16,777,216 possible input combinations and turning them into 256 possible output combinations means that there are 65,536 possible input values for each output value, so there's no way to reverse the process.

    Now, taking a grayscale value of, say, R=G=B=235, I would imagine that not all input values that result in that output value are equally likely to occur in say a picture of a forest, or a beach, or a city skyline or whatever. So I'm sure there are complex algorithms that get a sense for what the contents of the picture are and can do a best guess as to what the original color MAY have been. Certainly way, way beyond anything I could provide any useful information about. Good luck.

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,021

    Re: Reverse of this Color calculation

    Quote Originally Posted by OptionBase1 View Post
    So I'm sure there are complex algorithms that get a sense for what the contents of the picture are and can do a best guess as to what the original color MAY have been.
    There are indeed. A Google search for <automatic colorization> gave this impressive site as the first hit:
    http://richzhang.github.io/colorization/

    Getting colour data from grayscale doesn't enter into it. It would be a lot harder than reconstituting a live pig from a pork sausage -- at least the sausage may contain some piggy DNA.

    BB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width