Solarized Dark Color Scheme


[Solarized Dark]
text(bold)=839496
magenta(bold)=6c71c4
text=839496
white(bold)=fdf6e3
green=859900
red(bold)=cb4b16
green(bold)=586e75
black(bold)=073642
red=dc322f
blue=268bd2
black=002b36
blue(bold)=839496
yellow(bold)=657b83
cyan(bold)=93a1a1
yellow=b58900
magenta=dd3682
background=042028
white=eee8d5
cyan=2aa198
[Names]
count=1
name0=Solarized Dark

对于struct file_operations中ioctl消失的学习笔记

对于struct file_operations中ioctl消失的学习笔记

 

文章启发点:

ioctl从大内核锁变更为需要内核开发工程师自己加小锁.这点很多内核开发工程师可能忘掉. 从而导致可能的条件竞争漏洞存在. 从这点来挖掘toc2tou的漏洞是个思路.

转载原文如下:

很久都没有写驱动代码了,对于一些驱动相关的内核变化也没有怎么关心。这次重游《LDD3》获益良多,其值对于struct file_operations中ioctl的消失也让我长了不少见识。
当年看《LDD3》的时候已经注意到了书中对ioctl的评价不是很好:“ioctl调用的非结构化本质导致众多内核开发者倾向于放弃它。” ,而在这次阅读3.0代码的时候,这个成员在struct file_operations中早已消失了。这个激起了我学习的兴趣,以下是对这个ioctl的学习小结:

1、消失的确切时间
ioctl的消失到底是从哪个版本开始的?网上给出的时间是2.6.36开始。网上就是这么说,但是自己必须找到代码中的证据。于是我通过git搜索主线内核代码,找到的删除ioctl的那个提交:

  1. commit b19dd42faf413b4705d4adb38521e82d73fa4249
  2. Author: Arnd Bergmann<arnd@arndb.de>
  3. Date: Sun Jul 4 00:15:10 2010 +0200
  4. bkl: Remove locked .ioctl file operation
  5. The last user is gone, so we can safely remove this
  6. Signed-off-by: Arnd Bergmann<arnd@arndb.de>
  7. Cc: John Kacur<jkacur@redhat.com>
  8. Cc: Al Viro<viro@zeniv.linux.org.uk>
  9. Cc: Thomas Gleixner<tglx@linutronix.de>
  10. Signed-off-by: Frederic Weisbecker<fweisbec@gmail.com>

好不容易找到了这个提交,好的,这样就可以确定消失的时间了:

  1. git tag –contains b19dd42
  2. v2.6.36
  3. v2.6.36-rc1
  4. v2.6.36-rc2
  5. v2.6.36-rc3
  6. v2.6.36-rc4
  7. v2.6.36-rc5
  8. v2.6.36-rc6
  9. v2.6.36-rc7
  10. v2.6.36-rc8
  11. ……以下省略ooxx行

可以证明ioctl消失的版本是v2.6.35到v2.6.36-rc1间,于是我导出了v2.6.35到v2.6.36-rc1的补丁,果真在其中!
git diff v2.6.35..v2.6.36-rc1 > ../temp.patch

补丁过大不易上传,请自行生成。

2、消失的原因

简单的概括:这次ioctl的消失,并不是要把ioctl清理出去,而是要逐步的清理大内核锁(BKL)。

这个让ioctl消失的过渡期长达5年,从2005年开始内核黑客就开始替换ioctl了。具体的原因lwn.net中有一篇很好的文章:The new way of ioctl()。我将他翻译了一下:ioctl()的新方法(必看)

当然,顺便了解一下大内核锁也是很有必要的:转载好文:《大内核锁将何去何从》

3、ioctl的替代者

对于原来的ioctl,其实可以叫做locked ioctl。这个其实是相对于他的替代方法来讲的。我们来看看2.6.35以前在struct file_operations中有关ioctl的成员:

  1. /*
  2.  * NOTE:
  3.  * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
  4.  * can be called without the big kernel lock held in all filesystems.
  5.  */
  6. struct file_operations {
  7.     struct module *owner;
  8.     loff_t (*llseek) (struct file *, loff_t, int);
  9.     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  10.     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  11.     ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
  12.     ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
  13.     int (*readdir) (struct file *, void *, filldir_t);
  14.     unsigned int (*poll) (struct file *, struct poll_table_struct *);
  15.     int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  16.     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  17.     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  18.     int (*mmap) (struct file *, struct vm_area_struct *);
  19.     int (*open) (struct inode *, struct file *);
  20.     int (*flush) (struct file *, fl_owner_t id);
  21.     int (*release) (struct inode *, struct file *);
  22.     int (*fsync) (struct file *, struct dentry *, int datasync);
  23.     int (*aio_fsync) (struct kiocb *, int datasync);
  24.     int (*fasync) (int, struct file *, int);
  25.     int (*lock) (struct file *, int, struct file_lock *);
  26.     ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  27.     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  28.     int (*check_flags)(int);
  29.     int (*flock) (struct file *, int, struct file_lock *);
  30.     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
  31.     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
  32.     int (*setlease)(struct file *, long, struct file_lock **);
  33. };

这个结构体其实是在过渡期的结构体,unlocked_ioctl就是ioctl的替代者。对于新的驱动,不要再使用ioctl了,而是使用unlocked_ioctl。

4、调用ioctl与unlocked_ioctl在内核代码上的不同

   其实ioctl与unlocked_ioctl所对应的系统调用都是ioctl。但是在应用层调用ioctl的时候,对于我们实现ioctl或者unlocked_ioctl有什么不同呢?这里我们可以追溯一下ioctl系统调用代码的执行过程,这里我简单的写出这个系统调用对于设备驱动(一般是设备驱动使用ioctl)的执行顺序:(fs/ioctl.c)

 SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)—>do_vfs_ioctl—> vfs_ioctl

