Skip to content

Commit

Permalink
Added all benchmaring scripts and codes
Browse files Browse the repository at this point in the history
  • Loading branch information
saswatlevin committed Oct 19, 2020
1 parent 2a1c7eb commit 570684f
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benchmarking/compile_benchmarks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
g++ -std=c++11 mae.cpp -o mae `pkg-config opencv --cflags --libs`
g++ -std=c++11 npcr_uaci.cpp -o npcr_uaci `pkg-config opencv --cflags --libs`
g++ -std=c++11 mse.cpp -o mse `pkg-config opencv --cflags --libs`
g++ -std=c++11 resize.cpp -o resize `pkg-config opencv --cflags --libs`
g++ -std=c++11 pixel_replace.cpp -o pixel_replace `pkg-config opencv --cflags --libs`
8 changes: 8 additions & 0 deletions benchmarking/entropy_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from skimage import io
import skimage.measure
import sys

img = io.imread(sys.argv[1])
entropy = skimage.measure.shannon_entropy(img)
print("\nentropy = " + str(entropy))

55 changes: 55 additions & 0 deletions benchmarking/img_diff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream> /*For IO*/
#include <cstdlib> /*For standard C functions*/
#include <cstring> /*For char * strings */
#include <cmath>
#include <cstdio>
#include <opencv2/opencv.hpp> /*For OpenCV*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
cv::Mat img1 = cv::imread(std::string(argv[1]));

if(!img1.data)
{
cout<<"\nCould not load image from "<<argv[1]<<"\nExiting...";
exit(0);
}


cv::Mat img2 = cv::imread(std::string(argv[2]));

if(!img2.data)
{
cout<<"\nCould not load image from "<<argv[2]<<"\nExiting...";
exit(0);
}

int diff = 0;
int m = (int)img1.rows;
int n = (int)img1.cols;
int channels = (int)img1.channels();
int cnt = 0;

for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
for(int k = 0; k < channels; ++k )
{
diff = img1.at<Vec3b>(i, j)[k] - img2.at<Vec3b>(i, j)[k];
if(diff != 0)
{
++cnt;
}
}
}
}

cout<<"\nNumber of differences = "<<cnt;
return 0;
}
60 changes: 60 additions & 0 deletions benchmarking/mae.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream> /*For IO*/
#include <cstdlib> /*For standard C functions*/
#include <cstring> /*For char * strings */
#include <cmath>
#include <cstdio>
#include <opencv2/opencv.hpp> /*For OpenCV*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

/**
* Calculates the Mean Absolute Error between the plain image and the encrypted image
*/

static inline void MAE(cv::Mat img1,cv::Mat img2,int m,int n,int ch)
{
double init = 0,sum_mae = 0;

for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
for(int k = 0; k < ch; ++k)
{

init = (double)img1.at<Vec3b>(i,j)[k] - img2.at<Vec3b>(i,j)[k];
sum_mae += fabs(init);

}
}
}

sum_mae = (sum_mae) / (m * n * ch);
printf("\nMAE = %F",sum_mae);

}

int main(int argc,char *argv[])
{
std::string image_name_1 = std::string(argv[1]);
std::string image_name_2 = std::string(argv[2]);

cv::Mat img1 = cv::imread(image_name_1,cv::IMREAD_UNCHANGED);
cv::Mat img2 = cv::imread(image_name_2,cv::IMREAD_UNCHANGED);



int m = img1.rows;
int n = img1.cols;
int ch = img1.channels();

cout<<"\nRows = "<<m;
cout<<"\nColumns = "<<n;
cout<<"\nChannels = "<<ch;
MAE(img1,img2,m,n,ch);

return 0;
}
59 changes: 59 additions & 0 deletions benchmarking/mse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream> /*For IO*/
#include <cstdlib> /*For standard C functions*/
#include <cstring> /*For char * strings */
#include <cmath>
#include <cstdio>
#include <opencv2/opencv.hpp> /*For OpenCV*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

/**
* Calculates the Mean Squared Error between the corresponding plain images of 2 encrypted images, one of which is noisy
*/

static inline void MSE(cv::Mat img1,cv::Mat img2,int m,int n,int ch)
{
double init = 0,sum_mse = 0;

for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
for(int k = 0; k < ch; ++k)
{

init = (double)img1.at<Vec3b>(i,j)[k] - img2.at<Vec3b>(i,j)[k];
sum_mse += fabs(init);

}
}
}

