openmv 图像一维数组

跳舞语法 (Dancing Syntax)

介绍 (Introduction)

About a month into the Flatiron School Bootcamp, a friend of mine gave me this advice:

进入Flatiron学校训练营大约一个月后,我的一个朋友给了我以下建议:

“Learning to read code will help you become a better programmer.”

“学习阅读代码将帮助您成为更好的程序员。”

At first, I heard that and thought, “Yeah!” It made sense to me, but while working on labs, I came across some code that I struggled to make sense of:

一开始,我听到了,然后想:“是的!” 这对我来说很有意义,但是在实验室工作时,我遇到了一些难以理解的代码:

Unfamiliar code alert! What’s going on here? What’s a Pixel Matrix?
陌生的代码警报! 这里发生了什么? 什么是像素矩阵?

A function called rgbToHex, a pixel matrix, an npm package, some loops, hex codes, and a lot to do with arrays. Fascinating! At a high-level, data.js is loading a file into an npm package called get-pixels, that package is processing the data, which results in a nested array of hex codes. My brain was in overload! I wanted to understand, but so much was unfamiliar. Could I read my way into figuring this out?

名为rgbToHex的函数,像素矩阵,npm包,一些循环,十六进制代码以及与数组有很大关系的函数。 迷人! 从高层次上讲,data.js正在将文件加载到名为get-pixels的npm包中,该包正在处理数据,这导致嵌套的十六进制代码数组。 我的大脑超负荷了! 我想了解,但是非常陌生。 我可以读一下解决这个问题的方法吗?

After all, I didn’t need to try to understand it, it’s some helper code for the lab. The purpose of the lab was to learn how information flowed between components in React. The challenge was to finish implementing the functionality for an in-browser pixel drawing app.

毕竟,我不需要尝试去理解它,它是实验室的一些帮助代码。 实验室的目的是了解React中组件之间信息的流动方式。 挑战在于完成为浏览器内像素绘图应用程序实现功能。

Painting the learn symbol!
画学习符号!

Okay! Let’s move on, BUT WAIT… What about that code from earlier?

好的! 让我们继续前进,但请等待...早先的代码呢?

At first glance, I didn’t know what was going on, but transforming images into pixel data and rendering those pixels as divs seemed pretty bomb.com. With my friend’s advice in mind, Let’s pick it apart!

乍一看,我不知道发生了什么,但是将图像转换为像素数据并以div形式渲染这些像素似乎是bomb.com。 考虑到我朋友的建议,让我们把它分开!

获取像素 (Get-Pixels)

Let’s start with the npm package! After taking a look at the documentation, I found out that Get-Pixels reads the pixel data from an image and returns the result as a ndarray (Nth-Dimensional Array).

让我们从npm包开始! 查看文档后,我发现Get-Pixels从图像中读取像素数据,并将结果作为ndarray(第N维数组)返回

Specifically, it:

具体来说,它:

“Returns An ndarray of pixels in raster order having shape equal to [width, height, channels].”

“以栅格顺序返回形状等于[宽度,高度,通道]的像素nd阵列。”

Two things are new to me here: Raster order and ndarrays.After a quick search, I found out that Raster order describes the ordering of an image divided into rows of its pixels called scan lines.

这里有两件事对我来说是新的: 栅格顺序ndarrays。 快速搜索后,我发现光栅顺序描述了将图像分成像素行(称为扫描线)的顺序。

In this context, get-pixels takes in an image, reads the pixel data from left-right and top-bottom, and then records that image data to a ndarray. With a console.log of the pixels returned, we can begin to see a piece of the multidimensional array and some information about it. More things to learn about!

在这种情况下,get-pixels会摄取图像,从左至右和从上至下读取像素数据,然后将该图像数据记录到ndarray。 使用console.log记录的返回像素,我们可以开始查看多维数组的一部分以及有关它的一些信息。 还有更多要学习的东西!

// console.log('got pixels', pixels)         line 9 in data.js//=> got pixels View3duint8 {  data: Uint8Array(2500) [    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255,    ... 2400 more items  ],  shape: [ 25, 25, 4 ],  stride: [ 4, 100, 1 ],  offset: 0}

