OpenCV Structured Light getProjPixel() example - java

I have been trying to recreate the structure light functions by following the structured light Graycode tutorials on OpenCv for Unity (it's a port of OpenCV java)
https://docs.opencv.org/4.1.2/dc/da9/tutorial_decode_graycode_pattern.html
The tutorials seem straightforward, but I think they may be out of date now. For instance, there is no more "decode" function in the Graycode part of the API
https://docs.opencv.org/trunk/d1/dec/classcv_1_1structured__light_1_1GrayCodePattern.html
Mat disparityMap;
bool decoded = graycode->decode(captured_pattern, disparityMap, blackImages, whiteImages,
structured_light::DECODE_3D_UNDERWORLD);
Instead i think i might need to use getProjPixel() instead somehow? I can't find any other examples using this online thought.
This was my rough thought of maybe how to use it
for(int x = 0; x < 1920; x++)
{
for(int y = 0; y < 1080; y++)
{
Point thepoint= new Point(-1,-1);
grayCode.getProjPixel(photosCam1, x, y, thepoint );
projPix.Add(thepoint);
}
}
But i don't totally know what i would do next to get a pointcloud (and ultimately a mesh for unity) from this, or if i am even going the right direction

Unfortunately, it looks like the answer is just that the Java port of OpenCV is incomplete when it comes to the structure light module. So there basically is no way to actually decode the captured images for java (or the subsequent ports to unity)
https://answers.opencv.org/question/222527/structured-light-module-missing-decode-function/?comment=222534#post-id-222534
too bad!

Related

Fabric API getting the color of a grass block

I recently started modding minecraft using the fabric api.
I want to get the color of the grass block the player is currently standing on. I found that in the Biome class there is a method called getGrassColorAt(double x,double y) I managed to get it's data but it's an int. And I don't know how to use it.
For instance by calling getGrassColorAt(double x,double y) in a plain biome I would get: -7226023, in a river biome I would get -7423631. In a desert it would be -4212907.
I know the color of a grass block is determined with a texture map called grass.png as I have read on this but i don't know how an int could help me using it.
NB: If getting the color is impossible I would like to at least get the name of the biome (I could not find a getName in the class)
And if anyone know where I can get a readable documentation can you link it please. (I'm currently using my IDE to know the different methods from a class)
Thank you for your time.
Someone on the fabric discord helped me.
So apparently the data vas encoded in the string I had to isolate the r g b values directly from the bits (or so I understood) here is the solution (j is the color as an int)
int r = j >> 16 & 255;
int g = j >> 8 & 255;
int b = j & 255;

How do you generate random patterns and convert them to an image?

I am currently making a game. I have multiple users and for each user I have a id (UUID). I was hoping to use this id as like a key to generate a random pattern then to a image for that player.
boolean[] booleanPlayerID = BSUtility.bytesToBooleans(playerID.getBytes(UTF_8));
WritableImage image = new WritableImage(50, 50);
PixelWriter writer = image.getPixelWriter();
int booleanIndex = 0;
for(int y = 0; 50 > y; y++){
for(int x = 0; 50 > x; x++){
if(booleanIndex >= booleanPlayerID.length){
booleanIndex = 0;
}
if(booleanPlayerID[booleanIndex]){
writer.setColor(x, y, Color.BLACK);
}
booleanIndex++;
}
}
Assuming the image is going to be 50x50... What I have been doing is I take the player's id, convert to boolean array, cycle through image pixels, also cycle through boolean array, if the boolean value is true then set the pixel color to black. The if statement is to avoid indexing out of bounds (I know I should probably convert the boolean array to be the same length as how many pixels are in the image)...
This does produce some what of a pattern, but to be honest it is a really shitty pattern. The patterns generally tend to be very similar to one another. I was wondering if someone could provide tips/example of how to do this better. With my little research I really couldn't find much. I did however find patterns that I am hoping to somewhat obtain, I believe they are called tangle patterns. I would really hope to have each user have their own unique image/pattern but obvious it is fine if they are somewhat similar but not same. I don't know if this is possible to be honest.
If you need a complete example I can replace the code above. Although the code above should be pretty straight forward to making it a full example (only things missing is generating the userID and converting it to boolean array from bytes).
What you are talking about is known as IDENTICON.
I actually don't know how they work but i know that they use some sort of hashing with the UNIQUE TEXT and generate image with that hash.
Here are two website i found who do provide source code so if you are interested you can look into the code and extract any useful information from there.
http://identicon.net/
https://jdenticon.com/
PS. Code are in JavaScript and on one website its in .NET and PHP too.

I'm new to java and CPLEX. Struggling with summations and arrays

I'm trying to write a code in Java with CPLEX but I have a problem. I'm new to CPLEX and Java as well.
I've studied for a few days and I could understand very simple CPLEX examples but I have to deal with something much more difficult now.
I was trying to write expressions like 1000*(k-Sigma y^k(k from 1)) but I couldn't make it work so I googled for this topic but I couldn't find a way to figure this out.
What I understood so far is I need an array like this
IloNumVar[] y = cplex.numVarArray(?, ?, Double.MAX_VALUE?);
I read the manual from the IBM website but I still don't understand what variables I should put inside brackets.
And I need a for loop for the summation so I wrote like
for(int k = 1; k <= bus; k++) {
objective.addTerm(1000, k-y[k]);
}
Of course it's not working. I guess something's wrong with 'k' but have no idea how to fix this.
Please take a look at the following page:
CPLEX Java API
You can see the following method header:
IloNumVar[] numVarArray(int n, double lb, double ub)
where the params are:
n - number of variables
lb - lower bound
ub - upper bound
If you use it like this:
IloNumVar[] y = cplex.numVarArray(5, 0, Double.MAX_VALUE);
you are going to create an array of 5 elements (variables). Each of them has to be a non-negative number.
Please specify more clearly the 2nd part of your question.

Generating knots procedurally (and visualizing them)

I'm looking for an algorithm that provides a way to generate graphical representations of knots (either 2D or 3D, but the former, with vector graphics is preferable).
I've seen many links to knot theory in general, spanning from punctual references to general information.
Before trying to devise something from scratch by myself, I'd like to know about the existence of some existing software that lets you not only represent them (in memory) but visualize in some of their graphical representations (there are many). It could come in the form of a library, or a simple function, or even a pseudocode algorithm that tries to specify how to properly draw a know on screen.
As the previous link suggests, there is a package in Wolfram Mathematica, named KnotTheory that does that (in an almost complete way). However, it is not portable, nor free software and accessing its modules would be very cumbersome for me (a free implementation in Java, just to name a language, but each language is fine, would be be ideal from the portability and openness perspectives).
I've seen that many softwares are available (but most of them are old and not reachable or usable).
Do you have some good pointers to share?
UPDATE:
Since two votes to close this question have appeared, I am restating it in a more pragmatic and clear way: are there algorithms to draw and generate knots? has something been already implemented
UPDATE 2 (for reopening)
The graphical representation could be a 3D rendered object or a 2D svg graphics (I am abstracting from it since I am looking forward a programming language as Processing (or the same Mathematica itself) that provides you the primitives to draw curves (splines, beziers, etc) on screen (and then export them to raster or vector graphics).
The algorithm shall take one knot parametrization as input (ie, if we are talking about knots described by their crossing properties, their values is what is needed), returning one of the graphical representation above (ie even a sequence of points in a 2d space). That is, any parametrization is fine, my objective is just to get introspection on how to draw knots so to get ONE algorithm that does that in a particular way, leading to a particular representation, would be fine (Mathematica's lib seems to be able to draw it in so many representations).
Something like this?
void setup() {
size(300, 300, P3D);
}
void draw() {
background(36, 10, 28);
int f = frameCount%360;
translate(width/2, height/2);
if (frameCount >= 360 && frameCount <= 1080 )
rotateY(radians(f));
stroke(0);
drawKnot(40);
translate(knotX(f)*40, knotY(f)*40, knotZ(f)*40);
noStroke();
fill(180,50,145);
sphere(10);
}
void drawKnot(float sz) {
stroke(200);
for (int i = 0; i < 360; i++) {
point(knotX(i)*sz, knotY(i)*sz, knotZ(i)*sz);
}
}
float knotX(int n) {
return sin(radians(n)) + 2*sin(radians(n*2));
}
float knotY(int n) {
return cos(radians(n)) - 2*cos(radians(n*2));
}
float knotZ(int n) {
return sin(radians(n*3))*-1;
}
Wolfram Mathematica has basic knot theory, including visualization, built in:
http://reference.wolfram.com/language/ref/KnotData.html
This webpage from the software documentation contains many examples of standard knot-theoretic visualization and computation that can be done with Mathematica.

I need to plot a simple animated arrow vector in 3 dimensional space

I am trying to display a single magnetic field vector (point in space, with arrow from origin) using data from Android phone sensors. I wrote a simple server in Python to poll the phone for sensor data, and I want to plot the data received in real time. I'm looking for a simple solution, but I can't find any.
I looked at matplotlib, blender, and visual python but I couldn't find a simple enough solution that simply takes the 3 coordinates and plots. The data received is simply a vector with 3 points. The arrow from the origin is not so important, I just want to be able to visualize the moving point in 3d space.
Also, if necessary, I can rewrite the server in Java and use a Java plotting library. I just need some suggestions, and short code examples that achieve this.
You can draw scene by VPython very easily:
from visual import *
import math
def make_grid(unit, n):
nunit = unit * n
f = frame()
for i in xrange(n+1):
if i%5==0:
color = (1,1,1)
else:
color = (0.5, 0.5, 0.5)
curve(pos=[(0,i*unit,0), (nunit, i*unit, 0)],color=color,frame=f)
curve(pos=[(i*unit,0,0), (i*unit, nunit, 0)],color=color,frame=f)
return f
arrow(pos=(0,0,0), axis=(5,0,0), color=(1,0,0), shaftwidth=0.1)
arrow(pos=(0,0,0), axis=(0,5,0), color=(0,1,0), shaftwidth=0.1)
arrow(pos=(0,0,0), axis=(0,0,5), color=(0,0,1), shaftwidth=0.1)
grid_xy = make_grid(0.5, 10)
grid_xz = make_grid(0.5, 10)
grid_xz.rotate(angle=pi/2, axis=(1,0,0), origin=(0,0,0))
grid_yz = make_grid(0.5, 10)
grid_yz.rotate(angle=-pi/2, axis=(0,1,0), origin=(0,0,0))
sphere(radius=0.3)
obj = arrow(pos=(0,0,0), axis=(1,2,3), shaftwidth=0.3)
th = 0
while True:
rate(20)
obj.axis = (3*math.cos(th), 3*math.sin(th), 2)
th += 0.04
VPython is very simple:
pointer = arrow(pos=(0,0,0), axis=(1,2,3), shaftwidth=1)
Just change the axis=(1,2,3) to the 3 points in your vector. More information can be found here.
If c++ is an option you should really take a look at VTK
http://www.vtk.org/
It is very powerful to display 3D vector fields, and is pretty easy to use

Categories