ioctl与unlocked_ioctl的区别就体现在了这个vfs_ioctl中,我们先来看ioctl被删除前的函数:

  1. /**
  2.  * vfs_ioctl – call filesystem specific ioctl methods
  3.  * @filp:    open file to invoke ioctl method on
  4.  * @cmd:    ioctl command to execute
  5.  * @arg:    command-specific argument for ioctl
  6.  *
  7.  * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  8.  * invokes filesystem specific ->ioctl method. If neither method exists,
  9.  * returns -ENOTTY.
  10.  *
  11.  * Returns 0 on success, -errno on error.
  12.  */
  13. static long vfs_ioctl(struct file *filp, unsigned int cmd,
  14.          unsigned long arg)
  15. {
  16.     int error = -ENOTTY;
  17.     if (!filp->f_op)
  18.         goto out;
  19.     if (filp->f_op->unlocked_ioctl) {
  20.         error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  21.         if (error == -ENOIOCTLCMD)
  22.             error = -EINVAL;
  23.         goto out;
  24.     } else if (filp->f_op->ioctl) {
  25.         lock_kernel();
  26.         error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
  27.                      filp, cmd, arg);
  28.         unlock_kernel();
  29.     }
  30.  out:
  31.     return error;
  32. }

从这个函数中我们可以看出:

  1. ioctl是受到大内核锁保护的,而unlocked_ioctl是直接执行的。
  2. unlocked_ioctl优先级高于ioctl,如果存在unlocked_ioctl,则执行unlocked_ioctl,否则才执行ioctl。这个优先级的产生明显是为了过渡。

而在ioctl被删除后,vfs_ioctl函数也做了相应的改变(Linux-3.0):

  1. /**
  2.  * vfs_ioctl – call filesystem specific ioctl methods
  3.  * @filp:    open file to invoke ioctl method on
  4.  * @cmd:    ioctl command to execute
  5.  * @arg:    command-specific argument for ioctl
  6.  *
  7.  * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  8.  * returns -ENOTTY.
  9.  *
  10.  * Returns 0 on success, -errno on error.
  11.  */
  12. static long vfs_ioctl(struct file *filp, unsigned int cmd,
  13.          unsigned long arg)
  14. {
  15.     int error = -ENOTTY;
  16.     if (!filp->f_op || !filp->f_op->unlocked_ioctl)
  17.         goto out;
  18.     error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  19.     if (error == -ENOIOCTLCMD)
  20.         error = -EINVAL;
  21.  out:
  22.     return error;
  23. }

5、在驱动编程时的注意事项

    • 在注册文件操作方法的结构体

struct file_operations的时候原先的.ioctl=OOXX;替换为 .unlocked_ioctl=OOXX;但是要注意ioctl和unlocked_ioctl的定义有一点不同:unlocked_ioctl少了一个inode参数。但是如果方法中真的需要其中的数据,可以通过filp->f_dentry->d_inode获得。

  • 由于失去了大内核锁的保护,所以必须unlocked_ioctl方法中自行实现锁机制,以保证不会在操作设备的时候(特别在SMP系统中)产生竞态。(也就实现了用小锁替换大锁)

 

 

Linux下 解包/打包 Android 映像文件 system.img, boot.img, ramdisk.img, userdata.img.

转自: http://blog.csdn.net/yulix/article/details/12968705

 

Android源码编译成功后会输出映像文件:system.img,boot.img, ramdisk.img,userdata.img等等。有时我们需要修改里面的内容,下面列出在Linux下如何解包/打包这些映像文件。

 

ramdisk.img

ramdisk.img是经cpio打包、并用gzip压缩的文件。

解包: 新建一个工作目录,把当前目录更改为该工作目录,执行下面命令(注意: img文件位置可能不同).

 

[plain] view plaincopy

  1. gunzip -c  $HOME/img/ramdisk.img | cpio -i

打包:在工作目录下,把该目录下的所有内容打包

[plain] view plaincopy

  1. find . | cpio -o -H newc | gzip > ../newramdisk.img

 

参考文档:  http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images

 

boot.img

boot.img包含2K字节头部,后面跟着的是zImage格式内核和和ramdisk格式的根文件系统。

解包工具: Android自带的unpackbootimg,以及一些脚本工具比如split_bootimg.pl

打包工具: Android自带的mkbootimg。

参考资料 :

中文请看: http://blog.csdn.net/wh_19910525/article/details/8200372

  英文请看:  http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images

system.img (EXT4)

 

system.img 是 sparse image格式文件,现有的mount命令无法直接处理。

我们得把sparse image格式转变为普通的img格式,Android源码中带的ext4_utils可以做这个,没有Android源码也不用担心,该工具的源代码已被剥离出来,可以自行下载编译,地址是:http://forum.xda-developers.com/showthread.php?t=1081239

我们得到工具有: simg2img,make_ext4fs等等:

解包:

 

[plain] view plaincopy

  1. simg2img system.img system.img.ext4
  2. mkdir mnt_dir
  3. sudo mount -t ext4 -o loop system.img.ext4 mnt_dir

打包:

[plain] view plaincopy

  1. sudo make_ext4fs -s -l 512M -a system system_new.img mnt_dir

注意:在我的机器上必须用root权限执行make_ext4fs,否则新生成的image文件无法使用。

 

 

userdata.img (EXT4)

 

