Image Display¶
Here is a list of examples.
Tutorial 1¶
// ========================================================================== //
// This file is part of Sara, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2019 David Ok <david.ok8@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //
//! @example
#include <DO/Sara/Graphics.hpp>
using namespace std;
using namespace DO::Sara;
GRAPHICS_MAIN()
{
Window W = create_window(512, 512, "Bitmaps");
// Array of bytes
Color3ub cols[256*256];
// Some RGB function of (i,j)
for (int j = 0; j < 256; j++)
for (int i = 0; i < 256; i++)
cols[i+256*j]= Color3ub(i, 255-i, (j<128)?255:0);
// Draw this 256x256 RGB bitmap in (0,0)
put_color_image(0, 0, cols, 256, 256);
// An array of colors.
// Color3ub = 3D color vector where each channel has a value in [0,255].
Color3ub cols2[256*256];
for (int j = 0; j < 256; j++)
for (int i = 0; i < 256; i++)
cols2[i+256*j]=Color3ub(i, (2*j)%256, (i+j)%256); // RGB colors.
// Display the bitmap from the following top-left corner (0,256)
// TODO: rename this function.
put_color_image(Point2i(0, 256), cols2, 256, 256);
// A grayscale image.
unsigned char grey[256*256];
for (int j = 0; j < 256; ++j)
for (int i = 0; i < 256; ++i)
grey[i+256*j] = static_cast<unsigned char>(128+127*sin((i+j)/10.));
// Display the bitmap from the following top-left corner (0,256)
// TODO: rename this function.
put_grey_image(256 ,0, grey, 256, 256); // Draw at point (256,0);
click();
close_window(W);
return 0;
}
Tutorial 2¶
// ========================================================================== //
// This file is part of Sara, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2019 David Ok <david.ok8@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //
//! @example
#include <random>
#include <DO/Sara/Graphics.hpp>
GRAPHICS_MAIN()
{
using namespace std;
using namespace DO::Sara;
// Uniform distributions.
auto rng = std::mt19937{};
auto uni_0_255 = std::uniform_int_distribution<>{0, 255};
auto uni_0_300 = std::uniform_int_distribution<>{0, 300};
auto uniform_color_dist = [&]() {
return Color3ub(uni_0_255(rng), uni_0_255(rng), uni_0_255(rng));
};
// Create a white image and display it on a window.
auto image = Image<Rgb8>{300, 300};
image.flat_array().fill(White8);
create_window(300, 300);
display(image);
cout << "Display white image" << endl;
get_key();
// Draw random colors on an image surface.
for (auto y = 0; y < image.height(); ++y)
for (auto x = 0; x < image.width(); ++x)
draw_point(image, x, y, uniform_color_dist());
display(image);
cout << "Display random pixels" << endl;
get_key();
// Display random rectangles.
image.flat_array().fill(White8);
display(image);
get_key();
for (int i = 0; i < 10; ++i)
{
const auto x = uni_0_300(rng);
const auto y = uni_0_300(rng);
const auto w = uni_0_300(rng);
const auto h = uni_0_300(rng);
fill_rect(image, x, y, w, h, uniform_color_dist());
}
display(image);
cout << "Display random rectangles" << endl;
get_key();
close_window();
return 0;
}
Tutorial 3¶
// ========================================================================== //
// This file is part of Sara, a basic set of libraries in C++ for computer
// vision.
//
// Copyright (C) 2019 David Ok <david.ok8@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// ========================================================================== //
//! @example
#include <DO/Sara/Core/Timer.hpp>
#include <DO/Sara/Graphics.hpp>
using namespace std;
using namespace DO::Sara;
GRAPHICS_MAIN()
{
Image<Color3ub> I;
cout << src_path("../../../data/ksmall.jpg") << endl;
if (!load(I, src_path("../../../data/ksmall.jpg")))
{
cerr << "Error: could not open 'ksmall.jpg' file" << endl;
return 1;
}
int w = I.width(), h = I.height();
int x = 0, y = 0;
create_window(2 * w, h);
Timer drawTimer;
drawTimer.restart();
double elapsed;
for (int i = 0; i < 1; ++i)
{
clear_window();
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
draw_point(x, y, I(x, y));
draw_point(w + x, y, I(x, y));
#ifdef Q_OS_MAC
microsleep(10);
#endif
}
}
}
elapsed = drawTimer.elapsed();
std::cout << "Drawing time: " << elapsed << "s" << std::endl;
click();
int step = 2;
Timer t;
clear_window();
while (true)
{
microsleep(10);
display(I, x, y);
clear_window();
x += step;
if (x < 0 || x > w)
step *= -1;
//cout << x << endl;
if (t.elapsed() > 2)
break;
}
close_window(active_window());
cout << "Finished!" << endl;
return 0;
}