How to find the tangent to any pixel? - c#-4.0

I want to find tangent at each pixel in image.
NOTE: image is having white background and shape border color is block.
What i did is,
Algo
While(true)
take pixel
if pixel color is black
make 3 X 3 matrix => fill the matrix by surrounding pixel color
...means assume white =0 and black=1 then keeping selected pixel
at center for 3 X 3 matrix and finding all other value;
----------------------------here i want to find tangent line to selected pixel;
end if
Move to next pixel.
End while
Please help Exams on head .

What you're looking for is probably a Sobel Operator. It's implemented as a convolution of the neighborhood around a pixel with the matrix:
-1 0 1
-2 0 2
-1 0 1
and again with:
-1 -2 -1
0 0 0
1 2 1
Call the results of the 2 convolutions x and y, respectively. Once you have them, you can get the magnitude of the gradient by taking the square root of the sum of the squares:
mag = sqrt(x * x + y * y);
and the direction of the gradient (which should be tangent to the pixel you're examining) by taking the arctangent of y over x:
tangent = atan2(y / x)

Related

Plot colour of 3D point based actual value of each data point

I have a 3D array of slopes, X.
I would like to plot each data point and color-code the point based on the actual value. There are three possible values.
if the slope greater 80 then plot data point in red. If the slope greater -80 then blue, else green.
I would also like the legend.
Thanks
I have tried to create a couple of things but not getting it right.
#Make the array to 2D with X, Y, Z axis
X=fits.transpose(2,0,1).reshape(-1,3)
print(X.shape)
kcolors = ['red' if slope greater 0.2 elif slope greater -0.2 'green' else 'blue' for slope in X]
plt.scatter(transformed[:,0], transformed[:,1], transformed[:,2], c=kcolors)
File "<ipython-input-26-4efc22ca2a34>", line 6
kcolors = ['red' if slope > 0.2 elif slope > -0.2 'green' else 'blue' for slope in points]
SyntaxError: invalid syntax
I tried
colors = []
for slope in Xpoints[0]:
if slope > 0.2:
colors.append('r')
elif slope < -0.2:
colors.append('g')
else:
colors.append('b')
This seems to work.
Is there a better way of doing it?

Why are the colors in the 1931 CIE xyY chromaticity diagram white?

When we look at the 1931 CIE chromaticity diagram, represented within the x y plane of xyY space, it renders white colors (or close to white) at points of luminance like the D65 point highlighted here with E.
But why is this the case? The point for D65 is supposed to be represented at x = 0.33, y = 0.33. Given the formula Y = 1 - x - y, wouldn't that mean Y is 0.34?
The sRGB correlate or xyY at 0.33,0.33,0.34 is 158.4182, 155.5676, 176.8565 according to every converter I found. This is a light brown and not the near-white seen in every 1931 chromaticity diagram.
It seems like I need to scale the Y to get the proper luminance value for every channel.
Using the Y = 1 - x - y formula, my diagram looks like this, a muted diagram:
What don't I understand?
Edit
Setting Y = 1 and the diagram looks like the below, better.
Edit
Now looks like the below.
There is some imprecision on the interpretation of chromacity diagrams.
CIE xyY is a 3D figures. Often we see only a projections (often not a intersecting plane, just a projection).
One common projection is the "additive" xy chromacity diagram. You may notice it because it has yellow at border, and the white somewhere near the center. In such projection you show the maximum Y given a chromacity x,y.
Common is also the "subtractive" diagram, like your second one. No yellow, no white. This diagram has just the subtractive mix of the primaries, so the brighter colour are the primaries, and you get darken between them.
Note: usually the chromacity diagram are also extended also out of gamut, so the primaries are no more the real primaries, and white could not be white, and the yellow could be cut off, as your diagrams. You may try at first just the triangle between primaries, then expand. It is easier to debug.
The white will be just on top of 3D figure. In the first case, you take the outer surface of gamut, so you get the white. In the second case, you get a plane inside the figure, so you will never get white. But it is still a xy chromacity diagram.
On your case, I think you clipped the colour values (Note 1), which it is wrong: by clipping you will not get the correct chromacities (by clipping, one remove a certain value of a colour, so the ratio between channel is not maintained). One should use float or larger numbers for calculations, before to normalize (channel values in range 0 to 255). [Normalize (in this case): keep chromacity, but adapt Y so that final colour is in gamut]. In practice: you get the maximum value between R, G, B, and you multiply every channels by 255/max(R,G,B).
Note: this is not fully correct/precise. The above normalization should be done in linear space (light mix linearly), and only after normalization, the gamma funtion should be applied. On the other hand, on above figures, we do not have the correct colour for every point x,y. We can do it correctly only on a triangle (of gamut). By expanding the available colour on screen to full xz chromacity, we create errors/imprecisions. So normalization before or after gamma correction is not more so relevant (and it just change slightly the colours).
Note 1: From comment: this (clipping) it is not true, OTOH the very tiny part of blue (dark blue), and too much magenta and cyan, make me thinking about some numerical prolem)
The white point of CIE 1931 is not in x=1/3, y=1/3, and white color is not x=1/3, y=1/3, Y = 1/3.
According to Wikipedia:
The CIE 1931 color space chromaticity coordinates of D65 are
x=0.31271
y=0.32902
Since D65 represents white light, its co-ordinates are also a white point, corresponding to a correlated color temperature of 6504 K. Rec. 709, used in HDTV systems, truncates the CIE 1931 coordinates to x=0.3127, y=0.329.
The meaning of x=1/3, y=1/3 is different:
Light with a flat power spectrum in terms of wavelength (equal power in every 1 nm interval) corresponds to the point (x, y) = (1/3, 1/3).
Important: D65 is not a "flat power spectrum".
Computer systems (PCs) uses sRGB color format.
In sRGB the color components are after gamma (in contrast to CIE 1931 which applies linear curve).
In xyY color space, x,y are the chromaticity and Y is the luminance.
x=0.31271, y=0.32902 is the chromaticity without luminance and applies gray chromaticity.
For white color use Y = 1
Rec. 709, used in HDTV systems, truncates the CIE 1931 coordinates to x=0.3127, y=0.329
Lets compute sRGB of x=0.3127, y=0.329, Y = 1:
X = (Y/y)*x = 0.95046
Y = 1
Z = Y/y*(1-x-y) = 1.0891
Rlinear 3.240600 -1.537200 -0.498600 X 0.99984
Glinear = -0.968900 1.875800 0.041500 * Y = 1.00010
Blinear 0.055700 -0.204000 1.057000 Z 1.00007
Assume result is 1, 1, 1.
Last stage is applying gamma for converting "Linear sRGB" to sRGB.
Since all values are 1, the result is sRGB = 1, 1, 1.
We can repeat the computation for Y = 0.2, and the result is Linear sRGB = 0.2, 0.2, 0.2.
Apply gamma:
gamma(u) = 1.055*u^(1/2.4) - 0.055 for u > 0.0031308
1.055*0.2^(1/2.4) - 0.055 = 0.48453
So sRGB = 0.48453, 0.48453, 0.48453.
For converting to the standard range of [0, 255] (one byte per color channel), we need to scale by 255 and round the result: RGB888 = 124, 124, 124.

