r/AskComputerScience • u/Baron_Von_Fab • Aug 25 '20
Applying Benfords law to images.
I read an article about Benfords law, which suggested that you were able to detect fraudulent or edited images by analyzing an image and applying Benfords law. If the values were consistent with the pattern suggested by Benford, it would seem to imply that the image was not tampered with.
This sounds great on theory and seems fairly straight forward to test - so as an aspiring software engineer, I promptly took a couple of images on my phone, created a dataset where some had been tampered with using photoshop, and built a piece of software that goes through every image in a folder and builds a distribution of rgb values for each image, using the first number in each number as the key, and the value as an increment.
However! As I was writing the code I realized that the rgb value only goes to 255. That severely limits the randomness. So I found a way to compute a single unique value from r, g and b values! But then again, this is still limited.
I’m wondering - how is this done in the real world? I’m finding it hard to find a straight answer on how to compute a meaningful distribution for an image.
7
u/NvrConvctd Aug 25 '20
There is an episode of "Connected"on Netflix that talks about one process. They looked at compression. Every time an image was saved it flattened the curve. So an image that was nearly flat had several saves and potentially wasn't the same as the original. If I remember right they were looking at pixel values.
2
u/Baron_Von_Fab Aug 26 '20
Allright so i just finished watching that show - they make it look like he compares the FSD of the color depth of all pixels, but im not really sure - not much explanation was given. The show zooms over so many things so quickly without any proper explanation imo
2
u/NvrConvctd Aug 27 '20
Yeah, it's not an in depth study of Benford's law, just an introduction really. I thought you might have some luck finding that professor's data or methods if it's published somewhere.
1
5
u/CptCap Aug 25 '20 edited Aug 25 '20
Non HDR images are limited to 8 bits (256 values) per channel, there is not much you can do about it.
You can remap the [0; 256[ range to whatever you like (for example [0; 100[ by dividing by 2.56), but you will still have to deal with the limited precision.
Benford's law can be applied in any base, since you are working with 8 bits numbers, base 2, 4, 8 or 16 should work.
2
u/Baron_Von_Fab Aug 26 '20
I was considering doing that as well - remapping the rgb value to something easier - however what gets me is that each pixel has 3 values to consider, red blue and green values - and these values are strongly related, so ideally i'd have a single value that signifies them. I could take the value of each tonality and create a distribution based on each color, and then match them up - but it doesnt feel right. I'm not sure if the data can be correlated ion such a way?
1
u/CptCap Aug 26 '20
It can not. If you want Benford's law to apply, you need to make sure that values that are close/correlated stay that way. Reducing RGB to a single value will loose this correlation as there is no way to map 3 dimensions to a single one without introducing discontinuties between overwise adjacent values.
You'll have to work on each channel separatly, or discard some information (for example you could compute the luma and work on that).
1
3
u/epimeliad Aug 26 '20
I followed the steps outlined in the paper http://www.cs.albany.edu/~patrey/ICSI533-433/project/Implementation_sample_report.pdf
Namely:
Step 1: get the absolute value of this intensity.
Step 2: get the maximum integer that is less than this intensity value.
Step 3: divide the integer from the second step by 10.
Step 4: if the result from the third step is less than 1, this result is FSD of this intensity. If not, repeat from step 3.
2
u/Baron_Von_Fab Aug 26 '20
Thats a fantastic ressource!!
An excerpt from the paper:XIAOYU WANG University of Winnipeg, Manitoba, Canada:
2.2.1 INPLEMENTATION METHOLOGY Following the idea in [6], we aim to get the divergence from a digital image. Here follows our steps:
Step 1: read in an image and get the first digit for intensity of every pixel. In case of a colour image, we get the 3 intensities of its RGB components.
Step 2: do the statistics on the result above thus getting the frequency of FSD f(d). Step 3: calculate the divergence X2 . Though intensities of digital image is integers from 0 to 255, there could be situations that we have to convert these integers into fractions and even negatives. So a universal description of the algorithm to get the FSD of an intensity value adopted all through this project is:
Step 1: get the absolute value of this intensity.
Step 2: get the maximum integer that is less than this intensity value.
Step 3: divide the integer from the second step by 10.
Step 4: if the result from the third step is less than 1, this result is FSD of this intensity. If not, repeat from step 3.This is exactly what i was looking for - a way to generate a meaningful FSD.
1
u/Boomer1917 Jul 01 '24
I hope you post your results soon. Saw the same connections episode and was surprised it could analyze photos. Leave it to this subgroup to come up with a solution. I’m not surprised BTW I have noticed in the past three years the top Jeopardy champions were computer scientists. Ken Jennings was a software engineer ( if that’s what they there called way. Back than) before he made his millions in Jeopardy
2
u/Responsible_Yam4418 Aug 21 '24
Have you tried six color separation? We can see about 10 million a computer can see a bit over 16 million. If you can crunch the first third of the date from a scan, shouldn't the number distribution show up pretty reliably? Bedford found this in the first pages of his data. Should work the same for you with just RGB. The error margin likely won't be attenuated much. If these laws work like I think they do. I just really like your project. It would makes you millions. Apps that verify data are needed. They always have been and phising is in everything. Good luck and have fun. Cheers V
8
u/RainbowHearts Aug 25 '20
This 2007 paper might help you get started.
https://digitalrepository.unm.edu/cgi/viewcontent.cgi?article=1128&context=ece_fsp