sum_mse = (sum_mse * 100) / (m * n * ch);
printf("\nMSE = %F",sum_mse);

}

int main(int argc,char *argv[])
{
std::string image_name_1 = std::string(argv[1]);
std::string image_name_2 = std::string(argv[2]);

cv::Mat img1 = cv::imread(image_name_1,cv::IMREAD_UNCHANGED);
cv::Mat img2 = cv::imread(image_name_2,cv::IMREAD_UNCHANGED);

int m = img1.rows;
int n = img1.cols;
int ch = img1.channels();

cout<<"\nRows = "<<m;
cout<<"\nColumns = "<<n;
cout<<"\nChannels = "<<ch;

MSE(img1,img2,m,n,ch);

return 0;
}
66 changes: 66 additions & 0 deletions benchmarking/npcr_uaci.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream> /*For IO*/
#include <cstdlib> /*For standard C functions*/
#include <cstring> /*For char * strings */
#include <cmath>
#include <cstdio>
#include <opencv2/opencv.hpp> /*For OpenCV*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

/**
* Calculates the Number of Pixels Change Rate between two encrypted images whose corresponding plain images differ by a single pixel
*/

static inline void NPCR_UACI(cv::Mat img1,cv::Mat img2,int m,int n,int ch)
{
double NPCR = 0,UACI = 0,init = 0,sum = 0,sum_uaci = 0;

for(int i = 0; i < img1.rows; ++i)
{
for(int j = 0; j < img1.cols; ++j)
{
for(int k = 0; k < img1.channels(); ++k)
{

init = (double)img1.at<Vec3b>(i,j)[k] - img2.at<Vec3b>(i,j)[k];
sum_uaci += fabs(init);
if(img1.at<Vec3b>(i,j)[k] != img2.at<Vec3b>(i,j)[k])
{
NPCR = 1;
sum += NPCR;
}
}
}
}

sum = (sum * 100) / (m * n * ch);
printf("\nNPCR = %F",sum);
sum_uaci = sum_uaci / (m * n * 255 * ch);
printf("\nUACI = %F",sum_uaci * 100);
}

int main(int argc,char *argv[])
{
std::string image_name_1 = std::string(argv[1]);
std::string image_name_2 = std::string(argv[2]);

cv::Mat img1 = cv::imread(image_name_1,cv::IMREAD_UNCHANGED);
cv::Mat img2 = cv::imread(image_name_2,cv::IMREAD_UNCHANGED);



int m = img1.rows;
int n = img1.cols;
int ch = img1.channels();

cout<<"\nRows = "<<m;
cout<<"\nColumns = "<<n;
cout<<"\nChannels = "<<ch;

NPCR_UACI(img1,img2,m,n,ch);

return 0;
}
47 changes: 47 additions & 0 deletions benchmarking/resize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream> /*For IO*/
#include <cstdlib> /*For standard C functions*/
#include <cstring> /*For char * strings */
#include <cmath>
#include <cstdio>
#include <opencv2/opencv.hpp> /*For OpenCV*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
/**
* Resize an image
*/
static inline std::string getFileNameFromPath(std::string filename)
{
const size_t last_slash_idx = filename.find_last_of("\\/");
if (std::string::npos != last_slash_idx)
{
filename.erase(0, last_slash_idx + 1);
}

// Remove extension if present.
const size_t period_idx = filename.rfind('.');
if (std::string::npos != period_idx)
{
filename.erase(period_idx);
}

return filename;
}

int main(int argc,char *argv[])
{
std::string image_path = std::string(argv[1]);
std::string image_name = getFileNameFromPath(image_path);

int rows = atoi((const char*)argv[2]);
int cols = atoi((const char*)argv[3]);
cv::Mat image = cv::imread(image_path,cv::IMREAD_UNCHANGED);
cv::resize(image,image,cv::Size(cols,rows),CV_INTER_LANCZOS4);
std::string new_image_name = "";
new_image_name = new_image_name + image_name + "_" + std::to_string(rows) + "_" + std::to_string(cols) + ".png";
cout<<"\nNew image name = "<<new_image_name;
cv::imwrite(new_image_name,image);
return 0;
}
7 changes: 7 additions & 0 deletions benchmarking/salt_pepper_noise.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pkg load image
img_path = "baboon_1024_1024_ENC.png"
img = imread(img_path);
noisy_img = imnoise(img,'salt & pepper',0.05);
imwrite(noisy_img,"baboon_1024_1024_ENC_diffused.png");


0 comments on commit 570684f

Please sign in to comment.