opengl transformation from model coordinate to world coordinate

I am having trouble to understand the transformation from Model Coordinate to World Coordinate. Here is the slides, my main problem is what is m1, x1, x2, x3, y1, y2, y3, and z1, z2, z3. What kind of value are they representing? and How should I determine it.
They represent a transformation. Specifically, they represent the basis vectors of the transformed coordinate system. To understand what this means, I recommend watching 3Blue1Brown's "Essence of linear algebra", it explains all you need to know about these transformations in an intuitive way.
More practically, what you'll want to do is mainly 3 things: you want to scale your object, you want to rotate your object, and you want to move your object. All of these are transformations. Whenever you see the word "transformation", read it as "matrix". These operations are all matrices.
So for example the scaling matrix is:
sx 0 0 0
0 sy 0 0
0 0 sz 0
0 0 0 1
Where sx is the amount you want to scale in the direction x, and so on.
The rotation matrix will depend on the axis of rotation, for example this is the rotation matrix to rotate the object around the x axis by an angle of t radians, following the right hand rule:
1 0 0 0
0 cos(t) sin(t) 0
0 -sin(t) cos(t) 0
0 0 0 1
You can find the other ones here.
This is the translation matrix used to move the object around:
0 0 0 tx
0 0 0 ty
0 0 0 tz
0 0 0 1
You combine these transformation by multiplying them together. So if your point is (x, y, z), S is your scaling matrix, R is your rotation matrix and T is your translation matrix, you transform the point like this: p = T*R*S*(x,y,z,1). The 1 as the "4th dimension" is used for projection. Tthe GPU divides x, y and z by that value, called w, after it's done processing the vertex. Research projection matrices to know more.

how to calculate gradient color by percent

I have a float score, which is 0 to 1
I need translate this score to color,
0 is green
1 is red
0.5 should at the middle of green to red gradient color
and so on
how to write this? I have no idea.
let gradient colour parameter be t, 0.0 =< t =< 1.0
colour = RGB(255 * t, 255 * (1 - t), 0)
Multiply the float by 255 to get your green value, and multiply (1-float) by 255 to get your red value. If you need to output a css color code, use rgb(x,y,z).

How do I interpolate normals on Catmull-Clark Subdivision Surfaces

I'm using CCSS to generate smooth surfaces.
I've been using the regular subdivision rules to interpolate the surface/vertex normal, but I think this may be wrong.
Are there different stencils to interpolate normals?
The "normals" from the control mesh are not really normals to begin with. They're just made-up vectors at each vertex, and not something you want to interpolate.
Instead, use the derivative stencils, which yield tangent vectors in two directions. Once you have your tangent vectors, cross them to get a normal. The derivative stencils are:
1 4 1
0 (0) 0
-1 -4 -1
and
-1 0 1
-4 (0) 4
-1 0 1

Resources