Add RawImage class that handle images as byte array
This commit is contained in:
+182
@@ -0,0 +1,182 @@
|
||||
#ifndef RAWIMAGE_H
|
||||
#define RAWIMAGE_H
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <memory.h>
|
||||
|
||||
class RawImageAbs
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class RawImage : public RawImageAbs
|
||||
{
|
||||
uint32_t m_width,m_height;
|
||||
uint32_t m_width1,m_height1;
|
||||
std::vector<T> m_img;
|
||||
bool checkPixel(T c, uint32_t x, uint32_t y) const
|
||||
{
|
||||
T d = pixel(x, y);
|
||||
return c>=d;
|
||||
}
|
||||
public:
|
||||
class Peak
|
||||
{
|
||||
T m_v;
|
||||
uint32_t m_x,m_y;
|
||||
public:
|
||||
Peak() : m_v(0), m_x(0), m_y(0) {}
|
||||
Peak(T v, uint32_t x, uint32_t y) : m_v(v), m_x(x), m_y(y) {}
|
||||
T v() const { return m_v; }
|
||||
uint32_t x() const { return m_x; }
|
||||
uint32_t y() const { return m_y; }
|
||||
double distance(const Peak &d) const
|
||||
{
|
||||
uint32_t dx = m_x-d.m_x;
|
||||
uint32_t dy = m_y-d.m_y;
|
||||
return dx*dx + dy*dy;
|
||||
}
|
||||
bool operator <(const Peak &d) const
|
||||
{
|
||||
return m_v > d.m_v;
|
||||
}
|
||||
};
|
||||
|
||||
RawImage()
|
||||
{
|
||||
m_width = m_height = 0;
|
||||
}
|
||||
RawImage(int w, int h)
|
||||
{
|
||||
m_width = w;
|
||||
m_height = h;
|
||||
m_img.resize(w*h);
|
||||
}
|
||||
RawImage(int w, int h, std::vector<T> &img)
|
||||
{
|
||||
m_width = w;
|
||||
m_height = h;
|
||||
img.resize(w*h);
|
||||
img.clear();
|
||||
m_img = std::move(img);
|
||||
}
|
||||
T* data()
|
||||
{
|
||||
return m_img.data();
|
||||
}
|
||||
std::vector<T> dataArray() const
|
||||
{
|
||||
return m_img;
|
||||
}
|
||||
bool imageStats(T *mean, double *stdDev, T *median, T *min, T *max) const
|
||||
{
|
||||
if(m_img.size()==0)return false;
|
||||
|
||||
uint64_t sum = 0;
|
||||
uint64_t sqrSum = 0;
|
||||
T tMin = UINT64_MAX;
|
||||
T tMax = 0;
|
||||
|
||||
for(T i : m_img)
|
||||
{
|
||||
sum += i;
|
||||
sqrSum += i*i;
|
||||
tMin = tMin>i ? i : tMin;
|
||||
tMax = tMax<i ? i : tMax;
|
||||
}
|
||||
|
||||
if(mean)*mean = sum/m_img.size();
|
||||
if(min)*min = tMin;
|
||||
if(max)*max = tMax;
|
||||
if(stdDev)*stdDev = sqrt((sqrSum - (double)sum*sum/m_img.size()) / (double)(m_img.size()-1));
|
||||
|
||||
const int shift = sizeof(T)>2 ? sizeof(T)*8 - 16 : 0;
|
||||
|
||||
if(median)
|
||||
{
|
||||
uint32_t histogram[65536];
|
||||
memset(histogram, 0, sizeof(histogram));
|
||||
for(T i : m_img)
|
||||
histogram[i>>shift]++;
|
||||
size_t medianSum = 0;
|
||||
for(int i=0;i<65536;i++)
|
||||
{
|
||||
medianSum += histogram[i];
|
||||
if(medianSum>=m_img.size()/2)
|
||||
{
|
||||
*median = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
T pixel(uint32_t x, uint32_t y) const
|
||||
{
|
||||
if(x>=m_width)x=0;
|
||||
if(y>=m_height)y=0;
|
||||
return m_img[y*m_width+x];
|
||||
}
|
||||
void findPeaks(T background, double distance, std::vector<Peak> &peaks, T sigma) const
|
||||
{
|
||||
std::vector<Peak> tmpPeaks;
|
||||
const int r = 1;
|
||||
for(uint32_t i=r; i<m_height-r; i++)
|
||||
{
|
||||
for(uint32_t o=r; o<m_width-r; o++)
|
||||
{
|
||||
T c = pixel(o, i);
|
||||
if(c>background && checkPixel(c, o-r, i-r) && checkPixel(c, o, i-r) &&
|
||||
checkPixel(c, o+r, i-r) && checkPixel(c, o-r, i) && checkPixel(c, o+r, i)
|
||||
&& checkPixel(c, o-r, i+r) && checkPixel(c, o, i+r) && checkPixel(c, o+r, i+r))
|
||||
{
|
||||
tmpPeaks.append(Peak(c, o, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::qsort(tmpPeaks.begin(), tmpPeaks.end());
|
||||
tmpPeaks.resize(10000);
|
||||
uint32_t d = distance*distance;
|
||||
for(const Peak &p : tmpPeaks)
|
||||
{
|
||||
bool pass = true;
|
||||
for(const Peak &k : peaks)
|
||||
{
|
||||
if(p.distance(k) < d)
|
||||
{
|
||||
pass = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(pass)
|
||||
{
|
||||
peaks.append(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
void medianFilter()
|
||||
{
|
||||
std::vector<T> tmp;
|
||||
tmp.resize(m_width*m_height);
|
||||
size_t d=0;
|
||||
for(uint32_t y=0;y<m_height;y++)
|
||||
{
|
||||
for(uint32_t x=0;x<m_width;x++)
|
||||
{
|
||||
T array[9] = { pixel(x-1, y-1), pixel(x, y-1), pixel(x+1, y-1),
|
||||
pixel(x-1, y), pixel(x, y), pixel(x+1, y),
|
||||
pixel(x-1, y+1), pixel(x, y+1), pixel(x+1, y+1)};
|
||||
std::partial_sort(std::begin(array), array+5, std::end(array));
|
||||
tmp[d++] = array[4];
|
||||
}
|
||||
}
|
||||
m_img = std::move(tmp);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // RAWIMAGE_H
|
||||
Reference in New Issue
Block a user