GPUImage and Syphon - gpuimage

I have implemented a syphon server within a GPUImage application. However it produces a triangular area as seen in the attached image.
Can anyone say what’s wrong looking at the image?
Or Code?
In MyView#viewDidLoad
NSOpenGLContext *ctx = [[GPUImageContext sharedImageProcessingContext] context];
syphonServer = [[SyphonServer alloc] initWithName:#”MyServer” context:ctx.CGLContextObj options:[NSDictionary dictionaryWithObject:SyphonImageFormatRGBA8 forKey:SyphonServerOptionImageFormat]];
At the end of the myFilter#renderToTextureWithVertices
[myServer publishFrameTexture:[firstInputFramebuffer texture] textureTarget:GL_TEXTURE_RECTANGLE_EXT imageRegion:NSMakeRect(0, 0, size.width, size.height) textureDimensions:size flipped:YES];
Thanks for the input.

My hypothesis is that two things are wrong:
You're not clearing the back buffer before drawing the texture, leading to the garbled text you see.
The full screen rectangle you draw uses the wrong indices for the second triangle. If numbered 0, 1, 2, 3 (clockwise, starting at top left), it looks like the indices are [0, 1, 3] and [0, 2, 3], instead of [0, 1, 3] and [1, 3, 2].

Related

Game dev -Finding img sprites coords Haxe + Haxeflixel

I'm trying to make a combat system in haxe I made my sprites and now I need to find out the uv coordinates for them how do I achieve that ?
Example from my existing code :
animation.add("lr", [3, 4, 3, 5], 6, false);
animation.add("u", [6, 7, 6, 8], 6, false);
animation.add("d", [0, 1, 0, 2], 6, false);
I tried :
if (FlxG.mouse.justPressed) {
// Attack code here
}
Haxeflixel's sprite sheet system doesn't use UV coordinates, it assumes all sprites in the sheet are the same size and consistently spaced apart.
Look at the API reference for FlxSprite.loadGraphic. The animation tutorial uses that function like this:
loadGraphic(AssetPaths.player__png, true, 16, 16);
This means the sprites are in a 16x16 grid, and they're indexed from left to right, top to bottom (with the left-most being index 0).
Here's a visual example of how the coordinates map to indices:
And here's that same sprite, but with two rows instead of one:
Compare those indices to the values in the sample code:
animation.add("lr", [3, 4, 3, 5], 6, false);
animation.add("u", [6, 7, 6, 8], 6, false);
animation.add("d", [0, 1, 0, 2], 6, false);
The second parameter to add is an array of indices that make up the animation.
So to add a punch animation, just put the frames into the existing grid, and reference them using their indices in the overall spritesheet.
As far as implementing a "combat system", that's a more involved thing. If you just want to play an attack animation, you can make it a non-looping animation, and use a callback function to detect when it ends. Example:
//...defined somewhere in your sprite class
var is_punching:Bool = false;
//...somewhere inside your constructor maybe
animation.finishCallback = function(name:String){
switch(name){
case "punch-u", "punch-lr", "punch-d":
is_punching = false;
default:
trace("Some other animation finished: " + name);
}
}
//...somewhere in your update function
if (FlxG.mouse.justPressed && !is_punching) {
is_punching = true;
switch (facing)
{
case LEFT, RIGHT:
animation.play("punch-lr");
case UP:
animation.play("punch-u");
case DOWN:
animation.play("punch-d");
case _:
}
}
Then you can check if is_punching is true to prevent the player from walking while punching, or to damage an enemy if they collide with the player while they're punching.

Repeat 3d tensor's rows in pytorch

I have a BxCxd tensor of coordinates and want to repeat each row in the following way:
[[[1,0,0],[0,1,0],[0,0,1]]] -> [[[1,0,0],[1,0,0],[0,1,0],[0,1,0],[0,0,1],[0,0,1]]]
In the above example each row is repeated 2 times. What's especially important is the ordering. Each row in the first tensor should appear k times in the second one before the next row appears.
I tried the following code:
print(x.size())
params = x.repeat_interleave(self.k, dim=-1).permute(0,2,1)
In the above snippet, x is of size 32x128x4 before repeat_interleave. With self.k = 64 I would expect the result to be a 32x8192x4 tensor, however the result I am getting is 32x256x128 which does not make sense to me. What am I missing here?
I think you want:
t.repeat_interleave(2, dim=1)
Output:
ensor([[[1, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 1, 0],
[0, 0, 1],
[0, 0, 1]]])

PyTorch differentiable mask

How would I go about blacking out a portion of an image or feature map such that AutoGrad can backprop through the operation?
Specifically I want to black out everything except for n layers of border pixels. So if we consider a single channel of the feature map which looks like:
[
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
]
I set a constant n=1 so my operation does the following to the input:
[
[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1],
]
In my case I'd be doing it to a multi channel feature map and all channels would be treated the same way.
If possible, I want to do it in a functional manner.
Considering the comments you added, i.e. that you don't need the output to be differentiable wrt. to the mask (said differently, the mask is constant), you could just store the indices of the 1s in the mask and act only on the corresponding elements of whatever Tensor you're considering. Or if you don't want to deal with fancy indexing, you could just keep the mask as a Tensor of 0s and 1s and do an element-wise multiplication of it with whatever Tensor you're considering. Or, if you truly just need to compute a loss along just the border pixels, just extract the first and last row, and first and last column, and avoid double-counting the corners. This latter solution is essentially just the first solution recast in a special case.
To address the question in your comment to my answer:
x = torch.tensor([[1.0,2,3],[4,5,6]], requires_grad = True)
print(x[:,0])
gives
tensor([1., 4.], grad_fn=<SelectBackward>)
, so we see that slicing does not mess with the autograd engine (it's still tracking the contribution to the gradient). It is not too surprising that this works automatically; slicing can be viewed as the (mathematical) function that of projecting onto a subspace of R^n, for which it's easy to compute the gradient.

After making a copy of a matrix (list of lists), why does double indexing the copy change the original but indexing does not?

I have a matrix defined as a global variable:
BOARD = [
[0, 0, 3],
[2, 0, 0],
[0, 0, 0]
]
However, when manipulating this matrix, I want to leave the original untouched and instead work on a copy. So I did this in the main function in order to make an independent copy:
board = BOARD[:]
As expected, the results of board == BOARD and board is BOARD are True and False, respectively. So when I make an edit like so board[0] = [1, 0, 3], then only board is changed, which is fine. However, if I do board[0][0] = 1, then BOARD also changes, which is not what I expected. And stranger still, board is BOARD is still False.
Why is there this inconsistent behaviour and any suggestions on how I can get around it?
EDIT:
If I run:
board = BOARD[:]
board[0] = [1, 0, 3]
board[0][0] = 2
then BOARD does not change.
But I only added the single indexing for comparison. In the real program I want to be able to do this:
board = BOARD[:]
board[0][0] = 2
But this is where BOARD does change, even though I would like it to stay constant.
I'm not getting this error. Here is my code:
BOARD = [
[0, 0, 3],
[2, 0, 0],
[0, 0, 0]
]
board = BOARD[:]
print(board == BOARD) # prints True
board[0] = [1,0,3]
print(board == BOARD) # prints False
board[0][0] = 1
print(board == BOARD) # still prints False
After reading AkThao's clarification I understood what was going on.
Python uses pointers to implement lists.
For example, if you set a = [1,2,3] and b = a. If you then modify b[0] = 5, a will also be modified because both a and b are pointers to the same memory location.
2D lists are implemented using a pointer to pointers (double pointer), i.e. a list of pointers like [p1, p2, p3].
When you do board = BOARD[:], it creates a copy of the list, but it does not change the actual value of the pointers (the elements of the list). So, the list board will is essentially just a copy of the 3 different pointer values.
This is why when you do board[0][0] = 1, it changes the value of BOARD as well (because the actual pointer stored by board is unchanged).
When you do board[0] = [1, 0, 3] as I have done, you are changing the actual value of the pointer, which is why the original BOARD does not change afterwards.
Why does BOARD change at all?
BOARD and b are both references.
When coding vec = b you assign b address to BOARD. Therefore changing data in b will also "change" BOARD.
I have also tried every method to copy a list ,but one method was succesful,
b=copy.deepcopy(BOARD)
although the deepcopy is time consuming for large lists you can try it.
import copy
B = [
[0, 0, 3],
[2, 0, 0],
[0, 0, 0]
]
print(B)
b=copy.deepcopy(B)
b[0][0] = 2
print(b)
print(B)
output:
[[0, 0, 3], [2, 0, 0], [0, 0, 0]]
[[2, 0, 3], [2, 0, 0], [0, 0, 0]]
[[0, 0, 3], [2, 0, 0], [0, 0, 0]]
hope this helps you!

How to compute the 4 X 4 homogeneous transformation matrix for an image in python

I have an image and I want to obatain the homogeneous transformation matrix of that Image in python. Image.
Can someone please help me?
Thank you very much.
A translation matrix (a transformation matrix for translation) is of the form -
[[1, 0, 0, tx],
[0, 1, 0, ty],
[0, 0, 1, tz],
[0, 0, 0, 1]]
You could use imutils library which has convenient implementations of these transformations or refer to the official opencv docs.
I hope this helps.

Resources