Uint8 is shorthand for unsigned 8bit integer. That means (2⁸)-1, or 255. It’s commonly known as the value range for RGB color values: Red, Green, and Blue channels. Each channel has a value between 0–255, representing the intensity of its corresponding color. There’s also that sometimes hard to see alpha channel too, but that’s for opacity!

Uint8是无符号8位整数的简写。 这表示(2⁸)-1,即255。通常称为RGB颜色值的值范围:红色,绿色和蓝色通道。 每个通道的值在0到255之间,代表其相应颜色的强度。 有时也很难看到Alpha通道,但这是为了不透明!

Laughing cause I made a joke.
因为我开了个玩笑。

We also get the shape of this ndarray. That word can kiss it’s bold status goodbye because we’re about to learn about them!

我们也得到了这个ndarray的形状 这个词可以亲吻它的大胆地位,因为我们将要了解它们!

阵列 (Ndarray)

An Nth-Dimensional Array is a multidimensional array of items of the same type and size. The number of dimensions and items in the array are defined by the shape, positive integers that specify the sizes of each dimension. They are great for representing images, audio, matrices, strings, and other dimensional data.

第N维数组是相同类型和大小的项目的多维数组。 数组中的维数和项目数由shape定义,正整数指定每个维的大小。 它们非常适合表示图像,音频,矩阵,字符串和其他维度数据。

Ndarrays are themselves a part of a Python package called NumPy — a library of code for scientific computing in Python. The library provides a variety of data types for integers and floats, the multidimensional array itself, and methods to operate, transform, and iterate on the ndarrays.

Ndarrays本身就是称为NumPy的Python程序包的一部分, NumPy是用于Python中科学计算的代码库。 该库为整数和浮点数提供了多种数据类型,多维数组本身以及在ndarray上进行操作,转换和迭代的方法。

Get-Pixels uses sciJS, a Javascript library that allows you to implement similar functionality to NumPy’s scientific computing.

Get-Pixels使用sciJS (一个Javascript库),该库可让您实现与NumPy的科学计算类似的功能。

Cool! Let’s visualize this. A Rubix Cube!

凉! 让我们将其可视化。 魔方!

A 3D array with a shape of (3, 3, 3), a Rubik’s cube!
形状为(3、3、3)的3D阵列,即魔方!

Now in code, it is an array of 6 3x3 arrays:

现在在代码中,它是6个3x3数组的数组:

REPL Showing a representation of a Solved Rubiks Cube in Javascript code
REPL用Javascript代码显示已解决的Rubiks多维数据集的表示形式

Okay, that was a ton of googling, drawing, and REPL fun y’all. Now we know a bit more about nd-arrays. With all of the knowledge gathered, we can figure out how data.js does all its magic.

好的,这是很多Google搜寻,绘图和REPL的乐趣。 现在,我们对nd数组有了更多了解。 收集了所有知识之后,我们可以弄清楚data.js如何发挥其全部魔力。

破解密码 (Cracking the Code)

