图像的属性

Pixel

Pixel 像素点

每张图片都是由一个像素点矩阵构成的

坐标从左上角起始, 横向的是行数,标记为 x, 纵向的为列数标记为y

如果要访问下图红色圈圈标识的pixel, 我们如何才能找到他?

从左往右数, 在序号为5的列

从上往下数,在序号为11的行

所以该点的坐标为(255, 0, 0)

image_pixel_value

像素点的值

pixel格式随着图片的格式不同而不同

如果为灰度图, 返回的是一个整数, 范围在 0-255

如果是RGB图片, 返回的就是一个长度为3的元组 (r, g, b), 例如 (90, 90, 90)

读取单一像素

RGB图片的Pixel

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.

    print("Pixel Value")
    print(img.get_pixel(100, 100))

样例输出

Pixel Value
(8, 8, 8)
Pixel Value
(16, 4, 8)
Pixel Value
(8, 4, 8)

获取灰度图的Pixel

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.

    print("Pixel Value")
    print(img.get_pixel(100, 100))

样例输出

Pixel Value
10
Pixel Value
11
Pixel Value
10

设定单一像素

为了让效果更明显, 我们将像素设置为80* 60

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQQVGA) # QQQVGA : 80 * 60
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.


while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.A
    img.set_pixel(20, 20, (255, 255, 255))

效果

set_pixel

DIY绘制矩形

我的天, 如果你能控制一个pixel, 也就是说, 你也拥有了控制整个世界的能力, 一点都不夸张。 那么接下来我们自己造一个绘制矩形的程序

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQQVGA) # QQQVGA : 80 * 60
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.


# 绘制水平线
def draw_hori_line(img, x0, x1, y, color):
    for x in range(x0, x1):
        img.set_pixel(x, y, color)


# 绘制竖直线
def draw_vec_line(img, x, y0, y1, color):
    for y in range(y0, y1):
        img.set_pixel(x, y, color)

# 绘制矩形
def draw_rect(img, x, y, w, h, color):
A
    draw_hori_line(img, x, x+w, y, color)
    draw_hori_line(img, x, x+w, y+h, color)
    draw_vec_line(img, x, y, y+h, color)
    draw_vec_line(img, x+w, y, y+h, color)

white = (255, 255, 255)

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.A
    #img.set_pixel(20, 20, (255, 255, 255))
    draw_rect(img, 10, 10, 30, 20, white)

效果

大功告成

draw_rectangle

思考题

  • 如何绘制任意斜率的直线, 并且可以设置不同的宽度?
  • 如何绘制圆形?

图像的整体属性

高度 height

img.width()

宽度 width

img.width()

图像的大小

img.size()

size的取值其实是width * height, 就是说一共有多少个pixels

图像格式

img.format()

图像格式一共有两种

  • sensor.GRAYSCALE: 8-bits per pixel.
  • sensor.RGB565: 16-bits per pixel.

函数返回数值为整数 取值范围是0 / 1

format 0 : 灰度图, 每个pixel占8个位

format 1 : RGB图, 每个pixel占16个位

老夫知道你此时已心生疑虑, 对于grayscale是单个数值,占8个位

rgb应该是其三倍啊, 因为其存储了三个值,但是为啥是16个位呢

详情参见 RGB565详解 未写

实验代码

import sensor, image, time

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    print("IMG Format")
    print(img.format())
    print("IMG Width")
    print(img.width())
    print("IMG Height")
    print(img.height())
    print("IMG SIZE")
    print(img.size())

样例输出

IMG Format
1
IMG Width
320
IMG Height
240
IMG SIZE
153600
Copyright 杭州云江科技有限公司 2017 all right reserved,powered by Gitbook该文件修订时间: 2019-04-06 01:27:51

results matching ""

    No results matching ""