和system.img(EXT4) 一样处理

Python做图像处理/计算机视觉–PIL(python image library)

链接: PIL官方在线教程:
http://effbot.org/imagingbook/introduction.htm

We’re back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Tutorial

Using the Image Class

The most important class in the Python Imaging Library is the Image class, defined in the module with the same name. You can create instances of this class in several ways; either by loading images from files, processing other images, or creating images from scratch.

To load an image from a file, use the open function in the Image module.

>>> import Image
>>> im = Image.open("lena.ppm")

If successful, this function returns an Image object. You can now use instance attributes to examine the file contents.

>>> print im.format, im.size, im.mode
PPM (512, 512) RGB

The format attribute identifies the source of an image. If the image was not read from a file, it is set to None. Thesize attribute is a 2-tuple containing width and height (in pixels). The mode attribute defines the number and names of the bands in the image, and also the pixel type and depth. Common modes are “L” (luminance) for greyscale images, “RGB” for true colour images, and “CMYK” for pre-press images.

If the file cannot be opened, an IOError exception is raised.

Once you have an instance of the Image class, you can use the methods defined by this class to process and manipulate the image. For example, let’s display the image we just loaded:

    >>> im.show()

(The standard version of show is not very efficient, since it saves the image to a temporary file and calls the xvutility to display the image. If you don’t have xv installed, it won’t even work. When it does work though, it is very handy for debugging and tests.)

The following sections provide an overview of the different functions provided in this library.

Reading and Writing Images

The Python Imaging Library supports a wide variety of image file formats. To read files from disk, use the openfunction in the Image module. You don’t have to know the file format to open a file. The library automatically determines the format based on the contents of the file.

To save a file, use the save method of the Image class. When saving files, the name becomes important. Unless you specify the format, the library uses the filename extension to discover which file storage format to use.

Convert files to JPEG

import os, sys
import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print "cannot convert", infile

A second argument can be supplied to the save method which explicitly specifies a file format. If you use a non-standard extension, you must always specify the format this way:

Create JPEG Thumbnails

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for", infile

It is important to note that the library doesn’t decode or load the raster data unless it really has to. When you open a file, the file header is read to determine the file format and extract things like mode, size, and other properties required to decode the file, but the rest of the file is not processed until later.

This means that opening an image file is a fast operation, which is independent of the file size and compression type. Here’s a simple script to quickly identify a set of image files:

Identify Image Files

import sys
import Image

for infile in sys.argv[1:]:
    try:
        im = Image.open(infile)
        print infile, im.format, "%dx%d" % im.size, im.mode
    except IOError:
        pass

Cutting, Pasting and Merging Images

The Image class contains methods allowing you to manipulate regions within an image. To extract a sub-rectangle from an image, use the crop method.

Copying a subrectangle from an image

    box = (100, 100, 400, 400)
    region = im.crop(box)

The region is defined by a 4-tuple, where coordinates are (left, upper, right, lower). The Python Imaging Library uses a coordinate system with (0, 0) in the upper left corner. Also note that coordinates refer to positions between the pixels, so the region in the above example is exactly 300×300 pixels.

The region could now be processed in a certain manner and pasted back.

Processing a subrectangle, and pasting it back

    region = region.transpose(Image.ROTATE_180)
    im.paste(region, box)

When pasting regions back, the size of the region must match the given region exactly. In addition, the region cannot extend outside the image. However, the modes of the original image and the region do not need to match. If they don’t, the region is automatically converted before being pasted (see the section on Colour Transformsbelow for details).

Here’s an additional example:

Rolling an image

def roll(image, delta):
    "Roll an image sideways"

    xsize, ysize = image.size

    delta = delta % xsize
    if delta == 0: return image

    part1 = image.crop((0, 0, delta, ysize))
    part2 = image.crop((delta, 0, xsize, ysize))
    image.paste(part2, (0, 0, xsize-delta, ysize))
    image.paste(part1, (xsize-delta, 0, xsize, ysize))

    return image

For more advanced tricks, the paste method can also take a transparency mask as an optional argument. In this mask, the value 255 indicates that the pasted image is opaque in that position (that is, the pasted image should be used as is). The value 0 means that the pasted image is completely transparent. Values in-between indicate different levels of transparency.

The Python Imaging Library also allows you to work with the individual bands of an multi-band image, such as an RGB image. The split method creates a set of new images, each containing one band from the original multi-band image. The merge function takes a mode and a tuple of images, and combines them into a new image. The following sample swaps the three bands of an RGB image:

Splitting and merging bands

r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))

Note that for a single-band image, split returns the image itself. To work with individual colour bands, you may want to convert the image to “RGB” first.

Geometrical Transforms

The Image class contains methods to resize and rotate an image. The former takes a tuple giving the new size, the latter the angle in degrees counter-clockwise.

Simple geometry transforms

out = im.resize((128, 128))
out = im.rotate(45) # degrees counter-clockwise

To rotate the image in 90 degree steps, you can either use the rotate method or the transpose method. The latter can also be used to flip an image around its horizontal or vertical axis.

Transposing an image

out = im.transpose(Image.FLIP_LEFT_RIGHT)
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)

There’s no difference in performance or result between transpose(ROTATE) and corresponding rotateoperations.

A more general form of image transformations can be carried out via the transform method. See the reference section for details.

Colour Transforms

The Python Imaging Library allows you to convert images between different pixel representations using the convert function.

Converting between modes

    im = Image.open("lena.ppm").convert("L")

The library supports transformations between each supported mode and the “L” and “RGB” modes. To convert between other modes, you may have to use an intermediate image (typically an “RGB” image).

