1 /**
2   @file ela.cpp
3   @author Alessandro de Oliveira Faria (A.K.A. CABELO)
4   @brief Error Level Analysis (ELA) permits identifying areas within an image that are at different compression levels. With JPEG images, the entire picture should be at roughly the same level. If a section of the image is at a significantly different error level, then it likely indicates a digital modification. This example allows to see visually the changes made in a JPG image based in it's compression error analysis. Questions and suggestions email to: Alessandro de Oliveira Faria cabelo[at]opensuse[dot]org or OpenCV Team.
5   @date Jun 24, 2018
6 */
7 
8 #include <opencv2/highgui.hpp>
9 #include <iostream>
10 
11 using namespace cv;
12 
13 int scale_value = 7;
14 int quality = 95;
15 Mat image;
16 Mat compressed_img;
17 const char* decodedwin = "the recompressed image";
18 const char* diffwin = "scaled difference between the original and recompressed images";
19 
processImage(int,void *)20 static void processImage(int , void*)
21 {
22     Mat Ela;
23 
24     // Compression jpeg
25     std::vector<int> compressing_factor;
26     std::vector<uchar> buf;
27 
28     compressing_factor.push_back(IMWRITE_JPEG_QUALITY);
29     compressing_factor.push_back(quality);
30 
31     imencode(".jpg", image, buf, compressing_factor);
32 
33     compressed_img = imdecode(buf, 1);
34 
35     Mat output;
36     absdiff(image,compressed_img,output);
37     output.convertTo(Ela, CV_8UC3, scale_value);
38 
39     // Shows processed image
40     imshow(decodedwin, compressed_img);
41     imshow(diffwin, Ela);
42 }
43 
main(int argc,char * argv[])44 int main (int argc, char* argv[])
45 {
46     CommandLineParser parser(argc, argv, "{ input i | ela_modified.jpg | Input image to calculate ELA algorithm. }");
47     parser.about("\nJpeg Recompression Example:\n");
48     parser.printMessage();
49 
50     // Read the new image
51     image = imread(samples::findFile(parser.get<String>("input")));
52 
53     // Check image
54     if (!image.empty())
55     {
56         processImage(0, 0);
57         createTrackbar("Scale", diffwin, &scale_value, 100, processImage);
58         createTrackbar("Quality", diffwin, &quality, 100, processImage);
59         waitKey(0);
60     }
61     else
62     {
63         std::cout << "> Error in load image\n";
64     }
65 
66     return 0;
67 }
68