|
Toshihiro Shimizu |
890ddd |
#include <iostream></iostream>
|
|
Toshihiro Shimizu |
890ddd |
#include <vector></vector>
|
|
Shinya Kitaoka |
120a6e |
#include <stdexcept> // std::domain_error</stdexcept>
|
|
Shinya Kitaoka |
120a6e |
#include <limits> // std::numeric_limits</limits>
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_color_rgb_hsv.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_math_random.h"
|
|
Shinya Kitaoka |
120a6e |
#include "igs_ifx_common.h" // igs::image::rgba
|
|
Toshihiro Shimizu |
890ddd |
#include "igs_hsv_noise_in_camera.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
namespace igs {
|
|
Shinya Kitaoka |
120a6e |
namespace hsv_noise_in_camera {
|
|
Toshihiro Shimizu |
890ddd |
/* バッファをまとめて確保(return or throwで自動解放) */
|
|
Shinya Kitaoka |
120a6e |
class noise_reference {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
noise_reference(const int ww, const int hh, const double hue_range,
|
|
Shinya Kitaoka |
120a6e |
const double sat_range, const double val_range,
|
|
Shinya Kitaoka |
120a6e |
const double alp_range, const unsigned long random_seed,
|
|
Shinya Kitaoka |
120a6e |
const double near_blur
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int camera_x, const int camera_y, const int camera_w,
|
|
Shinya Kitaoka |
120a6e |
const int camera_h)
|
|
Shinya Kitaoka |
120a6e |
: w_(ww), h_(hh), nblur_(near_blur) {
|
|
Shinya Kitaoka |
120a6e |
if (0 == ww) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0 == hh) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 枚数ゼロ(rangeが全部ゼロ)なのでノイズかけない */
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 == hue_range) && (0.0 == sat_range) && (0.0 == val_range) &&
|
|
Shinya Kitaoka |
120a6e |
(0.0 == alp_range)) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* memory確保 */
|
|
Shinya Kitaoka |
120a6e |
const int sz = ww * hh;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
this->val_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_.resize(sz);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* タネを設定 */
|
|
Shinya Kitaoka |
120a6e |
igs::math::random hue_rand, sat_rand, val_rand, alp_rand;
|
|
Shinya Kitaoka |
120a6e |
unsigned long step = 0;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
hue_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
sat_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
val_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
alp_rand.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
igs::math::random hue_rand_ext, sat_rand_ext, val_rand_ext, alp_rand_ext;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
hue_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
sat_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
val_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
alp_rand_ext.seed(random_seed + step++);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* ノイズを書く */
|
|
Shinya Kitaoka |
120a6e |
/* -???_range/2〜???_range/2 */
|
|
Shinya Kitaoka |
120a6e |
int x1 = camera_x;
|
|
Shinya Kitaoka |
120a6e |
int y1 = camera_y;
|
|
Shinya Kitaoka |
120a6e |
int x2 = camera_x + camera_w - 1;
|
|
Shinya Kitaoka |
120a6e |
int y2 = camera_y + camera_h - 1;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* マージンあるかないかでノイズパターンが変わってしまうため、
|
|
Shinya Kitaoka |
120a6e |
near_blur用に広げてはいけない */
|
|
Shinya Kitaoka |
120a6e |
// if (0.0 != near_blur) { x1-=1; x2+=1; y1-=1; y2+=1; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_[pos] = hue_range * (hue_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_[pos] = hue_range * (hue_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_[pos] = sat_range * (sat_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_[pos] = sat_range * (sat_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->val_array_[pos] = val_range * (val_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->val_array_[pos] = val_range * (val_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_range) {
|
|
Shinya Kitaoka |
120a6e |
int pos = 0;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++pos) {
|
|
Shinya Kitaoka |
120a6e |
if ((xx < x1) || (x2 < xx) || (yy < y1) || (y2 < yy)) {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_[pos] = alp_range * (alp_rand_ext.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_[pos] = alp_range * (alp_rand.next_d() - 0.5);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double hue_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->hue_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double sat_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->sat_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double val_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->val_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double alp_value(const int xx, const int yy) {
|
|
Shinya Kitaoka |
120a6e |
return this->noise_value_(this->alp_array_, this->w_, this->h_, xx, yy,
|
|
Shinya Kitaoka |
120a6e |
this->nblur_);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
void clear() {
|
|
Shinya Kitaoka |
120a6e |
this->alp_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
this->val_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
this->sat_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
this->hue_array_.clear();
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
~noise_reference() { this->clear(); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Shinya Kitaoka |
120a6e |
const int w_;
|
|
Shinya Kitaoka |
120a6e |
const int h_;
|
|
Shinya Kitaoka |
120a6e |
const double nblur_;
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> hue_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> sat_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> val_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
std::vector<double> alp_array_;</double>
|
|
Shinya Kitaoka |
120a6e |
void accum_in_(const double *noise_array, const int ww, const int hh,
|
|
Shinya Kitaoka |
120a6e |
const int xx, const int yy, double &accum_val,
|
|
Shinya Kitaoka |
120a6e |
int &accum_count) {
|
|
Shinya Kitaoka |
120a6e |
if ((0 <= xx) && (xx < ww) && (0 <= yy) && (yy < hh)) {
|
|
Shinya Kitaoka |
120a6e |
accum_val += noise_array[yy * ww + xx];
|
|
Shinya Kitaoka |
120a6e |
++accum_count;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double noise_value_(const std::vector<double> &noise_vector, const int ww,</double>
|
|
Shinya Kitaoka |
120a6e |
const int hh, const int xx, const int yy,
|
|
Shinya Kitaoka |
120a6e |
const double near_blur) {
|
|
Shinya Kitaoka |
120a6e |
if (noise_vector.size() <= 0) {
|
|
Shinya Kitaoka |
120a6e |
return 0.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
const double *noise_array = &noise_vector.at(0);
|
|
Shinya Kitaoka |
120a6e |
if (0.0 == near_blur) {
|
|
Shinya Kitaoka |
120a6e |
return noise_array[yy * ww + xx];
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
double accum_val = 0.0;
|
|
Shinya Kitaoka |
120a6e |
int accum_count = 0;
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx - 1, yy - 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx, yy - 1, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx + 1, yy - 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx - 1, yy, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx + 1, yy, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx - 1, yy + 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx, yy + 1, accum_val, accum_count);
|
|
Shinya Kitaoka |
120a6e |
this->accum_in_(noise_array, ww, hh, xx + 1, yy + 1, accum_val,
|
|
Shinya Kitaoka |
120a6e |
accum_count);
|
|
Shinya Kitaoka |
120a6e |
if (accum_count <= 0) {
|
|
Shinya Kitaoka |
120a6e |
return noise_array[yy * ww + xx];
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
accum_val /= static_cast<double>(accum_count);</double>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 中心Pixelと廻り(1Pixel幅)の平均値とのバランスを返す */
|
|
Shinya Kitaoka |
120a6e |
return (near_blur * accum_val) +
|
|
Shinya Kitaoka |
120a6e |
((1.0 - near_blur) * noise_array[yy * ww + xx]);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* copy constructorを無効化 */
|
|
Shinya Kitaoka |
120a6e |
noise_reference(const noise_reference &);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 代入演算子を無効化 */
|
|
Shinya Kitaoka |
120a6e |
noise_reference &operator=(const noise_reference &);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Shinya Kitaoka |
120a6e |
namespace igs {
|
|
Shinya Kitaoka |
120a6e |
namespace hsv_noise_in_camera {
|
|
Toshihiro Shimizu |
890ddd |
/* 端値を適度に調整する */
|
|
Shinya Kitaoka |
120a6e |
class control_term_within_limits {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits(const double effective_low = 0.0,
|
|
Shinya Kitaoka |
120a6e |
const double effective_high = 0.0,
|
|
Shinya Kitaoka |
120a6e |
const double center = 0.5, const int type = 0,
|
|
Shinya Kitaoka |
120a6e |
const double noise_range = 0.0)
|
|
Shinya Kitaoka |
120a6e |
: effective_low_(effective_low)
|
|
Shinya Kitaoka |
120a6e |
, effective_high_(effective_high)
|
|
Shinya Kitaoka |
120a6e |
, center_(center)
|
|
Shinya Kitaoka |
120a6e |
, type_(static_cast<term_type_>(type))</term_type_>
|
|
Shinya Kitaoka |
120a6e |
, noise_range_(noise_range) {}
|
|
Shinya Kitaoka |
120a6e |
void exec(const double current_value /* 0...1 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
double &noise /* -noise_range/2...noise_range/2 */
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
double &shift_value) {
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 < this->effective_low_) && (current_value < this->center_)) {
|
|
Shinya Kitaoka |
120a6e |
const double cen = this->center_;
|
|
Shinya Kitaoka |
120a6e |
const double val = current_value;
|
|
Shinya Kitaoka |
120a6e |
const double ran = this->noise_range_;
|
|
Shinya Kitaoka |
120a6e |
const double eff = this->effective_low_;
|
|
Shinya Kitaoka |
120a6e |
switch (this->type_) {
|
|
Shinya Kitaoka |
120a6e |
case shift_all_:
|
|
Shinya Kitaoka |
120a6e |
shift_value = ((cen - val) / cen) * (ran / 2.0) * eff;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case shift_term_:
|
|
Shinya Kitaoka |
120a6e |
if (val < ran) {
|
|
Shinya Kitaoka |
120a6e |
shift_value = (((cen < ran) ? cen : ran) - val) / 2.0 * eff;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_all_: {
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (cen - val) / cen * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_term_:
|
|
Shinya Kitaoka |
120a6e |
if (val < (ran / 2.0)) {
|
|
Shinya Kitaoka |
120a6e |
const double stop = (cen < (ran / 2.0)) ? cen : ran / 2.0;
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (stop - val) / stop * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 < this->effective_high_) && (this->center_ < current_value)) {
|
|
Shinya Kitaoka |
120a6e |
const double cen = this->center_;
|
|
Shinya Kitaoka |
120a6e |
const double val = current_value;
|
|
Shinya Kitaoka |
120a6e |
const double ran = this->noise_range_;
|
|
Shinya Kitaoka |
120a6e |
const double eff = this->effective_high_;
|
|
Shinya Kitaoka |
120a6e |
switch (this->type_) {
|
|
Shinya Kitaoka |
120a6e |
case shift_all_:
|
|
Shinya Kitaoka |
120a6e |
shift_value = ((cen - val) / (1.0 - cen)) * (ran / 2.0) * eff;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case shift_term_:
|
|
Shinya Kitaoka |
120a6e |
if ((1.0 - ran) < val) {
|
|
Shinya Kitaoka |
120a6e |
const double ira = 1.0 - ran;
|
|
Shinya Kitaoka |
120a6e |
shift_value = (((cen < ira) ? ira : cen) - val) / 2.0 * eff;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_all_: {
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (val - cen) / (1.0 - cen) * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} break;
|
|
Shinya Kitaoka |
120a6e |
case decrease_term_:
|
|
Shinya Kitaoka |
120a6e |
if ((1.0 - (ran / 2.0)) < val) {
|
|
Shinya Kitaoka |
120a6e |
const double rpos = 1.0 - (ran / 2.0);
|
|
Shinya Kitaoka |
120a6e |
const double stop = (cen < rpos) ? rpos : cen;
|
|
Shinya Kitaoka |
120a6e |
const double tmp = (val - stop) / (1.0 - stop) * eff;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 < tmp) {
|
|
Shinya Kitaoka |
120a6e |
noise *= 1.0 - tmp;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double noise_range(void) const { return this->noise_range_; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Shinya Kitaoka |
120a6e |
/* low,high両方ゼロ =exec()内処理せず =端値はカット =default */
|
|
Shinya Kitaoka |
120a6e |
const double effective_low_;
|
|
Shinya Kitaoka |
120a6e |
const double effective_high_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
const double center_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
enum term_type_ { /* 端値の調整方法 */
|
|
Shinya Kitaoka |
120a6e |
shift_all_ = 0, /* 0.全体的にノイズ位置がずれる */
|
|
Shinya Kitaoka |
120a6e |
shift_term_, /* 1.端のみでノイズ位置がずれる */
|
|
Shinya Kitaoka |
120a6e |
decrease_all_, /* 2.全体的にノイズ幅が減る */
|
|
Shinya Kitaoka |
120a6e |
decrease_term_, /* 3.端のみでノイズ幅が減る */
|
|
Shinya Kitaoka |
120a6e |
};
|
|
Shinya Kitaoka |
120a6e |
const term_type_ type_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
const double noise_range_;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* copy constructorを無効化 */
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits(const control_term_within_limits &);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 代入演算子を無効化 */
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &operator=(const control_term_within_limits &);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Shinya Kitaoka |
120a6e |
namespace igs {
|
|
Shinya Kitaoka |
120a6e |
namespace hsv_noise_in_camera {
|
|
Toshihiro Shimizu |
890ddd |
/* RGB値にノイズをのせる */
|
|
Shinya Kitaoka |
120a6e |
void pixel_rgb(const double red_in, const double gre_in, const double blu_in,
|
|
Shinya Kitaoka |
120a6e |
const double alp_in, const double hue_noise,
|
|
Shinya Kitaoka |
120a6e |
const double sat_noise, const double val_noise,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &sat_term,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &val_term, double &red_out,
|
|
Shinya Kitaoka |
120a6e |
double &gre_out, double &blu_out);
|
|
Toshihiro Shimizu |
890ddd |
/* Alpha値にノイズをのせる */
|
|
Shinya Kitaoka |
120a6e |
void pixel_a(const double alp_in, const double alp_noise,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &alp_term, double &alp_out);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
void igs::hsv_noise_in_camera::pixel_rgb(
|
|
Shinya Kitaoka |
120a6e |
const double red_in, const double gre_in, const double blu_in,
|
|
Shinya Kitaoka |
120a6e |
const double alp_in, const double hue_noise, const double sat_noise,
|
|
Shinya Kitaoka |
120a6e |
const double val_noise, control_term_within_limits &sat_term,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &val_term, double &red_out, double &gre_out,
|
|
Shinya Kitaoka |
120a6e |
double &blu_out) {
|
|
Shinya Kitaoka |
120a6e |
if (0.0 == alp_in) {
|
|
Shinya Kitaoka |
120a6e |
red_out = red_in;
|
|
Shinya Kitaoka |
120a6e |
gre_out = gre_in;
|
|
Shinya Kitaoka |
120a6e |
blu_out = blu_in;
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
double hue, sat, val;
|
|
Shinya Kitaoka |
120a6e |
igs::color::rgb_to_hsv(red_in, gre_in, blu_in, hue, sat, val);
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != hue_noise) {
|
|
Shinya Kitaoka |
120a6e |
hue += 360.0 * hue_noise * alp_in;
|
|
Shinya Kitaoka |
120a6e |
while (hue < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
hue += 360.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
while (360.0 <= hue) {
|
|
Shinya Kitaoka |
120a6e |
hue -= 360.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != sat_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0;
|
|
Shinya Kitaoka |
120a6e |
double satnoise = sat_noise;
|
|
Shinya Kitaoka |
120a6e |
sat_term.exec(sat, satnoise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
sat += shift_value * alp_in;
|
|
Shinya Kitaoka |
120a6e |
sat += satnoise * alp_in;
|
|
Shinya Kitaoka |
120a6e |
if (sat < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
sat = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < sat) {
|
|
Shinya Kitaoka |
120a6e |
sat = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
// if( 0.0 == sat ) hue = -1.0; // hsv_to_rgb(-)
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0;
|
|
Shinya Kitaoka |
120a6e |
double valnoise = val_noise;
|
|
Shinya Kitaoka |
120a6e |
val_term.exec(val, valnoise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
val += shift_value * alp_in;
|
|
Shinya Kitaoka |
120a6e |
val += valnoise * alp_in;
|
|
Shinya Kitaoka |
120a6e |
if (val < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
val = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < val) {
|
|
Shinya Kitaoka |
120a6e |
val = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
igs::color::hsv_to_rgb(hue, sat, val, red_out, gre_out, blu_out);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Shinya Kitaoka |
120a6e |
void igs::hsv_noise_in_camera::pixel_a(const double alp_in,
|
|
Shinya Kitaoka |
120a6e |
const double alp_noise,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &alp_term,
|
|
Shinya Kitaoka |
120a6e |
double &alp_out) {
|
|
Shinya Kitaoka |
120a6e |
// if (0.0 == alp_in) { return; }
|
|
Shinya Kitaoka |
120a6e |
double alpin = alp_in;
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0.0;
|
|
Shinya Kitaoka |
120a6e |
double alpnoise = alp_noise;
|
|
Shinya Kitaoka |
120a6e |
alp_term.exec(alpin, alpnoise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
const double mask = alpin;
|
|
Shinya Kitaoka |
120a6e |
alpin += shift_value * mask;
|
|
Shinya Kitaoka |
120a6e |
alpin += alpnoise * mask;
|
|
Shinya Kitaoka |
120a6e |
if (alpin < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
alpin = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < alpin) {
|
|
Shinya Kitaoka |
120a6e |
alpin = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
alp_out = alpin;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Shinya Kitaoka |
120a6e |
namespace igs {
|
|
Shinya Kitaoka |
120a6e |
namespace hsv_noise_in_camera {
|
|
Toshihiro Shimizu |
890ddd |
/* raster画像にノイズをのせるtemplate */
|
|
Toshihiro Shimizu |
890ddd |
template <class t=""></class>
|
|
Shinya Kitaoka |
120a6e |
void change_template_(T *image_array, const int ww, const int hh, const int ch,
|
|
Shinya Kitaoka |
120a6e |
noise_reference &noise, const double hue_range,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &sat_term,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &val_term,
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits &alp_term) {
|
|
Shinya Kitaoka |
120a6e |
const double div_val = static_cast<double>(std::numeric_limits<t>::max());</t></double>
|
|
Shinya Kitaoka |
120a6e |
const double mul_val =
|
|
Shinya Kitaoka |
120a6e |
static_cast<double>(std::numeric_limits<t>::max()) + 0.999999;</t></double>
|
|
Shinya Kitaoka |
120a6e |
double rr, gg, bb, aa;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (igs::image::rgba::siz == ch) {
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgba;
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx) {
|
|
Shinya Kitaoka |
120a6e |
if (((0.0 != hue_range) || (0.0 != val_term.noise_range()) ||
|
|
Shinya Kitaoka |
120a6e |
(0.0 !=
|
|
Shinya Kitaoka |
120a6e |
sat_term.noise_range())) /* ノイズがhsvのどれか一つはある */
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
pixel_rgb(static_cast<double>(image_array[red]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
static_cast<double>(image_array[gre]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
static_cast<double>(image_array[blu]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
static_cast<double>(image_array[alp]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
noise.hue_value(xx, yy), noise.sat_value(xx, yy),
|
|
Shinya Kitaoka |
120a6e |
noise.val_value(xx, yy), sat_term, val_term, rr, gg, bb);
|
|
Shinya Kitaoka |
120a6e |
image_array[red] = static_cast<t>(rr * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
image_array[gre] = static_cast<t>(gg * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
image_array[blu] = static_cast<t>(bb * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != alp_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
pixel_a(static_cast<double>(image_array[alp]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
noise.alp_value(xx, yy), alp_term, aa);
|
|
Shinya Kitaoka |
120a6e |
image_array[alp] = static_cast<t>(aa * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
image_array += ch;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} else if (igs::image::rgb::siz == ch) {
|
|
Shinya Kitaoka |
120a6e |
using namespace igs::image::rgb;
|
|
Shinya Kitaoka |
120a6e |
if (((0.0 != hue_range) || (0.0 != sat_term.noise_range()) ||
|
|
Shinya Kitaoka |
120a6e |
(0.0 != val_term.noise_range())) /* ノイズがhsvのどれか一つはある */
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, image_array += ch) {
|
|
Shinya Kitaoka |
120a6e |
pixel_rgb(static_cast<double>(image_array[red]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
static_cast<double>(image_array[gre]) / div_val,</double>
|
|
Shinya Kitaoka |
120a6e |
static_cast<double>(image_array[blu]) / div_val, 1.0,</double>
|
|
Shinya Kitaoka |
120a6e |
noise.hue_value(xx, yy), noise.sat_value(xx, yy),
|
|
Shinya Kitaoka |
120a6e |
noise.val_value(xx, yy), sat_term, val_term, rr, gg, bb);
|
|
Shinya Kitaoka |
120a6e |
image_array[red] = static_cast<t>(rr * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
image_array[gre] = static_cast<t>(gg * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
image_array[blu] = static_cast<t>(bb * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
} else if (1 == ch) { /* grayscale */
|
|
Shinya Kitaoka |
120a6e |
if (0.0 != val_term.noise_range()) {
|
|
Shinya Kitaoka |
120a6e |
for (int yy = 0; yy < hh; ++yy) {
|
|
Shinya Kitaoka |
120a6e |
for (int xx = 0; xx < ww; ++xx, ++image_array) {
|
|
Shinya Kitaoka |
120a6e |
double val = static_cast<double>(image_array[0]) / div_val;</double>
|
|
Shinya Kitaoka |
120a6e |
double shift_value = 0;
|
|
Shinya Kitaoka |
120a6e |
double val_noise = noise.val_value(xx, yy);
|
|
Shinya Kitaoka |
120a6e |
val_term.exec(val, val_noise, shift_value);
|
|
Shinya Kitaoka |
120a6e |
val += shift_value;
|
|
Shinya Kitaoka |
120a6e |
val += val_noise;
|
|
Shinya Kitaoka |
120a6e |
if (val < 0.0) {
|
|
Shinya Kitaoka |
120a6e |
val = 0.0;
|
|
Shinya Kitaoka |
120a6e |
} else if (1.0 < val) {
|
|
Shinya Kitaoka |
120a6e |
val = 1.0;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
image_array[0] = static_cast<t>(val * mul_val);</t>
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void igs::hsv_noise_in_camera::change(
|
|
Shinya Kitaoka |
120a6e |
void *image_array
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int height, const int width, const int channels, const int bits
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const int camera_x, const int camera_y, const int camera_w,
|
|
Shinya Kitaoka |
120a6e |
const int camera_h
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double hue_range, const double sat_range, const double val_range,
|
|
Shinya Kitaoka |
120a6e |
const double alp_range, const unsigned long random_seed,
|
|
Shinya Kitaoka |
120a6e |
const double near_blur
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
,
|
|
Shinya Kitaoka |
120a6e |
const double sat_effective, const double sat_center, const int sat_type,
|
|
Shinya Kitaoka |
120a6e |
const double val_effective, const double val_center, const int val_type,
|
|
Shinya Kitaoka |
120a6e |
const double alp_effective, const double alp_center, const int alp_type) {
|
|
Shinya Kitaoka |
120a6e |
if ((0.0 == hue_range) && (0.0 == sat_range) && (0.0 == val_range) &&
|
|
Shinya Kitaoka |
120a6e |
(0.0 == alp_range)) {
|
|
Shinya Kitaoka |
120a6e |
return;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if ((igs::image::rgba::siz != channels) &&
|
|
Shinya Kitaoka |
120a6e |
(igs::image::rgb::siz != channels) && (1 != channels) /* grayscale */
|
|
Shinya Kitaoka |
120a6e |
) {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad channels,Not rgba/rgb/grayscale");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* ノイズ参照画像を作成する */
|
|
Shinya Kitaoka |
120a6e |
noise_reference noise(width, height, hue_range, sat_range, val_range,
|
|
Shinya Kitaoka |
120a6e |
alp_range, random_seed, near_blur, camera_x, camera_y,
|
|
Shinya Kitaoka |
120a6e |
camera_w, camera_h);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* 端値を適度に調整する設定 */
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits sat_term(sat_effective, sat_effective, sat_center,
|
|
Shinya Kitaoka |
120a6e |
sat_type, sat_range);
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits val_term(val_effective, val_effective, val_center,
|
|
Shinya Kitaoka |
120a6e |
val_type, val_range);
|
|
Shinya Kitaoka |
120a6e |
control_term_within_limits alp_term(alp_effective, alp_effective, alp_center,
|
|
Shinya Kitaoka |
120a6e |
alp_type, alp_range);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
/* rgb(a)画像にhsv(a)でドットノイズを加える */
|
|
Shinya Kitaoka |
120a6e |
if (std::numeric_limits<unsigned char="">::digits == bits) {</unsigned>
|
|
Shinya Kitaoka |
120a6e |
change_template_(static_cast<unsigned *="" char="">(image_array), width, height,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
channels, noise, hue_range, sat_term, val_term, alp_term);
|
|
Shinya Kitaoka |
120a6e |
noise.clear(); /* ノイズ画像メモリ解放 */
|
|
Shinya Kitaoka |
120a6e |
} else if (std::numeric_limits<unsigned short="">::digits == bits) {</unsigned>
|
|
Shinya Kitaoka |
120a6e |
change_template_(static_cast<unsigned *="" short="">(image_array), width, height,</unsigned>
|
|
Shinya Kitaoka |
120a6e |
channels, noise, hue_range, sat_term, val_term, alp_term);
|
|
Shinya Kitaoka |
120a6e |
noise.clear(); /* ノイズ画像メモリ解放 */
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
throw std::domain_error("Bad bits,Not uchar/ushort");
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|