-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRJ1.cpp
executable file
·122 lines (101 loc) · 3.68 KB
/
PRJ1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*Project 1 is developed for applying linear filter to images.
It is run by the command line by adding the path of the image and the csv file.
The format of the csv file should be like below
1,2,3
4,5,6
7,8,9
*/
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <string>
#include <fstream>
using namespace std;
using namespace cv;
Mat applyFilter(Mat image, Mat filter);
int N;
int main(int argc, char** argv )
{
Mat src;
Mat dest;
src = imread( argv[1], 1 );
//We read the csv file
CvMLData mlData;
mlData.read_csv(argv[2]);
const CvMat* tmp = mlData.get_values();
Mat filterMatrix(tmp, true);
filterMatrix.convertTo(filterMatrix, CV_32FC1);
N = filterMatrix.cols/2;
if ( !src.data )
{
printf("No image data \n");
return -1;
}
dest = applyFilter(src,filterMatrix);
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", dest);
imwrite(argv[3], dest);
waitKey(0);
return 0;
}
Mat applyFilter(Mat image, Mat filter){
//Apply Filter to the image
Mat filterApplied(image.rows, image.cols, CV_32FC3, Scalar(0,0,0));
for(int x = N ; x< image.cols-N;x++){
for (int y = N; y < image.rows-N; y++)
{
float blue = 0;
float green = 0;
float red = 0;
for (int filterx = -N; filterx < filter.cols-N; filterx++)
{
for (int filtery = -N; filtery < filter.rows-N; filtery++)
{
Vec3b imagePixel = image.at<Vec3b>(y+filtery,x+filterx);
float filterConstant = filter.at<float>(filtery+N,filterx+N);
blue += imagePixel[0]*filterConstant;
green += imagePixel[1]*filterConstant;
red += imagePixel[2]*filterConstant;
}
}
Vec3f newPixel(blue,green,red);
filterApplied.at<Vec3f>(y,x) = newPixel;
}
}
// Find Max and Min Values
float min = 1000000000;
float max = -100000000;
Mat cropedImage = filterApplied(Rect(N,N,filterApplied.rows-2*N,filterApplied.cols-2*N));
for (int i = 0; i < cropedImage.rows; ++i)
{
for (int j = 0; j < cropedImage.cols; ++j)
{
if(cropedImage.at<Vec3f>(i,j)[0]>max)
max = cropedImage.at<Vec3f>(i,j)[0];
if(cropedImage.at<Vec3f>(i,j)[1]>max)
max = cropedImage.at<Vec3f>(i,j)[1];
if(cropedImage.at<Vec3f>(i,j)[2]>max)
max = cropedImage.at<Vec3f>(i,j)[2];
if (cropedImage.at<Vec3f>(i,j)[0]<min)
min = cropedImage.at<Vec3f>(i,j)[0];
if (cropedImage.at<Vec3f>(i,j)[1]<min)
min = cropedImage.at<Vec3f>(i,j)[1];
if (cropedImage.at<Vec3f>(i,j)[2]<min)
min = cropedImage.at<Vec3f>(i,j)[2];
}
}
//Change the ratio of the range
Mat final = Mat::zeros( cropedImage.rows, cropedImage.cols, CV_8UC3 );
float numerator = max-min;
for (int i = 0; i< cropedImage.rows; i++) {
for (int j = 0; j< cropedImage.cols; j++) {
float blue = cropedImage.at<Vec3f>(i,j)[0];
float green = cropedImage.at<Vec3f>(i,j)[1];
float red = cropedImage.at<Vec3f>(i,j)[2];
final.at<Vec3b>(i,j)[0] = (((blue-min)*255.0f)/numerator);
final.at<Vec3b>(i,j)[1] = (((green-min)*255.0f)/numerator);
final.at<Vec3b>(i,j)[2] = (((red-min)*255.0f)/numerator);
}
}
return final;
}