notion boost教程

您所在的位置:网站首页 notion中文社区 notion boost教程

notion boost教程

2022-03-25 17:10| 来源: 网络整理| 查看: 265

原文引用自

http://stlab.adobe.com/gil/html/giltutorial.html

 

翻译引用自(注:修改并重新翻译)

http://www.cppprog.com/boost_doc/libs/gil/doc/html/giltutorial.html

 

此教程的原文即英文版属于boost和adobe

http://www.boost.org/

http://www.adobe.com/

 

(翻译均为个人观点,请以原文为准)

 

Generic Image Library Tutorial

 

【翻译】泛型图片库(GIL)教程

 

Author:

Lubomir Bourdev ([email protected]) and Hailin Jin ([email protected]

Adobe Systems Incorporated

 

作者:

Lubomir Bourdev ([email protected]) and Hailin Jin ([email protected]

Adobe系统公司

 

Version:

2.1

 

版本:

2.1

 

Date:

September 15, 2007

 

日期:

2007年9月15日

 

The Generic Image Library (GIL) is a C++ library that abstracts image representations from algorithms and allows writing code that can work on a variety of images with performance similar to hand-writing for a specific image type.

 

泛型图片库(GIL)是算法上的抽象图片表示的C++函数库,允许书写代码,操纵一些不同的图片,而性能近似于针对特定图片类型的手工代码。

 

This document will give you a jump-start in using GIL. It does not discuss the underlying design of the library and does not cover all aspects of it. You can find a detailed library design document on the main GIL web page at http://opensource.adobe.com/gil

 

本文档将给你使用GIL的快速起点。这里不讨论库的底层设计,也不会涵盖它的所有方面。你可以GIL的主网页http://stlab.adobe.com/gil找到更详细的库设计文档。

 

Installation

Example - Computing the Image Gradient

Interface and Glue Code

First Implementation

Using Locators

Creating a Generic Version of GIL Algorithms

Image View Transformations

1D pixel iterators

STL Equivalent Algorithms

Color Conversion

Image

Virtual Image Views

Run-Time Specified Images and Image Views

Conclusion

Appendix

Naming convention for GIL concrete types

 

安装

示例 - 计算图片梯度

接口和粘合代码

第一个实现

使用定位器

创建GIL算法的泛型版本

图像视图变换

一维像素迭代器

STL对应算法

颜色变换

图片

虚拟图片视图

运行时图片和图片视图

结论

附录

GIL实际类型的命名约定

 

Installation

 

安装

 

The latest version of GIL can be downloaded from GIL's web page, at http://opensource.adobe.com/gil. GIL is approved for integration into Boost and in the future will be installed simply by installing Boost from http://www.boost.org. GIL consists of header files only and does not require any libraries to link against. It does not require Boost to be built. Including boost/gil/gil_all.hpp will be sufficient for most projects.

 

GIL的最新版本可以从GIL的网页下载,http://opensource.adobe.com/gil。 GIL的被批准集成进Boost,将来可以从http://www.boost.org通过安装Boost来简单安装。GIL只包含头文件,不要求链接任何库。它不需要编译Boost。包含boost/gil/gil_all.hpp将足以应对大多数项目。

 

Example - Computing the Image Gradient

 

示例 - 计算图片梯度

 

This tutorial will walk through an example of using GIL to compute the image gradients. We will start with some very simple and non-generic code and make it more generic as we go along. Let us start with a horizontal gradient and use the simplest possible approximation to a gradient - central difference. The gradient at pixel x can be approximated with the half-difference of its two neighboring pixels: D[x] = (I[x-1] - I[x+1]) / 2

 

本教程将把使用GIL计算图像梯度的示例贯穿始终。我们将从一些非常简单和非泛型的代码开始,使之随着我们的步伐更泛型。让我们从水平梯度开始,使用梯度的最简近似算法 - 中心差。水平像素x的梯度可以用它的两个邻接像素的半差分近似计算:D[x] = (I[x-1] - I[x+1]) / 2

 

For simplicity, we will also ignore the boundary cases - the pixels along the edges of the image for which one of the neighbors is not defined. The focus of this document is how to use GIL, not how to create a good gradient generation algorithm.

 

简单起见,我们也将忽略边界情况 - 图片边界像素的一个邻接像素没有定义。本文档的焦点是如何使用GIL,而不是如何创建一个好的梯度生成算法.

 

Interface and Glue Code

 

接口和粘合代码

 

Let us first start with 8-bit unsigned grayscale image as the input and 8-bit signed grayscale image as the output. Here is how the interface to our algorithm looks like:

 

让我们首先从输入是8位无符号灰度图而输出是8位有符号灰度图开始。这里我们的算法接口看起来就像这样子:

 

#include

using namespace boost::gil;

void x_gradient(const gray8c_view_t& src, const gray8s_view_t& dst) {

    assert(src.dimensions() == dst.dimensions());

    ...    // compute the gradient //计算梯度

}

 

gray8c_view_t is the type of the source image view - an 8-bit grayscale view, whose pixels are read-only (denoted by the "c"). The output is a grayscale view with a 8-bit signed (denoted by the "s") integer channel type. See Appendix 1 for the complete convension GIL uses to name concrete types.

 

gray8c_view_t是源图片视图的类型——一个8位灰度视图,它的像素只读(用“c”表明),输出是8位有符号(用“s”表示)整型通道类型。附录1提供命名GIL实际类型的完整约定(注:约定的正式英文应该是convention)。

 

GIL makes a distinction between an image and an image view. A GIL image view, is a shallow, lightweight view of a rectangular grid of pixels. It provides access to the pixels but does not own the pixels. Copy-constructing a view does not deep-copy the pixels. Image views do not propagate their constness to the pixels and should always be taken by a const reference. Whether a view is mutable or read-only (immutable) is a property of the view type.

 

GIL区分图片和图片视图。一个GIL图片视图, 是一个像素矩形格的平薄的、轻量级的视图。它提供像素的读写能力,但并不拥有像素。视图的复制构造并不会深度复制像素。图片视图不会把它们的常量性传播到图片像素,而总是应该以常量引用来持有它们(注:这里可能指持有图片视图而非持有图片像素。个人认为,对C++的引用型参数都加上const可避免类型转换)。不管视图可变还是只读(不变),它都是视图类型的一个属性。

 

A GIL image, on the other hand, is a view with associated ownership. It is a container of pixels; its constructor/destructor allocates/deallocates the pixels, its copy-constructor performs deep-copy of the pixels and its operator== performs deep-compare of the pixels. Images also propagate their constness to their pixels - a constant reference to an image will not allow for modifying its pixels.

 

另一方面,GIL图片是一种拥有相关所有权的视图。它是像素的容器;它的构造或析构函数分配或释放像素。它的复制构造函数执行像素的深度复制,而操作符==会执行像素值的深度比较。图像也会把它们的常量性传播给它们的像素——一个指向图片的常量引用将不允许用于修改它的像素。

 

Most GIL algorithms operate on image views; images are rarely needed. GIL's design is very similar to that of the STL. The STL equivalent of GIL's image is a container, like std::vector, whereas GIL's image view corresponds to STL's range, which is often represented with a pair of iterators. STL algorithms operate on ranges, just like GIL algorithms operate on image views.

 

大多数GIL算法是在图片视图上操作的;极少需要用图片。GIL的设计非常近似于STL的设计。GIL图片的STL等价物是容器,像std::vector,而GIL's的图片视图对应STL中常表现为一对迭代器的区间(注:这里可能指begin()和end()这两个迭代器)。操作在区间的STL算法,正像操作在图片视图的GIL算法那样.

 

GIL's image views can be constructed from raw data - the dimensions, the number of bytes per row and the pixels, which for chunky views are represented with one pointer. Here is how to provide the glue between your code and GIL:

 

GIL的图片视图可以从原生数据中构造——尺寸(注:其实就是size,图片大小)、每行(注:图片横向的像素线)和每个像素的字节数,对于一小块视图可以用一个指针表示。这里介绍如何把你的代码和GIL粘合起来(注:就是说,如果你只有裸数据指针,你必须用下面的代码把它转成视图,否则GIL算法函数x_gradient无法使用):

 

void ComputeXGradientGray8(const unsigned char* src_pixels, ptrdiff_t src_row_bytes, int w, int h,

                                   signed char* dst_pixels, ptrdiff_t dst_row_bytes) {

    gray8c_view_t src = interleaved_view(w, h, (const gray8_pixel_t*)src_pixels,src_row_bytes);

    gray8s_view_t dst = interleaved_view(w, h, (     gray8s_pixel_t*)dst_pixels,dst_row_bytes);

    x_gradient(src,dst);

}

 

This glue code is very fast and views are lightweight - in the above example the views have a size of 16 bytes. They consist of a pointer to the top left pixel and three integers - the width, height, and number of bytes per row.

 

这段粘合代码非常块,而且视图都是轻量级的 - 上面的示例中每个视图只占16个字节。它们包括一个指向左上角像素的指针和三个整数——宽,高以及每行字节数。(注:像素是单字节,可能已经反映在实际类型中,所以不需要专门保存)

 

First Implementation

 

第一个实现

 

Focusing on simplicity at the expense of speed, we can compute the horizontal gradient like this:

 

把焦点放在简单而非速度上面,我们可以像这样计算水平梯度:

 

void x_gradient(const gray8c_view_t& src, const gray8s_view_t& dst) {

    for (int y=0; y



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3