Image Enhancement

The Python Imaging Library provides a number of methods and modules that can be used to enhance images.

Filters

The ImageFilter module contains a number of pre-defined enhancement filters that can be used with the filtermethod.

Applying filters

import ImageFilter
out = im.filter(ImageFilter.DETAIL)

Point Operations

The point method can be used to translate the pixel values of an image (e.g. image contrast manipulation). In most cases, a function object expecting one argument can be passed to the this method. Each pixel is processed according to that function:

Applying point transforms

# multiply each pixel by 1.2
out = im.point(lambda i: i * 1.2)

Using the above technique, you can quickly apply any simple expression to an image. You can also combine thepoint and paste methods to selectively modify an image:

Processing individual bands

# split the image into individual bands
source = im.split()

R, G, B = 0, 1, 2

# select regions where red is less than 100
mask = source[R].point(lambda i: i < 100 and 255)

# process the green band
out = source[G].point(lambda i: i * 0.7)

# paste the processed band back, but only where red was < 100
source[G].paste(out, None, mask)

# build a new multiband image
im = Image.merge(im.mode, source)

Note the syntax used to create the mask:

    imout = im.point(lambda i: expression and 255)

Python only evaluates the portion of a logical expression as is necessary to determine the outcome, and returns the last value examined as the result of the expression. So if the expression above is false (0), Python does not look at the second operand, and thus returns 0. Otherwise, it returns 255.

Enhancement

For more advanced image enhancement, you can use the classes in the ImageEnhance module. Once created from an image, an enhancement object can be used to quickly try out different settings.

You can adjust contrast, brightness, colour balance and sharpness in this way.

Enhancing images

import ImageEnhance

enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")

Image Sequences

The Python Imaging Library contains some basic support for image sequences (also called animation formats). Supported sequence formats include FLI/FLC, GIF, and a few experimental formats. TIFF files can also contain more than one frame.

When you open a sequence file, PIL automatically loads the first frame in the sequence. You can use the seek andtell methods to move between different frames:

Reading sequences

import Image

im = Image.open("animation.gif")
im.seek(1) # skip to the second frame

try:
    while 1:
        im.seek(im.tell()+1)
        # do something to im
except EOFError:
    pass # end of sequence

As seen in this example, you’ll get an EOFError exception when the sequence ends.

Note that most drivers in the current version of the library only allow you to seek to the next frame (as in the above example). To rewind the file, you may have to reopen it.

The following iterator class lets you to use the for-statement to loop over the sequence:

A sequence iterator class

class ImageSequence:
    def __init__(self, im):
        self.im = im
    def __getitem__(self, ix):
        try:
            if ix:
                self.im.seek(ix)
            return self.im
        except EOFError:
            raise IndexError # end of sequence

for frame in ImageSequence(im):
    # ...do something to frame...

Postscript Printing

The Python Imaging Library includes functions to print images, text and graphics on Postscript printers. Here’s a simple example:

Drawing Postscript

import Image
import PSDraw

im = Image.open("lena.ppm")
title = "lena"
box = (1*72, 2*72, 7*72, 10*72) # in points

ps = PSDraw.PSDraw() # default is sys.stdout
ps.begin_document(title)

# draw the image (75 dpi)
ps.image(box, im, 75)
ps.rectangle(box) 

# draw centered title
ps.setfont("HelveticaNarrow-Bold", 36)
w, h, b = ps.textsize(title)
ps.text((4*72-w/2, 1*72-h), title) 

ps.end_document()

More on Reading Images

As described earlier, the open function of the Image module is used to open an image file. In most cases, you simply pass it the filename as an argument:

im = Image.open("lena.ppm")

If everything goes well, the result is an Image object. Otherwise, an IOError exception is raised.

You can use a file-like object instead of the filename. The object must implement readseek and tell methods, and be opened in binary mode.

Reading from an open file

fp = open("lena.ppm", "rb")
im = Image.open(fp)

To read an image from string data, use the StringIO class:

Reading from a string

import StringIO

im = Image.open(StringIO.StringIO(buffer))

Note that the library rewinds the file (using seek(0)) before reading the image header. In addition, seek will also be used when the image data is read (by the load method). If the image file is embedded in a larger file, such as a tar file, you can use the ContainerIO or TarIO modules to access it.

Reading from a tar archive

import TarIO

fp = TarIO.TarIO("Imaging.tar", "Imaging/test/lena.ppm")
im = Image.open(fp)

Controlling the Decoder

Some decoders allow you to manipulate the image while reading it from a file. This can often be used to speed up decoding when creating thumbnails (when speed is usually more important than quality) and printing to a monochrome laser printer (when only a greyscale version of the image is needed).

The draft method manipulates an opened but not yet loaded image so it as closely as possible matches the given mode and size. This is done by reconfiguring the image decoder.

Reading in draft mode

im = Image.open(file)
print "original =", im.mode, im.size

im.draft("L", (100, 100))
print "draft =", im.mode, im.size

This prints something like:

original = RGB (512, 512)
draft = L (128, 128)

Note that the resulting image may not exactly match the requested mode and size. To make sure that the image is not larger than the given size, use the thumbnail method instead.

 

 

backtrack5 安装postgresql

root登录
密码toor

sudo apt-get install postgresql

一路安装

安装完成使用linux帐号postgres登录postgresql

sudo -u postgres postgresql

在postgresql的命令行中执行如下命令:

—-
ALTER USER postgres WITH PASSWORD ‘postgres’;
\q
—-
以将postgres的密码修改成postgres

安装一个pgadmin3,图形化管理postgresql

