height and width of View region in three js - three.js

I have positioned my camera at a distance of 850 (camera.position.z = 850) and camera.setlens(50) and perspective FOV as 70. How can I calculate width and height of View (I just want to draw cube so I want the width and height of View which changes from camera distance.)

Related

Calculating the width of the YOLO bounding box in pixels

I am trying to find the width of the bounding box of the output image in pixels:
In this article, it says YOLO v3 extracts coordinates and dimensions of the bounding box (line 82). YOLO returns bounding box coordinates in the form:
(centerX, centerY, width, and height)
Are these coordinates, width and height, real pixel values? Or do they perform scaling on them?
Can I print out the value of width and consider it as a real pixel value for the width of the aforementioned box?
Please note that my question is about YOLO v3.
Those are what are called normalized coordinates. To get the width in pixels you would need to multiply by the width of the images. For example if your image 640x480 than multiple the width values outputted by Yolo by the width of the image.
If the numbers in your screenshot are the width:
Dog width = .98 * 640 = 627 px
Cat width = .88 * 563 = 563 px

How to adjust a three.js perspective camera's field of view to fit a sphere?

I have a sphere centered in front of my camera with a known radius and distance from the camera. How can I adjust the camera's field of view (FOV) to exactly fit the the camera to the sphere within an arbitrary viewport size?
This answer is similar but I want to adjust the FOV instead of the camera's distance
To adjust the FOV to fit the sphere, I needed to use inverse trigonometric functions to calculate the angle from the triangle formed from the distance to the sphere and the furthest visible point on the sphere.
Triangle that will give the correct angle:
// to get the fov to fit the sphere into the camera
var vFOV = 2 * Math.asin(sphereRadius / distance);
// get the project's aspect ratio to calculate a horizontal fov
var aspect = this.width / this.height;
// more trig to calculate a horizontal fov, used to fit a sphere horizontally
var hFOV = 2 * Math.atan(Math.tan(vFOV / 2) / aspect);
This will give the answer in radians. Multiply the hFOV or vFOV to degrees fov * (180 / Math.PI) and apply to camera.fov.
I initially ran into the trap of using the wrong triangle. As this answer states
"The sphere is being clipped for the same reason that if you stand close to a large sphere you can't see its "north pole"
Don't do this; shown is the wrong triangle for a cropped view:

is there a way to set the width and height of html canvas in cm or inches?

when I set the width and height of canvas in pixels, its works fine but when I set the width and height like '10cm' or something, it doesn't works. What am I doing wrong? can we only set the canvas in pixels? not in any other? I am working on SVG and convert it to canvas to save as image file, my SVG canvas's width and height is in cm so I want that canvas should be in cm too, otherwise the final image crops the data of SVG.

Find the new position of rectangle for resized image

I have a four element position vector [xmin ymin width hight] that specifies the size and position of crop rectangle from image I. How can i find the new position and size for the resized image I?
It is not entirely clear, what you want, as we don't know your coordinate system. Assuming x is the horizontal axis and y is the vertical axis and your point (1,1) is at the top left corner, you can use the following snippet:
p = [xmin ymin width height];
I = I_orig(p(2):p(2)+p(4)-1,p(1):p(1)+p(3)-1);
The size is of course your specified width and height.
You can convert your original bounding box to relative values (that is assuming the image size is 1x1)
[origH origW] = size( origI(:,:,1) );
relativeBB = [xmin / origW, ymin / origH, width / origW, hight / origH];
Now, no matter how you resized your origI, you can recover the bounding box w.r.t the new size from the relative representation:
[currH currW] = size(I(:,:,1));
currBB = relativeBB .* [currW, currH, currW, currH];
You might need to round things a bit: you might find floor better for xmin and ymin and ceil more suitable for width and height.

Maintain Aspect Ration on SVG Image Calculation Formula

I have an image element of the SVG inb the HTML5,
The image size is:
(stretch, preserveAspectRatio is set to "none")
width=520px
height=240px
then, I modify it to maintain aspect ratio (preserveAspectRatio is set to defer)
because width>height,
the height still the same = 240px
but how can I calculate the new width (it is shrinked because the maintain aspect ratio)
is there a formula to this?
viewBox width / viewBox height * 240px I imagine.

Resources