Going back to that original console.log, we now know that this is a 3-Dimensional Array of RGB color values, It has a shape equal to its width, height, and channels. In this case [25W, 25H, 4Channels(RGBa)

回到原始的console.log,我们现在知道这是RGB颜色值的3维数组,其形状等于其宽度,高度和通道。 在这种情况下[25W,25H,4通道(RGBa)

// console.log('got pixels', pixels)         line 9 in data.js//=> got pixels View3duint8 {  data: Uint8Array(2500) [    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,    255, 255, 255, 255,    ... 2400 more items  ],  shape: [ 25, 25, 4 ],  stride: [ 4, 100, 1 ],  offset: 0
Function to convert RGB values into RR GG BB hex values
将RGB值转换为RR GG BB十六进制值的功能

We see the first function from the file takes in a single RGB value and converts that value to a hexadecimal string. If that string is only one character long, add a 0 to it.

我们看到文件中的第一个函数采用单个RGB值,并将该值转换为十六进制字符串。 如果该字符串只有一个字符长,请向其添加一个0。

Number(255).toString(16) //=> "ff" or 'red' in hex.

Now we can look at those fancy loops and derive what is happening:

现在我们可以看一下这些奇特的循环并得出正在发生的事情:

Traversing the ndarray to convert each pixel value into a full hex color code.
遍历ndarray将每个像素值转换为完整的十六进制颜色代码。

First, we traverse the height, then the width of the 3Darray with for loops. Each loop is set to terminate before it’s pointer gets to our width and height values from the shape.

首先,我们遍历高度,然后遍历3Darray的宽度并使用for循环。 每个循环都设置为在指针从形状到达我们的宽度和高度值之前终止。

let row = ['#FFF', '#FFF', '#FFF', '#FFF', '#FFF', '#FFF', '#FFF', '#FFF', '#FFF', '#FFF']

Declare a variable and assign it an array of white color hex codes. This array is forming a single line of white hex color pixels to begin our raster scan. We’re building a frame for our picture.

声明一个变量,并为其分配白色十六进制代码数组。 该阵列形成一行白色十六进制彩色像素,以开始我们的光栅扫描。 我们正在为图片建立框架。

The Hex codes are built here, using rgbToHex as a callback.
使用rgbToHex作为回调在此处构建十六进制代码。

If we look closely at this line, you see that we are targeting each pixel element across the RGB channels, converting the values into hex code pieces, and including an octothorpe in the beginning. Then we push those codes onto the end of the row array.

如果我们仔细观察这条线,您会发现我们以RGB通道中的每个像素元素为目标,将这些值转换为十六进制代码段,并在开始处包含一个八面体。 然后,将这些代码推送到行数组的末尾。

Traversing the ndarray to convert each pixel value into a full hex color code.
遍历ndarray将每个像素值转换为完整的十六进制颜色代码。

Line 7–8: Once the loop reaches the end, push on another line of white hex colors, push the completed row into a container array, then start again at the next step down in our height loop. We continue building rows like this, mimicking the pattern of the raster scan building left-right, top-bottom.

第7-8行:循环结束时,推另一行白色十六进制颜色,将完成的行推入容器数组,然后在高度循环中的下一个步骤再次开始。 我们继续像这样构建行,以模仿从左到右,从上到下的光栅扫描模式

One sort of optimization that we could make is making the loops terminate dynamically based on the shape value we have access to.

我们可以进行的一种优化是使循环根据我们可以访问的形状值动态终止。

for (let y = 0; y < 25; y++)change to:for (let y = 0; y < pixels.shape[0]; y++)for (let x = 0; x < 25; x++)change to:for (let x = 0; x < pixels.shape[1]; x++)

I said this is a -sort of- optimization because it is playing with fire. It’s great if you don’t precisely know the pixel dimensions of your image, it’s also terrible if your image has large pixel dimensions. Though, I’d argue that you would rarely use this method of processing a large image anyways, so have fun!

我说这是一种优化,因为它在玩火。 如果您不完全了解图像的像素尺寸,那就太好了,如果图像的像素尺寸很大,那也很糟糕。 不过,我认为您还是很少会使用这种处理大图像的方法,所以请尽情享受!

The completed array of hex values looks like this:

十六进制值的完整数组如下所示:

The Learn logo represented as a multidimensional array of hex color codes in Raster scan order.
学习徽标表示为按光栅扫描顺序的十六进制颜色代码的多维数组。

结论 (Conclusion)

Whew! That’s long, I went ahead and processed a different image to bring you joy. There’s more code going on behind the scenes, but since you know about Raster order, I’m sure you can figure it out!

ew! 很长一段时间,我继续进行处理,并制作了不同的图像,以带给您快乐。 幕后还有更多代码在做,但是由于您了解Raster的顺序,因此我敢肯定您会发现的!

So what did we learn from all this? We learned about:

那么我们从这一切中学到了什么? 我们了解到:

  1. multidimensional arrays多维数组
  2. Two npm packages: get-pixels and scijs/ndarray两个npm软件包:get-pixels和scijs / ndarray
  3. Raster scan order栅格扫描顺序
  4. A starting point for solving a Rubik’s cube algorithmically从算法上解决魔方的起点
  5. Creating fun things with divs.使用div创建有趣的事物。
  6. Reading and dissecting code that is unfamiliar to you is a great way to stretch your brain and get exposed to new information.

    整理和剖析您不熟悉的代码是扩展您的大脑并接触新信息的好方法。

Enjoy!

请享用!

资料来源: (Sources:)

  1. npmJS: Get-Pixels

    npmJS:获取像素

  2. Wikipedia: Raster Scan

    维基百科:光栅扫描

  3. Github: sciJs/ndarray library

    Github:sciJs / ndarray库

  4. W3Resource: NumPy

    W3资源:NumPy

  5. W3Resource: ndarray

    W3资源:ndarray

  6. W3Schools: Hex Colors

    W3Schools:十六进制颜色

  7. W3Schools: RGB Colors

    W3Schools:RGB颜色

翻译自: https://medium.com/weekly-webtips/demystifying-unfamiliar-code-ndarrays-and-get-pixels-34e0db4ac7ce

openmv 图像一维数组


http://www.taodudu.cc/news/show-5047445.html

相关文章:

  • ggplot绘制柱状图 python_ggplot2堆积柱形图笔记
  • 堆积柱形图
  • appium自动化绘制锁屏图案
  • android 设置锁屏密码,密码锁屏是什么?怎么设置密码锁屏
  • Android11.0(R) 预留清空锁屏密码接口
  • Android 添加屏幕锁和移除锁屏密码
  • ZOJ 3861 安卓图案锁屏 DFS深搜
  • 根据屏幕手指划痕解图案锁屏
  • Android6.0 无法锁屏
  • 手机图案锁屏小记
  • php 手势验证码,通过微信小程序如何实现手势图案锁屏
  • 破解安卓图案锁屏密码
  • 微信小程序----手势图案锁屏
  • 天翼云对象存储android实现,天翼云对象存储
  • 天翼云盘php插件,天翼云盘直接下载
  • 天翼云对象存储android实现,使用天翼云对象存储服务
  • C# 如何使用倒计时
  • java怎样做倒计时,Java 中怎么实现倒计时
  • 前端从服务器获取时间进行倒计时
  • vue 5s倒计时
  • js开发实例 —— 发送短信验证码倒计时5S钟之后自动跳转页面
  • c语言输出函数定义,c语言输入输出函数的定义.doc
  • 浅析修理厂的5s管理内容及效用
  • YoLoV5学习(5)-- Train.py 程序文件与yolov5s模型文件讲解
  • YOLOv5白皮书-第Y3周:yolov5s.yaml文件解读
  • 深度学习Week14-yolov5s.yaml文件解读(YOLOv5)
  • yolov5s 预训练模型_GitHub上YOLOv5开源代码的训练数据定义
  • 【玩转yolov5】使用TensorRT C++ API搭建yolov5s-v4.0网络结构(1)
  • 定义struct结构体数组
  • yolov5s模型剪枝详细过程(v6.0)

openmv 图像一维数组_第N维数组和图像处理。相关推荐

  1. python循环生成二维数组_嵌套循环二维数组的计算与构造 - python

    我正在尝试使用Python进行计算.我想产生一个带有嵌套循环的20 * 20数组.我不知道我的方向是否正确,但这是我的代码: w = 1.5 m = 0.556 E = np.linspace(15. ...

  2. 【C 语言】数组 ( 一维数组形参退化 | 二维数组形参退化 | 函数形参等价关系 )

    文章目录 一.一维数组形参退化 二.二维数组形参退化 三.数组形参等价关系 一.一维数组形参退化 C 中将 一维数组 作为参数 , 传递到函数中 , 该 一维数组 会退化为 指针 ; 将 int ar ...

  3. 【C 语言】数组 ( 验证二维数组内存是线性的 | 打印二维数组 | 以一维数组方式打印二维数组 | 打印二维数组值和地址 )

    文章目录 一.验证二维数组内存是线性的 1.打印二维数组 2.以一维数组方式打印二维数组 3.打印二维数组值和地址 二.完整代码示例 一.验证二维数组内存是线性的 验证二维数组内存是线性的 : 验证方 ...

  4. arrays中copyof复制两个数组_数组,及二维数组

    1.1 命令行参数(C) 在程序运行过程中,可以向应用程序传递一些参数,这些参数称为命名行参数. public 命令行参数以字符串的形式传入args数组中.可以一次传递0-多个参数,以空格分割. 如果 ...

  5. 一维数组名与二维数组名的关联

    1.一维数组名与二维数组名的关系之于普通指针与数组指针的关系 2.首先数组名编译器会隐式变换看做指针常量,因此a[i]与a+i是等价的. a+i返回由i指定的行地址,假设元素类型为char,则行指针类 ...

  6. 2022. 将一维数组转变成二维数组

    2022. 将一维数组转变成二维数组 给你一个下标从 0 开始的一维整数数组 original 和两个整数 m 和 n .你需要使用 original 中 所有 元素创建一个 m 行 n 列的二维数组 ...

  7. 一维数组转化为二维数组(java)

    由于经常在使用矩阵进行计算时,会首先将一维数组转为二维数组.因此,在这里记录一下,也希望对他人有帮助. package deal; /** author:合肥工业大学 管院学院 钱洋 *1563178 ...

  8. PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?

    如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) {echo '是一维数组'; ...

  9. 二维数组 赋值_数组,及二维数组

    1.1 命令行参数(C) 在程序运行过程中,可以向应用程序传递一些参数,这些参数称为命名行参数. public class Test01{public static void main(String[ ...

最新文章

  1. 2020考研 统考英语 核心词汇:社会生活(1)(苗嘉)
  2. Java里边什么是值传递和引用传递?两个有什么区别
  3. 【学习笔记】CO内部订单
  4. C语言运算符优先级列表
  5. matlab错误使用builtin,MATLAB环境下运行MATLAB函数时发生异常
  6. vue再次入手(数据传递①)
  7. oracle秒级查询,oracle 中查询超过10秒以上的sql语句(性能优化)
  8. 拓端tecdat|R语言使用马尔可夫链Markov Chain, MC来模拟抵押违约
  9. PPT图片虚化效果要怎样实现?
  10. AP与CP介绍【转】
  11. 运行java程序需要的工具软件的目录,运行Java程序需要的工具软件所在的目录是A.JDK的bin目录B.JDK的demo目录C.JDK的lib目录D.JDKR的j...
  12. Spring框架的基本使用
  13. 市面上微型计算机的主频,目前市面上最大屏幕的手机,你知道是哪款吗?
  14. Python中除法取整以及求余数(模)的方式
  15. 利用Pandas拆分Excel的单元格为多行并保留其他行的数据
  16. 揭秘!用标准Go语言能写脚本吗?
  17. 金三银四马上到了,找工作需要准备什么?
  18. MDK编译过程和文件详解
  19. 阿里云点播录制,上传,播放使用说明及遇到的坑
  20. Webix UI JavaScript 10.0.6 Crack

热门文章

  1. Linux通过主机名免密码建立互信
  2. 备战考研是否需要ipad?
  3. 免费的大数据分析可视化网站-司南智图
  4. 组合数学-离散数学重点摘记
  5. 水文监测系统 水文遥测终端机 遥测终端机-助力母亲河平稳度汛
  6. 数据分析基础教程 ( 1 )
  7. 产品分析时的自攻螺丝注意事项:主要对应塑胶件上面的自攻螺丝
  8. 山东自考c语言程序设计停考了吗,2019年10月山东自考停考二十多个专业 自考专业停考怎么办...
  9. python 录音vad_2020-02-25 python使用ffmpeg、speech-vad-demo、百度语音识别生成字幕
  10. 电源硬件设计----电源基础知识(1)