Background image in react-three - three.js

I need to set static background image on scene with image formats like .png, .jpg, etc. May be someone know how to do it? I tryed lot of variants, mut still can't fiend correct solution.

Assuming that your background is a skybox with 6 separate files, you can load images with CubeTextureLoader and set it as scene.background:
const path = 'textures/cube/your-skybox-directory/';
const format = '.jpg';
const urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
const reflectionCube = new THREE.CubeTextureLoader().load(urls);
scene = new THREE.Scene();
scene.background = reflectionCube;
Based on examples from: https://threejs.org/examples/?q=cube#webgl_materials_cubemap

Related

Saved an image sequence as a Tiff file but cannot view or open it outside of Matlab

Im trying to save an image sequence as a Tiff file to be later used in another program. I read each image in, applied color thresholding to get the mask, and then append that to my 3d array that I initialized earlier.
I read up on the Tiff class needed to write Tiff images and I think I set all the tags properly, but although the file gets created, I cannot open it in an image viewer. I get the error:
Invalid or Unsupported Tif file
There are 290 images in folder, so tiff file will have 290 planes. Below is how I am saving the tiff file:
clear;
path = 'D:\complete stack';
img_list = dir(fullfile(path, '*.tif'));
img = imread(fullfile(img_list(1).folder, img_list(1).name));
tiff_array = zeros(size(img, 1), size(img, 2), numel(img_list), 'uint8');
clear img;
for i = 1:numel(img_list)
img = imread(fullfile(img_list(i).folder, img_list(i).name));
[bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);
thanks
edit:
A more reproducible, more minimal example:
clear;
img = ones(750, 750, 'uint8');
tiff_array = zeros(size(img, 1), size(img, 2), 290, 'uint8');
clear img;
for i = 1:290
bw = ones(750, 750, 'uint8');
% [bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);

How to display a locally selected image in three.js?

I always get a cross-origin-error. how can I circumvent this, or better yet, fix it?
I need the user to be able to pic a local image and let it be displayed in a PlaneGeometry.
my code:
this.reader.readAsDataURL(file);
this.reader.onloadend = function( ev ) {
var file = ev.target.result,
//geometry = new THREE.CubeGeometry(1, 1, 0.1),
geometry = new THREE.PlaneGeometry(1, 1, 1, 2),
texture = THREE.ImageUtils.loadTexture(file),
material = new THREE.MeshBasicMaterial({ map: texture }),
mesh = new THREE.Mesh(geometry, material);
mesh.name = "Marker" + mesh.id;
mesh.rotation.x = 90 * ( Math.PI / 180 );
Editor.addObject(mesh);
console.log(Editor.scene.children);
SignalHub.signals.updateScene.dispatch();
SignalHub.signals.markerAdded.dispatch();
The geometry shows up but it's blank!
Option 1
Use an img.
var image = document.createElement( 'img' );
image.src = dataurl;
var texture = new THREE.Texture( image );
texture.needsUpdate = true;
See here.
Option 2
Where is the html server from? If it is a local htm file, then you could instead serve it from a web server on the local machine (e.g. IIS / apache).
Whether the server is local or remote, you can upload the image to your web server when they have selected it and use a URL to retrieve it from the web server. This will satisfy the cross-origin policies.
Option 3
Convert the image into Base64 text
Store it in an external Javascript file
Link it to your project page
Load it into your texture
See here (SO answer) and here (details off-site).
Option 4
Create a shortcut for the browser such as:
c:// ... /chrome.exe --allow-file-access-from-files
Option 5
Use the .crossOrigin = '' property. It was not working, but I think it is now fixed. See here and here.
I made this example: http://develoteca.com/EjerciosOnline/Exampleplane/, you can load a local image
Greetings.

draw an image in node.js

I'd like to create an application using node.js which creates an image. In the image I'd like to programmatically draw circles, lines or any function f(x) (well I could draw that function by adding points at some coordinates). I'd like to know which node.js modules I should use, or if there is something created for this.
In other words I need to draw a given mathematical functions and export it to an image file.
Thanks.
Have a look at node-canvas which is a canvas implementation for Node.js
Source code example:
var Canvas = require('canvas')
, canvas = new Canvas(200,200)
, ctx = canvas.getContext('2d');
ctx.font = '30px Impact';
ctx.rotate(.1);
ctx.fillText("Awesome!", 50, 100);
var te = ctx.measureText('Awesome!');
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.beginPath();
ctx.lineTo(50, 102);
ctx.lineTo(50 + te.width, 102);
ctx.stroke();
console.log('<img src="' + canvas.toDataURL() + '" />');

How to tell if an image is 90% black?

I have never done image processing before.
I now need to go through many jpeg images from a camera to discard those very dark (almost black) images.
Are there free libraries (.NET) that I can use? Thanks.
Aforge is a great image processing library. Specifically the Aforge.Imaging assembly.
You could try to apply a threshold filter, and use an area or blob operator and do your comparisons from there.
I needed to do the same thing. I came up with this solution to flag mostly black images. It works like a charm. You could enhance it to delete or move the file.
// set limit
const double limit = 90;
foreach (var img in Directory.EnumerateFiles(#"E:\", "*.jpg", SearchOption.AllDirectories))
{
// load image
var sourceImage = (Bitmap)Image.FromFile(img);
// format image
var filteredImage = AForge.Imaging.Image.Clone(sourceImage);
// free source image
sourceImage.Dispose();
// get grayscale image
filteredImage = Grayscale.CommonAlgorithms.RMY.Apply(filteredImage);
// apply threshold filter
new Threshold().ApplyInPlace(filteredImage);
// gather statistics
var stat = new ImageStatistics(filteredImage);
var percentBlack = (1 - stat.PixelsCountWithoutBlack / (double)stat.PixelsCount) * 100;
if (percentBlack >= limit)
Console.WriteLine(img + " (" + Math.Round(percentBlack, 2) + "% Black)");
filteredImage.Dispose();
}
Console.WriteLine("Done.");
Console.ReadLine();

Printing a multi-page HTML5 canvas element

This is the first question I've ever asked on here, as normally I can find an answer that will solve all my problems. However, this week I've hit quite the wall.
I'm using a canvas on my page that's working great and can be saved as an image file. I've also created a button that will open the canvas in a new window for printing after using toDataURL. I'm using the code below:
function printable() {
var dataUrl = document.getElementById("mainCanvas").toDataURL("image/png");
var windowContent = '<!DOCTYPE html>';
windowContent += '<head><title>Print Map</title></head>';
windowContent += '<body>';
windowContent += '<img src = "' + dataUrl + '">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('', '', width = 340, height = 260);
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
}
This works great and opens the print dialogue to print my canvas image. My issue is that my image is larger than a single page and only the first page of my image prints. Is there any way to print my canvas across multiple pages, or should i just chalk this up to bad browser to printer functionality and get my users who need to print to use a different program after saving their canvas locally?
Thanks
there is a stupid solution, but definitely work.
use a script to split canvas, and put contents to several separate canvases.
use these functions:
ImageData getImageData(in float x, in float y, in float width, in float height);
void putImageData(in ImageData imagedata, in float dx, double dy, in float dirtyX Optional , in float dirtyY Optional , in float dirtyWidth Optional , in float dirtyHeight Optional );

Resources