sudo apt-get install pgadmin3

图像的几何不变矩

矩特征主要表征了图像区域的几何特征,又称为几何矩, 由于其具有旋转、平移、尺度等特性的不变特征,所以又称其为不变矩。在图像处理中,几何不变矩可以作为一个重要的特征来表示物体,可以据此特征来对图像进行分类等操作。如果想详细了解不变矩的概念、公式等请参考下面的论文:
不变矩方法研究
一种实用的不变矩计算方法

http://download.csdn.net/source/1943495

1.     HU矩

几何矩是由Hu(Visual pattern recognition by moment invariants)在1962年提出的,图像f(x,y)的(p+q)阶几何矩定义为

Mpq =∫∫(x^p)*(y^q)f(x,y)dxdy(p,q = 0,1,……∞)

矩在统计学中被用来反映随机变量的分布情况,推广到力学中,它被用作刻画空间物体的质量分布。同样的道理,如果我们将图像的灰度值看作是一个二维或三维的密度分布函数,那么矩方法即可用于图像分析领域并用作图像特征的提取。最常用的,物体的零阶矩表示了图像的“质量”:

Moo= ∫∫f(x,y )dxdy

一阶矩(M01,M10)用于确定图像质心( Xc,Yc):

Xc = M10/M00;Yc = M01/M00;

若将坐标原点移至 Xc和 Yc处,就得到了对于图像位移不变的中心矩。如

Upq =∫∫[(x-Xc)^p]*[(y-Yc)^q]f(x,y)dxdy。

Hu在文中提出了7个几何矩的不变量,这些不变量满足于图像平移、伸缩和旋转不变。如果定义

Zpq=Upq/(U20 + U02)^(p+q+2),

Hu 的7种矩为:

H1=Z20+Z02;H1=(Z20+Z02)^2+4Z11^2;……

矩是描述图像特征的算子,它在模式识别与图像分析领域中有重要的应用.迄今为止,常见的矩描述子可以分为以下几种:几何矩、正交矩、复数矩和旋转矩.其中几何矩提出的时间最早且形式简单,对它的研究最为充分。几何矩对简单图像有一定的描述能力,他虽然在区分度上不如其他三种矩,但与其他几种算子比较起来,他极其的简单,一般只需用一个数字就可表达。所以,一般我们是用来做大粒度的区分,用来过滤显然不相关的文档。

比如在图形库中,可能有100万幅图,也许只有200幅图是我们想要的。使用一维的几何矩的话,就可以对几何矩进行排序,建立索引,然后选出与目标图的几何矩最近的2000幅图作比较就好了。而对于其他的矩来说,由于一般是多维的关系,一般不好排序,只能顺序查找,自然速度有巨大的差别.所以。虽然几何矩不太能选出最像的,但可以快速排除不像的,提高搜索效率。

几种简单的几何矩:

令平面上点坐标为P(x,y),重心为C(x!,y!),
二阶行距:rowMoment = [∑(x- x!)*(x- x!)]/A
二阶列距:colMoment = [∑(y- y!)*(y- y!)]/A
A为点的个数。
由以上两个信息可以算出图形的圆度:circleDisgree = rowMoment /colMoment .如果图形的circleDisgree 越小于1,则它越趋向于长轴为y方向的椭圆。如果图形的circleDisgree 越大于1,则它越趋向于长轴为x方向的椭圆.如果图形的circleDisgree 越接近于1,则它越趋向于圆。
所以我们可以使用圆度这种几何矩,对其进行索引,实现快速过滤。

//代码实例

