42 #include <visp3/core/vpImage.h>
46 template <
typename PixelType> PixelType checkPixelAccess(
unsigned int height,
unsigned int width,
double v,
double u)
49 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
50 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
51 I[i][j] =
static_cast<PixelType
>(i * I.
getWidth() + j);
58 template <>
vpRGBa checkPixelAccess(
unsigned int height,
unsigned int width,
double v,
double u)
61 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
62 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
65 static_cast<unsigned char>(i * I.
getWidth() + j));
72 double randomDouble(
double a,
double b)
74 double random = (
static_cast<double>(rand())) /
static_cast<double>(RAND_MAX);
76 double r = random * diff;
80 unsigned char randomPixelValue()
82 const int min = 0, max = 255;
83 return static_cast<unsigned char>((rand() % (max - min + 1) + min));
95 unsigned int iround =
static_cast<unsigned int>(floor(i));
96 unsigned int jround =
static_cast<unsigned int>(floor(j));
98 double rratio = i -
static_cast<double>(iround);
99 double cratio = j -
static_cast<double>(jround);
101 double rfrac = 1.0 - rratio;
102 double cfrac = 1.0 - cratio;
104 unsigned int iround_1 = std::min<unsigned int>(I.
getHeight() - 1, iround + 1);
105 unsigned int jround_1 = std::min<unsigned int>(I.
getWidth() - 1, jround + 1);
108 (
static_cast<double>(I[iround][jround]) * rfrac +
static_cast<double>(I[iround_1][jround]) * rratio) * cfrac +
109 (
static_cast<double>(I[iround][jround_1]) * rfrac +
static_cast<double>(I[iround_1][jround_1]) * rratio) * cratio;
111 return static_cast<PixelType
>(roundValue ?
vpMath::round(value) : value);
121 std::cout <<
"checkPixelAccess<unsigned char>(3, 4, 2, 3): "
122 <<
static_cast<unsigned int>(checkPixelAccess<unsigned char>(3, 4, 2, 3)) << std::endl;
124 std::cout <<
"checkPixelAccess<unsigned char>(3, 4, -2, -3): "
125 <<
static_cast<unsigned int>(checkPixelAccess<unsigned char>(3, 4, -2, -3)) << std::endl;
126 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
133 std::cout <<
"checkPixelAccess<unsigned char>(3, 4, 3, 4): "
134 <<
static_cast<unsigned int>(checkPixelAccess<unsigned char>(3, 4, 3, 4)) << std::endl;
135 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
143 std::cout <<
"checkPixelAccess<vpRGBa>(3, 4, 2, 3): " << checkPixelAccess<vpRGBa>(3, 4, 2, 3) << std::endl;
145 std::cout <<
"checkPixelAccess<vpRGBa>(3, 4, -2, -3): " << checkPixelAccess<vpRGBa>(3, 4, -2, -3) << std::endl;
146 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
153 std::cout <<
"checkPixelAccess<vpRGBa>(3, 4, 3, 4): " << checkPixelAccess<vpRGBa>(3, 4, 3, 4) << std::endl;
154 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
162 std::cout <<
"checkPixelAccess<int>(3, 4, 2, 3): " << checkPixelAccess<int>(3, 4, 2, 3) << std::endl;
164 std::cout <<
"checkPixelAccess<int>(3, 4, -2, -3): " << checkPixelAccess<int>(3, 4, -2, -3) << std::endl;
165 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
172 std::cout <<
"checkPixelAccess<int>(3, 4, 3, 4): " << checkPixelAccess<int>(3, 4, 3, 4) << std::endl;
173 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
181 std::cout <<
"checkPixelAccess<double>(3, 4, 2, 3): " << checkPixelAccess<double>(3, 4, 2, 3) << std::endl;
183 std::cout <<
"checkPixelAccess<double>(3, 4, -2, -3): " << checkPixelAccess<double>(3, 4, -2, -3) << std::endl;
184 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
191 std::cout <<
"checkPixelAccess<double>(3, 4, 3, 4): " << checkPixelAccess<double>(3, 4, 3, 4) << std::endl;
192 std::cerr <<
"Out of image access exception should have been thrown" << std::endl;
205 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
206 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
207 I[i][j] = randomPixelValue();
211 double diff_round = 0.0, diff = 0.0;
213 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
214 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
215 double idx1 = randomDouble(0, I.
getHeight() - 1);
216 double idx2 = randomDouble(0, I.
getWidth() - 1);
217 unsigned char val1 = I.
getValue(idx1, idx2);
218 unsigned char val2 = getValue<unsigned char>(I, idx1, idx2,
true);
219 unsigned char val3 = getValue<unsigned char>(I, idx1, idx2,
false);
221 diff_round += std::fabs((
double)val1 - (
double)val2);
222 diff += std::fabs((
double)val1 - (
double)val3);
226 double meanDiffRound = diff_round / I.
getSize();
227 double meanDiff = diff / I.
getSize();
228 std::cout <<
"diff_round: " << diff_round <<
" ; meanDiffRound: " << meanDiffRound << std::endl;
229 std::cout <<
"diff: " << diff <<
" ; meanDiff: " << meanDiff << std::endl;
230 const double maxInterpolationErrorDiff = 1.0;
231 if (std::fabs(meanDiffRound) > maxInterpolationErrorDiff) {
232 std::cerr <<
"Too much pixel difference between fixed-point vpImage::getValue(double, double) and old method."
241 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
242 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
243 I[i][j] = randomPixelValue();
247 std::vector<std::pair<double, double> > indexes;
248 for (
int cpt = 0; cpt < 1000000; cpt++) {
249 double idx1 = randomDouble(0, I.
getHeight() - 1);
250 double idx2 = randomDouble(0, I.
getWidth() - 1);
251 indexes.push_back(std::pair<double, double>(idx1, idx2));
256 for (
size_t cpt = 0; cpt < indexes.size(); cpt++) {
257 double idx1 = indexes[cpt].first;
258 double idx2 = indexes[cpt].second;
262 std::cout <<
"\nFixed-point vpImage::getValue(double, double), sum1: " << sum1 <<
" in " << t_optim <<
" ms"
267 for (
size_t cpt = 0; cpt < indexes.size(); cpt++) {
268 double idx1 = indexes[cpt].first;
269 double idx2 = indexes[cpt].second;
270 sum2 +=
getValue(I, idx1, idx2,
true);
273 std::cout <<
"Old method, sum2: " << sum2 <<
" in " << t_old <<
" ms" << std::endl;
274 std::cout <<
"Speed-up: " << t_old / t_optim <<
"X" << std::endl;
280 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
281 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
282 I[i][j] = randomPixelValue();
286 std::vector<std::pair<double, double> > indexes;
287 for (
int cpt = 0; cpt < 1000000; cpt++) {
288 double idx1 = randomDouble(0, I.
getHeight() - 1);
289 double idx2 = randomDouble(0, I.
getWidth() - 1);
290 indexes.push_back(std::pair<double, double>(idx1, idx2));
295 for (
size_t cpt = 0; cpt < indexes.size(); cpt++) {
296 double idx1 = indexes[cpt].first;
297 double idx2 = indexes[cpt].second;
301 std::cout <<
"\nFixed-point vpImage::getValue(double, double), sum1: " << sum1 <<
" in " << t_optim <<
" ms"
306 for (
size_t cpt = 0; cpt < indexes.size(); cpt++) {
307 double idx1 = indexes[cpt].first;
308 double idx2 = indexes[cpt].second;
309 sum2 +=
getValue(I, idx1, idx2,
false);
312 std::cout <<
"Old method (without vpMath::round()), sum2: " << sum2 <<
" in " << t_old <<
" ms" << std::endl;
313 std::cout <<
"Speed-up: " << t_old / t_optim <<
"X" << std::endl;
319 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
320 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
321 I[i][j] = randomPixelValue();
326 for (
unsigned int i = 0; i < I_copy.getHeight(); i++) {
327 double y =
static_cast<double>(i);
329 for (
unsigned int j = 0; j < I_copy.getWidth(); j++) {
330 double x =
static_cast<double>(j);
336 bool same = (I == I_copy);
337 std::cout <<
"\nCheck that getValue returns correct results for integer coordinates\n(I == I_copy)? " << same
340 std::cerr <<
"Issue with vpImage::getValue(double, double)!" << std::endl;
error that can be emitted by ViSP classes.
@ notInitializedError
Image not initialized.
@ notInTheImage
Pixel not in the image.
Definition of the vpImage class member functions.
unsigned int getWidth() const
Type getValue(unsigned int i, unsigned int j) const
double getValue(double i, double j) const
unsigned int getSize() const
unsigned int getHeight() const
static int round(double x)
VISP_EXPORT double measureTimeMs()