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

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.

Related

OpenCV Structured Light getProjPixel() example

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!

Neural networks and Python

I'm trying to study about neural networks, following a great guide:
http://neuralnetworksanddeeplearning.com/chap1.html
Currently I've reached this code snippet which I'm trying to understand and write in Java:
class Network(object):
def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]
I managed to figure out what everything means except for the last line:
[np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]
As far as I can understand: create a matrix with y rows and x columns, for each pair x,y which can be found in the matrix zip which is created by the merging of the two "sizes" arrays. I understand that sizes[1:] means taking all elements from sizes starting from index 1, but sizes[:-1] makes no sense to me.
I read online that s[::-1] means getting the reverse of the array, but in the above case we only have one colon, while in the formula for the reverse array there seems to be two colons.
Sadly, I have no idea how Python works and I got pretty far along with the online book to give it up now (I also truly like it), so can someone say if I'm right until now, correct me if needed, or straight out explaining that final line?
sizes[:-1] is a list slice which returns a copy of the sizes list but without the last item.

Using Processing language to visually represent XML. How do I draw an ellipse at each point the data ends/breaks?

I'm involved in a research project analysing how (if at all) architecture affects people's paths as they move around locations. So far we have used OpenCV Blob-tracker to successfully produce XML file data mapping the blobs as they move.
What I would like to do now, is draw an ellipse at each point the data begins and ends (to represent each person's start and end point).
Any help on reaching this conclusion would be most welcome.
I don't really understand the structure of the data you have provided either. But in processing if you want to represent xml data (specifically position as coordinates on the screen and time as color) you first need to parse your xml file then map the values appropriately. Take a look at
http://processing.org/reference/XMLElement.html
http://processing.org/reference/map_.html
http://processing.org/reference/fill_.html
these should have about everything you need.
You can do something like this to represent xml as ellipses and colors.
say this is your xml, (im just making this up)
<?xml version="1.0"?>
<people>
<person time="45.6" x="6.5" y="10.3"></person>
...
</people>
XMLElement xml;
void setup() {
size(200, 200);
int size = 10; //just a default size for the ellipse, maybe you want to pull this value from your data as well though
xml = new XMLElement(this, "people.xml");
int numPeople = xml.getChildCount();
for (int i = 0; i < numPeople; i++) {
XMLElement person = xml.getChild(i);
float time = person.getFloat("time");
float xPos = person.getFloat("x");
float yPos = person.getFloat("y");
int personColor = map(time, 0, 100, 0, 255); //you will need some way of mapping your time values (i have no idea what the scale is, to a range of 0-255
fill(personColor);
ellipse(xPos, yPos, size, size);
}
}
Based on the series of numbers you provided I would guess that your xml structure is much more complicated than what I provided in this example, if you want help parsing your specific data then please post a more complete example and description of the xml.

Implemeting a File Character Analysis

I'm stuck on my assignment and wanted some pointers on an algorithm.
I am presented with text files that represent different images. blank space is where no pixels are on and '&' represents an on pixel.
The aim is i am given a 100 x 100 image text file to analyse and work out the probability that the object is there and then the co-ordinate of where it is on the file.
I know that i have to use character analysis of some sort but i feel that i have to check for example 10x10 grids at a time, analyse how many pixels are on and work out the certainty that the object is there. (This is because more or less pixels can be on and the object still present)
Thanks for your help.
I think I understand your question correctly. One thing that will change the answer is whether or not you know the object beforehand. If you are looking for an arbitrary pattern, it is somewhat more difficult, but still feasible. To find an object that you know what it will look like will come down to nested for loops and a solid understanding of 2D arrays. You can pull in each line of the text file and look for an '&'. If it finds one, it begins looking for the rest of the pattern based on the location relative to that first '&'.
For example, if you are looking for a diagonal line from top left to bottom right, you would continue along until you came to the first '&'. After that, you would look at the cell one column over and 1 row down. If that is an ampersand as well, you know that you have a diagonal line. If not, just keep going along after the first '&'.
for (int c = 0; c < textArray.length; c++)
{
for (int i = 0; i < textArray[c].length; i++)
{
Look at the character
If it is '&'
Look for the next character and so forth
If the pattern is there
return true
}
}
See if that helps get your algorithm rolling. You will need to make sure to check for legal bounds in your arrays in order to combat out of bounds exceptions.

Colorize Objects

My problem is that I need for a graphical output of an algorithm a method to colorize all the objects I have. So I wrote this one here:
int[] next_color = {0x70,0x00,0x00};
private int max_co = 0xF0;
private int next_c = 0x01;
private int step = 0x10;
public Color getNextColor(){
next_color[next_c%3]%=max_co;
next_color[(next_c++)%3]+=step;
return new Color(next_color[0], next_color[1], next_color[2]);
}
What I was thinking while writing this: I found out that colors under #707070 will appear mostly like black, so it does not make any sense to use them. Also I found out that only steps with greater than 0x10 are (really good) recognized by the eye. But now I have only red and some blue objects arround (when using small amounts of objects) - so it looks a little bit like crap. Is there a good way to generate a new color, which can be good differed from the previous and next one?
HSV/HSL color geometries should be easier to use in such algorithms.

Categories