double* Getsquare(int **Array1,int Width,int Height//Array1图像灰度矩阵

{

int x,y;

double pSum,dx,dy;

int xmax,xmin,ymax,ymin,xSum,ySum,PointSum;

double dd,n1,n2,xAve,yAve;

pSum=Height*Width;

xSum=0;ySum=0;PointSum=0;

xmin=10000;ymin=10000;xmax=-1;ymax=-1;

 

for (y=0 ;y<Height; y++)

{

for (x=0; x<Width; x++)

{

if (Array1[x][y]==255)

{

continue;

}

xSum=xSum+x;ySum=ySum+y;++PointSum;

if (x<xmin)

{

xmin=x;

}

if (x>xmax)

{

xmax=x;

}

}

if (y<ymin)

{

ymin=y;

}

if (y>ymax)

{

ymax=y;

}

}

if (pSum==0)

{

goto Loop;

}

xAve=xSum/pSum;

yAve=ySum/pSum;

///////////////////////////////////////////上面为计算x,y平均值

for (x=1; x<16; x++)

{

SqureNumber[x]=0;

}

// 11 20 02 21+ 21- 12+ 12- 30+ 30- 03+ 02-

for (y=0; y<Height; y++)

{

for (x=0; x<Width; x++)

{

if (Array1[x][y]==255)

{

continue;

}

dx=x-xAve;dy=y-yAve;

SqureNumber[1]=SqureNumber[1]+dx*dx;      // 计算 u(11) 11

SqureNumber[2]=SqureNumber[2]+dx*dx;      // 计算 u(20) 20

SqureNumber[3]=SqureNumber[3]+dy*dy;      // 计算 u(02) 02

if (dy>0)

{

SqureNumber[4]=SqureNumber[4]+dx*dx*dy; // 计算 u(21)+ 21+

}

else

{

SqureNumber[5]=SqureNumber[5]+dx*dx*dy; // 计算 u(21)- 21-

}

if( dx>0 )

{

SqureNumber[6]=SqureNumber[6]+dx*dy*dy; // 计算 u(12)+ 12+

}

else

{

SqureNumber[7]=SqureNumber[7]+dx*dy*dy; // 计算 u(12}- 12-

}

if (dx>0 )

{

SqureNumber[8]=SqureNumber[8]+dx*dx*dx ;// 计算 u(30)+ 30+

}

else

{

SqureNumber[9]=SqureNumber[9]+dx*dx*dx; // 计算 u(30)- 30-

}

if (dy>0)

{

SqureNumber[10]=SqureNumber[10]+dy*dy*dy ;// 计算 u(03)+ 03+

}

else

{

SqureNumber[11]=SqureNumber[11]+dy*dy*dy;// 计算 u(03)- 03-

}

}//end for x

}// end for y;

for (x=1; x<12; x++)

{

SqureNumber[x]=SqureNumber[x]/pSum;

}

///////////////////////////////////////////////////////计算图像的各阶矩

for (x=12; x<21; x++)

{

SqureNumber[x]=0;

}

SqureNumber[12]=((SqureNumber[2]-SqureNumber[3])/(SqureNumber[2]+SqureNumber[3]))/2; //长宽比特征

 

dd=sqrt((SqureNumber[2]-SqureNumber[3])*(SqureNumber[2]-SqureNumber[3])+4*SqureNumber[1]*SqureNumber[1]);

dd=dd+(SqureNumber[2]-SqureNumber[3]);

SqureNumber[16]=2*atan(dd/(2*SqureNumber[1]))/M_PI; ///字型倾斜度

 

dd=sqrt((SqureNumber[2]-SqureNumber[3])*(SqureNumber[2]-SqureNumber[3])+4*SqureNumber[1]*SqureNumber[1]);

n1=((SqureNumber[2]+SqureNumber[3])+dd)/2;

n2=((SqureNumber[2]+SqureNumber[3])-dd)/2;

SqureNumber[14]=(n1-n2)/(n1+n2);                   //拉长度

 

n1=sqrt((ymax-ymin)*(xmax-xmin));

dd=sqrt((SqureNumber[2]+SqureNumber[3])/PointSum);

SqureNumber[15]=dd/n1;                             //伸展度

 

dd=(SqureNumber[8]-SqureNumber[9])/(SqureNumber[8]+SqureNumber[9]);

SqureNumber[16]=(dd+1)/2;                          //水平偏移度

 

dd=(SqureNumber[10]-SqureNumber[11])/(SqureNumber[10]+SqureNumber[11]);

SqureNumber[17]=(dd+1)/2;                          //垂直偏移度

 

dd=(SqureNumber[4]-SqureNumber[5])/(SqureNumber[4]+SqureNumber[5]);

SqureNumber[18]=(dd+1)/2;                          //水平伸展度度

 

dd=(SqureNumber[6]-SqureNumber[7])/(SqureNumber[6]+SqureNumber[7]);

SqureNumber[19]=(dd+1)/2;                          //垂直伸展度

Loop:;

}

 

2.Zernike矩

在模式识别中,一个重要的问题是对目标的方向性变化也能进行识别。Zernike 矩是一组正交矩,具有旋转不变性的特性,即旋转目标并不改变其模值。。由于Zernike 矩可以构造任意高阶矩,所以Zernike 矩的识别效果优于其他方法.

Zernike 提出了一组多项式{ V nm ( x , y) } 。这组多项式在单位圆{ x2 + y2 ≤1} 内是正交的,具有如下形式: V nm ( x , y) = V nm (ρ,θ) = Rnm (ρ) exp ( jmθ) ,并且满足   ∫∫ x^2+y^2 <= 1  [( V nm ( x , y) 的共轭]* V pq ( x , y) d x d y.      = [pi/(n+1)]*δnpδmq .

if(a==b)  δab = 1 else  δab = 0,n 表示正整数或是0;m是正整数或是负整数它表示满足m的绝对值<=n 而且n-m的绝对值是偶数这两个条件;ρ 表示原点到象素(x,y)的向量的距离;θ 表示向量ρ 跟x 轴之间的夹角(逆时针方向).

 

 

对于一幅数字图象,积分用求和代替,即A nm =∑x∑y f(x,y) *[( V nm (ρ,θ) 的共轭],x^2+y^2 <=1

实际计算一幅给定图象的Zernike 矩时,必须将图象的重心移到坐标圆点,将图象象素点映射到单位圆内。由以上可知,使[ V nm (ρ,θ) 的共轭]可提取图象的特征,低频特性由n 值小的[( V nm (ρ,θ) 的共轭]来提取,高频特性由n 值大的来提取。Zernike 矩可以任意构造高价矩, 而高阶矩包含更多的图象信息, 所以Zernike 矩识别效果更好。,Zernike 矩仅仅具有相位的移动。

它的模值保持不变。所以可以将| A nm | 作为目标的旋转不变性特征。因为| A nm | =| A n , – m | ,所以只需计算m ≥0 的情况。

 

 

光流法简单介绍

光流的概念是Gibson在1950年首先提出来的。它是空间运动物体在观察成像平面上的像素运动的瞬时速度,是利用图像序列中像素在时间域上的变化以及相邻帧之间的相关性来找到上一帧跟当前帧之间存在的对应关系,从而计算出相邻帧之间物体的运动信息的一种方法。一般而言,光流是由于场景中前景目标本身的移动、相机的运动,或者两者的共同运动所产生的。其计算方法可以分为三类:

(1)基于区域或者基于特征的匹配方法;

(2)基于频域的方法;

(3)基于梯度的方法;

简单来说,光流是空间运动物体在观测成像平面上的像素运动的“瞬时速度”。光流的研究是利用图像序列中的像素强度数据的时域变化和相关性来确定各自像素位置的“运动”。研究光流场的目的就是为了从图片序列中近似得到不能直接得到的运动场。

 

光流法的前提假设:

(1)相邻帧之间的亮度恒定;

(2)相邻视频帧的取帧时间连续,或者,相邻帧之间物体的运动比较“微小”;

(3)保持空间一致性;即,同一子图像的像素点具有相同的运动

 

这里有两个概念需要解释:

运动场,其实就是物体在三维真实世界中的运动;

光流场,是运动场在二维图像平面上的投影。

 

如上图所示,H中的像素点(x,y)在I中的移动到了(x+u,y+v)的位置,偏移量为(u,v)。

 

光流法用于目标检测的原理:给图像中的每个像素点赋予一个速度矢量,这样就形成了一个运动矢量场。在某一特定时刻,图像上的点与三维物体上的点一一对应,这种对应关系可以通过投影来计算得到。根据各个像素点的速度矢量特征,可以对图像进行动态分析。如果图像中没有运动目标,则光流矢量在整个图像区域是连续变化的。当图像中有运动物体时,目标和背景存在着相对运动。运动物体所形成的速度矢量必然和背景的速度矢量有所不同,如此便可以计算出运动物体的位置。需要提醒的是,利用光流法进行运动物体检测时,计算量较大,无法保证实时性和实用性。

光流法用于目标跟踪的原理:

(1)对一个连续的视频帧序列进行处理;

(2)针对每一个视频序列,利用一定的目标检测方法,检测可能出现的前景目标;

(3)如果某一帧出现了前景目标,找到其具有代表性的关键特征点(可以随机产生,也可以利用角点来做特征点);

(4)对之后的任意两个相邻视频帧而言,寻找上一帧中出现的关键特征点在当前帧中的最佳位置,从而得到前景目标在当前帧中的位置坐标;

(5)如此迭代进行,便可实现目标的跟踪;

16进制颜色代码(完全)

红色和粉红色,以及它们的16进制代码。

#990033 #CC6699 #FF6699 #FF3366 #993366 #CC0066 #CC0033 #FF0066 #FF0033 ..#CC3399..
#FF3399 #FF9999 #FF99CC #FF0099 #CC3366 #FF66CC #FF33CC #FFCCFF #FF99FF #FF00CC

紫红色,以及它们的16进制代码。

#FF66FF #CC33CC #CC00FF #FF33FF #CC99FF #9900CC #FF00FF #CC66FF #990099 #CC0099
#CC33FF #CC99CC #990066 #993399 #CC66CC #CC00CC #663366
蓝色,以及它们的16进制代码。
#660099 #666FF #000CC #9933CC #666699 #660066 #333366 #0066CC #9900FF #333399
#99CCFF #9933FF #330099 #6699FF #9966CC #3300CC #003366 #330033 #3300FF #6699CC
#663399 #3333FF #006699 #6633CC #3333CC #3399CC #6600CC #0066FF #0099CC #9966FF
#0033FF #66CCFF #330066 #3366FF #3399FF #6600FF #3366CC #003399 #6633FF #000066
#0099FF #CCCCFF #000033 #33CCFF #9999FF #0000FF #00CCFF #9999CC #000099 #6666CC
#0033CC
黄色、褐色、玫瑰色和橙色,以及它们的16进制代码。
#FFFFCC #FFCC00 #CC99090 #663300 #FF6600 #663333 #CC6666 #FF6666 #FF0000 #FFFF99
#FFCC66 #FF9900 #FF9966 #CC3300 #996666 #FFCCCC #660000 #FF3300 #FF6666 #FFCC33
#CC6600 #FF6633 #996633 #CC9999 #FF3333 #990000 #CC9966 #FFFF33 #CC9933 #993300
#FF9933 #330000 #993333 #CC3333 #CC0000 #FFCC99 #FFFF00 #996600 #CC6633
绿色,以及它们的16进制代码。
#99FFFF #33CCCC #00CC99 #99FF99 #009966 #33FF33 #33FF00 #99CC33 #CCC33 #66FFFF
#66CCCC #66FFCC #66FF66 #009933 #00CC33 #66FF00 #336600 #33300 #33FFFF #339999
#99FFCC #339933 #33FF66 #33CC33 #99FF00 #669900 #666600 #00FFFF #336666 #00FF99
#99CC99 #00FF66 #66FF33 #66CC00 #99CC00 #999933 #00CCCC #006666 #339966 #66FF99
#CCFFCC #00FF00 #00CC00 #CCFF66 #CCCC66 #009999 #003333 #006633 #33FF99 #CCFF99
#66CC33 #33CC00 #CCFF33 #666633 #669999 #00FFCC #336633 #33CC66 #99FF66 #006600
#339900 #CCFF00 #999966 #99CCCC #33FFCC #669966 #00CC66 #99FF33 #003300 #99CC66
#999900 #CCCC99 #CCFFFF #33CC99 #66CC66 #66CC99 #00FF33 #009900 #669900 #669933
#CCCC00
白色、灰色和黑色,以及它们的16进制代码。
#FFFFF #CCCCCC #999999 #666666 #333333 #000000
16色和它们的16进制代码。
Aqua Black Fuchsia Gray Gree Lime Maroon Navy Olive Purple
Red Silver Teal White Yellow Blue


当你要给你的网页添加颜色时,有时,你能够直接使用该颜色的名称,但是大多情况下,你只能使用十六进制代码来使用这些颜色。(浏览器能够理解这些代码。)
为了方便你去使用这些代码,我们制作了这个列表,右边是颜色,左边是十六进制代码。
Hex Code Color
#FFFFFF
#FFFFCC
#FFFF99
#FFFF66
#FFFF33
#FFFF00
#FFCCFF
#FFCCCC
#FFCC99
#FFCC66
#FFCC33
#FFCC00
#FF99FF
#FF99CC
#FF9999
#FF9966
#FF9933
#FF9900
#FF66FF
#FF66CC
#FF6699
#FF6666
#FF6633
#FF6600
#FF33FF
#FF33CC
#FF3399
#FF3366
#FF3333
#FF3300
#FF00FF
#FF00CC
#FF0099
#FF0066
#FF0033
#FF0000
Hex Code Color
#66FFFF
#66FFCC
#66FF99
#66FF66
#66FF33
#66FF00
#66CCFF
#66CCCC
#66CC99
#66CC66
#66CC33
#66CC00
#6699FF
#6699CC
#669999
#669966
#669933
#669900
#6666FF
#6666CC
#666699
#666666
#666633
#666600
#6633FF
#6633CC
#663399
#663366
#663333
#663300
#6600FF
#6600CC
#660099
#660066
#660033
#660000
Hex Code Color
#CCFFFF
#CCFFCC
#CCFF99
#CCFF66
#CCFF33
#CCFF00
#CCCCFF
#CCCCCC
#CCCC99
#CCCC66
#CCCC33
#CCCC00
#CC99FF
#CC99CC
#CC9999
#CC9966
#CC9933
#CC9900
#CC66FF
#CC66CC
#CC6699
#CC6666
#CC6633
#CC6600
#CC33FF
#CC33CC
#CC3399
#CC3366
#CC3333
#CC3300
#CC00FF
#CC00CC
#CC0099
#CC0066
#CC0033
#CC0000
Hex Code Color
#33FFFF
#33FFCC
#33FF99
#33FF66
#33FF33
#33FF00
#33CCFF
#33CCCC
#33CC99
#33CC66
#33CC33
#33CC00
#3399FF
#3399CC
#339999
#339966
#339933
#339900
#3366FF
#3366CC
#336699
#336666
#336633
#336600
#3333FF
#3333CC
#333399
#333366
#333333
#333300
#3300FF
#3300CC
#330099
#330066
#330033
#330000
Hex Code Color
#99FFFF
#99FFCC
#99FF99
#99FF66
#99FF33
#99FF00
#99CCFF
#99CCCC
#99CC99
#99CC66
#99CC33
#99CC00
#9999FF
#9999CC
#999999
#999966
#999933
#999900
#9966FF
#9966CC
#996699
#996666
#996633
#996600
#9933FF
#9933CC
#993399
#993366
#993333
#993300
#9900FF
#9900CC
#990099
#990066
#990033
#990000
Hex Code Color
#00FFFF
#00FFCC
#00FF99
#00FF66
#00FF33
#00FF00
#00CCFF
#00CCCC
#00CC99
#00CC66
#00CC33
#00CC00
#0099FF
#0099CC
#009999
#009966
#009933
#009900
#0066FF
#0066CC
#006699
#006666
#006633
#006600
#0033FF
#0033CC
#003399
#003366
#003333
#003300
#0000FF
#0000CC
#000099
#000066
#000033
#000000

各向同性/各向异性-滤波算子

说一个滤波算子(滤波模版)是各向同性的或者各向异性的,表示的是什么意思呢?

首先了解一下各向同性,各向异性的含义:

指物体的物理、化学等方面的性质不会因方向的不同而有所变化的特性,即某一物体在不同的方向所测得的性能数值完全相同

各向同性

亦称均质性。物理性质不随量度方向变化的特性。即沿物体不同方向所测得的性能,显示出同样的数值。如所有的气体、液体(液晶除外)以及非晶质物体都显示各向同性。例如,金属和岩石虽然没有规则的几何外形,各方向的物理性质也都相同,但因为它们是由许多晶粒构成的,实质上它们是晶体,也具有一定的熔点。由于晶粒在空间方位上排列是无规则的,所以金属的整体表现出各向同性。

各向同性是对于各向异性而言的,从微观角度来分析,对于单晶体,它是由一个晶粒组成,其晶格位向完全一致,(下图a)但由于其不同晶面和晶向上的原子排列情况不同,故其在不同方向上的性能不同,称之为各向异性。对于多晶体,它是由许多晶粒组成,每个晶粒的内部,晶格位向完全一致,而各个晶粒之间,彼此的位向各不相同,(下图b),其性能是各个晶粒性能的统计平均值,故其在各个方向上的性能大致相同,称之为各向同性。例如纯铁的的弹性模量,若为单晶体,其沿晶胞空间对角线方向的数值是290000MPa,而沿晶胞棱边方向的数值135000MPa;若为多晶体,无论从哪个部位取样所测得的数值均在210000MPa左右。

 

那么基于上面解释,把应用场景移植到图像处理/计算机视觉的领域来:

各向同性的滤波器也就是对与图像的横向和纵向作用强度相同的滤波器,例如高斯滤波器等

而各向异性滤波器则就表示对于图像的横向和纵向作用强度不同的滤波器,如下:

0 0 0               0 1 0

1 0 -1              0 0 0

0 0 0      和    0 -1 0  等 再如,给高斯滤波器加上一个正弦调制,那么就如同Gabor小波一样给高斯函数加上了方向性,对于图像的各个方向的作用强度不再相同,就变成了各向异性了。

 

阐述个人观点,如有错误,请留言斧正!谢谢!