0

I was writing a program that would scramble the colors of each pixel in an image so that the resulting image would be very noisy and hard to recogize but that it would appear normal in grayscale.

As soon as I wrote the program, I noticed a problem. The alogrithm I used assumed that the way to calculate grayscale was something like

Grayscale(Pixel p) {
    average := (p.red + p.green + p.blue) / 3
    p.red := p.green := p.blue := average;
}

I later realized that the numbers have weights attached to them:

Grayscale(Pixel p) {
    average := (c1 * p.red^gamma) + (c2 * p.green^gamma) + (c3 * p.blue^gamma)
    p.red := p.green := p.blue := average
}

and thus my program did not work.

I was specifically targeting the iOS grayscale filter, although the algorithm should be the same for any system.

(side note: I don't know the weights of the iOS values, so if anyone could give me those in the algorithm as well, that'd be pretty cool. I found this thread discussing a similar problem but it doesn't answer my question: https://www.reddit.com/r/compsci/comments/a1yrf1/what_algorithm_do_ios_devices_use_to_grayscale/ )

Assuming that the grayscale uses some sort of weights, what algorithm could I use to change the rgb values of the given pixel while keeping the grayscale values of the pixel the same?

Klaus Haukenstein
  • 570
  • 1
  • 5
  • 10
  • if you're just looking for the weights: https://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity – kmdreko Dec 28 '18 at 22:10
  • Make a gradient image that goes from black (0,0,0) to red (255,0,0) with no green or blue. Check the range of the resulting greyscale image, if it goes from 0..128, the red scale factor is 0.5, if it goes from 0..64, the red scale factor is 0.25. Do the same for black to green and black to blue. – Mark Setchell Dec 29 '18 at 01:23
  • Do you have a sample image that you think would lend itself to this please? – Mark Setchell Dec 29 '18 at 16:40
  • @MarkSetchell On iOS screenshots are stored in color even if the screen is in grayscale. So the gradient is an awesome idea but I don't think it will work for iOS. (it will work for other systems I have though so still a helpful suggestion). – Klaus Haukenstein Dec 31 '18 at 14:19
  • @MarkSetchell also, do you mean an image that I would use the algorithm on? Anything that's really saturated should work best since the colors can be scrambled more (e.g. 255-0-0 can be arranged more than 255-255-255 or 0-0-0- which cannot be scrambled at all). The simplest example would be a picture that is only fully saturated red. – Klaus Haukenstein Dec 31 '18 at 14:23

0 Answers0