简体   繁体   English

如何访问 p5.js canvas 上的实时草图图像?

[英]How can I access the real-time sketch images on a p5.js canvas?

Recently I am attempting to modify the source codes of this page .最近我正在尝试修改这个页面的源代码。 The underlying technique of this interactive programe is called sketch-rnn, a deep learning algorithm that can generate sketches.这个交互式程序的底层技术称为sketch-rnn,一种可以生成草图的深度学习算法。 I need to access the real time images on the canvas so that I can use convolutional neural network (CNN), and feed the image as a 2d array to the neural network so that I can further improve the programe.我需要访问 canvas 上的实时图像,以便我可以使用卷积神经网络 (CNN),并将图像作为二维数组输入神经网络,以便进一步改进程序。 Is there any p5.js function that can help me achieve that?是否有任何 p5.js function 可以帮助我实现这一目标?

It depends in what format the CNN accepts input.这取决于 CNN 接受输入的格式。

The simplest thing I can think of is using plain JavaScript (outside of p5.js) to access the <canvas /> element.我能想到的最简单的事情是使用普通的 JavaScript(在 p5.js 之外)来访问<canvas />元素。

For example this is something you can try in your browser console on the sketch_rnn_demo page:例如,您可以在 sketch_rnn_demo 页面上的浏览器控制台中尝试以下操作:

// access the default p5.js Canvas
canvasElement = document.querySelector('#defaultCanvas0')
// export the data as needed, for example encoded as a Base64 string:
canvasElement.toDataURL()

If you want to access pixels, you can via the Canvas context and getImageData() :如果要访问像素,可以通过Canvas 上下文getImageData()

//access <canvas/> context
var context = canvasElement.getContext('2d');
//access pixels:
context.getImageData(0,0,canvasElement.width,canvasElement.height);

This will return a 1D array of unsigned 8-bit integers (eg values from 0-255) in R,G,B,A order (eg pixel0R,pixel0G,pixel0B,pixel0A,pixel1R,pixel1G,pixel1B,pixel1A...etc.)这将在 R,G,B,A 顺序(例如 pixel0R,pixel0G,pixel0B,pixel0A,pixel1R,pixel1G,pixel1B,pixel1A...等.)

If you want to use p5.js instead, call loadPixels() first, then access the pixels[] array which is the same format as above.如果您想使用 p5.js,请先调用loadPixels() ,然后访问与上述格式相同的像素 []数组。

You can also use get(x,y) in p5.js which allows a 2D way to access to pixel data, however this is much slower.您还可以在 p5.js 中使用get(x,y) ,它允许以 2D 方式访问像素数据,但这要慢得多。

If you CNN takes in a 2D array you still need to create this 2D array yourself and populate it pixel values (using pixels[] or get() for example).如果你的 CNN 接收一个二维数组,你仍然需要自己创建这个二维数组并填充它的像素值(例如使用pixels[]get() )。 Be sure to double check the CNN input:请务必仔细检查 CNN 输入:

  • it is a 2D array of 32-bit integers (eg R,G,B,A or A,R,G,B as a single int (0xAARRGGBB or 0xRRGGBBAA), just RGB, etc.)它是一个 32 位整数的二维数组(例如 R,G,B,A 或 A,R,G,B 作为单个整数(0xAARRGGBB 或 0xRRGGBBAA),只是 RGB 等)
  • what resolution should the 2d array be?二维数组应该是什么分辨率? (your sketch-rnn canvas may be a different size and you might need to resize it to match what the CNN expects as an input) (您的sketch-rnn canvas 的大小可能不同,您可能需要调整它的大小以匹配 CNN 期望的输入)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM