|
@@ -1,4 +1,3 @@
|
|
|
-
|
|
|
#ifndef _IMAGE_HPP_
|
|
|
#define _IMAGE_HPP_
|
|
|
|
|
@@ -9,14 +8,12 @@
|
|
|
#include <string>
|
|
|
#include <vector>
|
|
|
|
|
|
-typedef unsigned char pixel_t;
|
|
|
-typedef std::vector<pixel_t> image_t;
|
|
|
+using pixel_t = unsigned char;
|
|
|
+using image_t = std::vector<pixel_t>;
|
|
|
|
|
|
// write image
|
|
|
-std::string writePgm(const std::string &filename,
|
|
|
- int width,
|
|
|
- int height,
|
|
|
- const image_t & data)
|
|
|
+std::string writePgm(const std::string &filename, int width, int height,
|
|
|
+ const image_t & data)
|
|
|
{
|
|
|
std::ofstream os(filename);
|
|
|
if (not os)
|
|
@@ -34,10 +31,8 @@ std::string writePgm(const std::string &filename,
|
|
|
}
|
|
|
|
|
|
// read image
|
|
|
-std::string readPgm(const std::string &filename,
|
|
|
- int & width,
|
|
|
- int & height,
|
|
|
- image_t & data)
|
|
|
+std::string readPgm(const std::string &filename, int & width, int & height,
|
|
|
+ image_t & data)
|
|
|
{
|
|
|
std::ifstream is(filename);
|
|
|
if (not is)
|
|
@@ -67,11 +62,13 @@ std::string readPgm(const std::string &filename,
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
-// Returns a blurred subimage of data1 (using the specified radius for the convolution kernel).
|
|
|
+// Returns a blurred subimage of data1
|
|
|
+// (using the specified radius for the convolution kernel).
|
|
|
// The size of data1 is width*height.
|
|
|
// The blurred region is, in data1, (x0, y0, x1, y1).
|
|
|
// Therefore the size of the subimage is (x1-x0)*(y1-y0)
|
|
|
-image_t blur(const image_t & data1, int width, int height, int radius, int x0, int y0, int x1, int y1)
|
|
|
+image_t blur(const image_t & data1, int width, int height, int radius, int x0,
|
|
|
+ int y0, int x1, int y1)
|
|
|
{
|
|
|
int width2 = x1-x0;
|
|
|
int height2 = y1-y0;
|
|
@@ -99,18 +96,20 @@ image_t blur(const image_t & data1, int width, int height, int radius, int x0, i
|
|
|
}
|
|
|
}
|
|
|
int pixel = n>0 ? s/n : data1[y*width + x];
|
|
|
- if (x==x0 or y==y0 or x==x1-1 or y==y1-1) pixel = 0; // draw border
|
|
|
+ if (x==x0 or y==y0 or x==x1-1 or y==y1-1) pixel = 0; // draw border
|
|
|
data2[(y-y0)*width2 + (x-x0)] = pixel;
|
|
|
}
|
|
|
}
|
|
|
return data2;
|
|
|
}
|
|
|
|
|
|
-image_t computeLaplacian(const image_t & data1, int width, int height, double scaling)
|
|
|
+image_t computeLaplacian(const image_t & data1, int width, int height,
|
|
|
+ double scaling)
|
|
|
{
|
|
|
assert(data1.size() >= unsigned(width*height));
|
|
|
// return the pixel value of (x,y) in [0,1]
|
|
|
- auto ind = [&data1,width](int x, int y) {return double(data1[y*width + x])/255.0;};
|
|
|
+ auto ind = [&data1,width](int x, int y)
|
|
|
+ {return double(data1[y*width + x])/255.0;};
|
|
|
// compute laplacian image
|
|
|
image_t data2(width*height);
|
|
|
for (int x=1; x<width-1; x++)
|
|
@@ -118,7 +117,8 @@ image_t computeLaplacian(const image_t & data1, int width, int height, double sc
|
|
|
for (int y=1; y<height-1; y++)
|
|
|
{
|
|
|
// compute laplacian value in [-4,4]
|
|
|
- double v = -4.0*ind(x,y) + ind(x,y-1) + ind(x,y+1) + ind(x-1,y) + ind(x+1,y);
|
|
|
+ double v = -4.0*ind(x,y)
|
|
|
+ + ind(x,y-1) + ind(x,y+1) + ind(x-1,y) + ind(x+1,y);
|
|
|
// scale the value in [0,255]
|
|
|
// and write this value in output image
|
|
|
int i255 = 255.0 * (scaling*v+4.0)/8.0;
|