Image files are rectangular, yet in a 3D environment, a rectangle will appear as an irregular quadrilateral most of the time. Consider the following image (credit to this blog):
Given that:
you already know the four vertices of the green face
you have access to the image file as a 2D array of color values
you are drawing to a screen that is a 2D array of pixels
What is the algorithm that OpenGL uses for drawing the image onto the green face?
The algorithm is basically (if we ignore stuff like shaders for now):
break down the quadrilateral into two triangles
for each triangle, compute the projection onto the image plane
for each screen pixel covered by the projection of the triangle:
compute the texture coordinates by interpolation from the vertices for the position to which the pixel location corresponds on the triangle.
look up the texture image at the location that corresponds to the texture coordinates. Typically, some form of filtering is applied here.
you have found the color for your pixel
Related
I know i can go from 3d space to 2d space of the Mesh by getting the corresponding uv coordinates of the vertex.
When i transform to uv space, each vertex will have its color and i can put the color in the pixel position what the uv co-ordinate returns for a particular vertex, but the issue is how do i derive the pixels that lie inbetween them, i want a smooth gradient.
For example, the color value at uv co-ordinate (0.5,0.5)->(u,v) is [30,40,50]->(RGB) and at [0.75,0.75] its [70,80,90] and lets say there are three vertices and theres one more at [0.25.0.6] as [10,20,30], how do i derive the colors that goes on the area these three uv/vertex coordinates fill, i mean the inbetween values for the pixels?
Just draw this mesh on a GPU. You know already that you can replace vertex positions with UVs so you have it represented on the texture space.
Keep your vertex colors unchanged and draw this mesh on your target texture, GPU will do color interpolation for every triangle you draw.
And keep in mind that if two or more triangles share the same texture space then the result will depend on triangle order.
I see that everybody normalizes his coordinates to be in range of [-1;1]. Why is this done? Is this required? Is this only for ThreeJS , or is it a common thing for every 3D framework?
Three.js is based on WebGL and WebGL is based on OpenGL ES (Embedded System)
In WebGL / OpenGL ES there is a viewport:
2.3 The WebGL Viewport
OpenGL manages a rectangular viewport as part of its state which defines the placement of the rendering results in the drawing buffer. Upon creation of the WebGL context, the viewport is initialized to a rectangle with origin at (0, 0) and width and height equal to (canvas.width, canvas.height).
5.13.4 Viewing and clipping
The viewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. The size of the drawing buffer is determined by the HTMLCanvasElement. The scissor box defines a rectangle which constrains drawing. When the scissor test is enabled only pixels that lie within the scissor box can be modified by drawing commands. When enabled drawing can only occur inside the intersection of the viewport, canvas area and the scissor box. When the scissor test is not enabled drawing can only occur inside the intersection of the viewport and canvas area.
This means gl.viewport defines the transformation from normalized device coordinates to the viewport rectangle.
Because of that everything which is drawn and should be shown on the viewport, has to be in the normalized device space, which is in the range from (-1,-1,-1) to (1, 1, 1).
Transformation of the geometry to normalized device coordinates:
The geometry is transformed by its model matrix, to world coordinates.
To transform from wolrd coordinates to normaliced device coordinates, Three.js provides the THREE.OrthographicCamera or the THREE.PerspectiveCamera. The camera defines a view matrix and a projection matrix.
The view matrix describes the direction and position from which the scene is looked at. The view matrix transforms from the wolrd space to the view (eye) space. In general world coordinates and view coordinates are Cartesian coordinates.
The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. The projection matrix transforms from view space to the clip space. Clip space coordinates are Homogeneous coordinates. The coordinates in the clip space are transformed to the normalized device coordinates (NDC) in the range (-1, -1, -1) to (1, 1, 1) by dividing with the w component of the clip coordinates.
I am writing a program in Matlab to detect a circle.
I've already managed to detect shapes such as the square, rectangle and the triangle, basically by searching for corners, and determining what shape it is based on the distance between them. The images are black and white, with black being the background and white the shape, so for me to find the corners I just have to search each pixel in the image until I find a white pixel.
However I just can't figure out how I can identify the circle.
Here it the an example of how a circle input would look like:
It is difficult to say what the best method is without more information: for example, whether more than one circle may be present, whether it is always centred in the image, and how resilient the algorithm needs to be to distortions. Also whether you need to determine the location and dimensions of the shape or simply a 'yes'/'no' output.
However a really simple approach, assuming only one circle is present, is as follows:
Scan the image from top to bottom until you find the first white pixel at (x1,y1)
Scan the image from bottom to top until you find the last white pixel at (x2,y2)
Derive the diameter of the suspected circle as y2 - y1
Derive the centre of the suspected circle as ((x1+x2)/2, y1+(y2-y1)/2)
Now you are able to score each pixel in the image as to whether it matches this hypothetical circle or not. For example, if a pixel is inside the suspected circle, score 0 if it is white and 1 if it black, and vice-versa if it is outside the suspected circle.
Sum the pixel scores. If the result is zero then the image contains a perfect circle. A higher score indicates an increasing level of distortion.
I think you may read about this two topics:
Theoretical:
Binary images
Hough transform
Matlab:
Circle Detection via Standard Hough Transform
Hough native in matlab
Binary images
I have a binary image in matlab and i have a centroid of an area (see the * in the picture) which was previously calculated. Now based on this centroid x and y, i would like to get to get the surface (m2) of the circle the centroid is in. But i don't know how to select an area which is very similar to the rest of the circles and i only know the centroid of the one i want. Any suggestions ? Thx in advance .
This is very easy with morphological reconstruction. While I don't know how you can access the matlab implementation, you should use a black image as a single white pixel at your centroid position as the marker image, and your shown binary image as the mask image, and the resolut of a morphological reconstruction by dilation will be an image just showing the selected circle in white. Count white pixels and you are done.
So we have such situation:
In this illustration, the first quadrilateral is shown on the Image Plane and the second quadrilateral is shown on the World Plane. [1]
In my particular case the Image Plane has 3 quadrilaterals - projections of real world squares, which, as we know, have same size, lying on the same plane, with same rotation relative to the plane they are lying on, and are not situated on same line on plane.
I wonder if we can get rotation angles of Image Plane to World Plane knowing stuff described?
In my case as input I have such data structures: original image (RGB pixels), objects (squares) with angles points in pixels (x,y) on Image Plane.
Take a look at Sections 2 and 3 of Algorithms for plane-based pose estimation.
The methods described there assume that you know the (x,y) coordinates of the features in question - in this case the red squares.
The problem you are describing is generally known as pose estimation - determining the 3D orientation and position of an object relative to a camera from a 2D view. For you, the object is a plane. Googling 'pose estimation plane' should give you more sources.