图像容器
Image<T>
类是一般的图像容器,基于Eigen
的行优先矩阵。T
有4种类型:grayscale, RGB, RGBA or 自定义类型
// A 8-bit gray image:
Image<unsigned char> grayscale_image_8bit;
// A 32-bit gray image:
Image<double> grayscale_image_32bit;
// Multichannel image: (use pre-defined pixel type)
// A 8-bit RGB image:
Image<RGBColor> rgb_image_8bit;
Image<Rgb<unsigned char> > rgb_image2_8bit;
// 8-bit RGBA image
Image<RGBAColor> rgba_image_8bit;
Image<Rgba<unsigned char> > rgba_image2_8bit;
// 32 bit RGB image:
Image<Rgb<double> > rgb_image_32bit;
//Image_test.cpp
//初始化
Image<unsigned char> imaGray(10,10); //(宽,高)
imaGray(1,1) = 1; //-- Pixel modification
// 复制初始化
Image<unsigned char> imageGray2(imaGray);
//通过矩阵初始化
Eigen::Matrix<unsigned char, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> matrix(5,5);
const Image<unsigned char> imageGray3(matrix);
//- 图像容器-->矩阵
matrix = imaGray.GetMat();
//克隆,和opencv不同
Image<unsigned char> imageGray4;
imageGray4 = matrix;
// 重新设置大小
Image<unsigned char> imaToResize;
imaToResize.resize(5,10);
//设置初始值
Image<RGBColor> imaColorRGB(5,5);
imaColorRGB.fill(RGBColor(10,10,10));
//大小:image.Width(), image.Height(), image.Depth(), imgHeader.width
注意:cout
不能输出unsigned char
类型的容器。因为Eigen
默认是当做字符输出的。可以通过强制类型转换,输出
图像IO
支持的图像类型:ppm/pgm, jpeg, png, tiff
//image_IO_test.cpp
// Read a grayscale image (if conversion need, it is done on the fly)
Image<unsigned char> gray_image;
bool bRet = ReadImage("Foo.imgExtension", &gray_image);
// Read a color image
Image<RGBColor> rgb_image_gray;
bool bRet = ReadImage("Foo.imgExtension", &rgb_image);
绘画
可以画:lines, circles, ellipses
。下面可以看出(x, y)
的方向
Image<unsigned char> image(10,10);
image.fill(0);
// Pixel access is done as matrix (row, line)
int row = 2;
int column = 4;
image(row, column) = 127;
// Horizontal scanline
DrawLine( 0, 5, w-1, 5, 255, &image);
// Circle of radius 3 and center (5,5)
const int radius = 3;
const int x = 5, y = 5;
DrawCircle(x, y, radius, (unsigned char)255, &image);
// Ellipse of center (5,5) and (3,0
const int radius1 = 3, radius2 = 1, angle = 0;
const int x = 5, y = 5;
DrawEllipse(x, y, radius1, radius2, (unsigned char)255, &image, (double)angle);
// Example with a RGB image
Image<RGBColor> imageRGB(10,10);
DrawCircle(x, y, radius, RGBColor(255,0,0), &imageRGB);
采样
有SamplerNearest,SamplerLinear, SamplerCubic, SamplerSpline16, SamplerSpline64
等5种采样类型
滤波
省略
图像积分
省略
c++技巧
//填充等值
fill(((unsigned char*)ptr+9*10),((unsigned char*)ptr+10*10),2);
源代码
命名空间:openMVG::image
image_container.hpp
内容
- 图像类
template <typename T>
class Image : public Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
c++知识
- 派生类的作用域嵌套在基类的作用域之内,可以使用作用域运算符来使用基类的成员
inline int Width() const
{
return static_cast<int>( Base::cols() );
}
image_concat.hpp
内容
template < class Image >
void ConcatH( const Image & imageA, const Image & imageB, Image & Out )
template < class Image >
void ConcatV( const Image & imageA, const Image & imageB, Image & Out )
c++知识
- eigen矩阵的
block(r1, c1, r2, c2)
及=
是值赋值
pixel_types.hpp
内容
rgb
、rgba
类
template <typename T>
class Rgb : public Eigen::Matrix<T, 3, 1, 0, 3, 1>
template <typename T>
class Rgba : public Eigen::Matrix<T, 4, 1, 0, 4, 1>
- 一些别名
/// Instantiation for unsigned char color component
using RGBColor = Rgb<unsigned char>;
/// Instantiation for float color component
using RGBfColor = Rgb<float>;
/// Type used to handle RGBA color in unsigned char format for each component
using RGBAColor = Rgba<unsigned char>;
const RGBColor WHITE( 255, 255, 255 );
const RGBColor BLACK( 0, 0, 0 );
const RGBColor BLUE( 0, 0, 255 );
const RGBColor RED( 255, 0, 0 );
const RGBColor GREEN( 0, 255, 0 );
const RGBColor YELLOW( 255, 255, 0 );
const RGBColor CYAN( 0, 255, 255 );
const RGBColor MAGENTA( 255, 0, 255 );
image_converter.hpp
内容
单个像素类型转换、不同像素类型的图像转换
c++知识
- 函数模板特殊化
template<typename Tin, typename Tout>
inline void Convert( const Tin& valin, Tout& out )
template<>
inline void Convert<RGBAColor, RGBColor>( const RGBAColor& valin, RGBColor& valOut )
image_convolution.hpp
- 内容
template<class T1, class T2> inline
void conv_buffer_( T1* buffer, const T2* kernel, int rsize, int ksize )
- c++知识
cstddef
里面定义了size_t
for ( size_t i = 0; i < rsize; ++i )
,这样编译器会不警告的