How does TersorFlow expect flattened images? - image-processing

When performing 2D convolutions in TersorFlow using the conv_2d layer, does it expect the pixels to be lined up as
[
[img[i].red, img[i].green, img[i].blue],
[img[i+1].red, etc.],
]
Or
[
[mg[i].red, img[i+1].red, etc.],
[mg[i].green, img[i+1].green, etc.],
]
or some other way?

2D convolutions expect a 4-d tensor as input with the following shape:
[batch_size, image_height, image_width, channel_size]
In case of rgb images the channels are the three colors. Therefore the pixel should be lined up as:
[
[
[img[i,j].red, img[i,j].green, img[i,j].blue],
[img[i, j+1].red, img[i, j+1].green, img[i, j+1].blue],
etc
],
[
[img[i+1,j].red, img[i+1,j].green, img[i+1,j].blue],
[img[i+1, j+1].red, img[i+1, j+1].green, img[i+1, j+1].blue],
etc
],
etc
]
(with img[y_coordinate, x_coordinate] and img[i,j] = img[i*image_width + j])

Related

Adding Bias to Preferential Attachment Network in NetLogo

I'm very new to NetLogo, and am attempting to incorporate bias into the Preferential Attachment model by making the probability of attachment depend on a node's color as well as degree; nodes will have a % bias (determined by a slider) to choose to link with a node of the same color as themselves.
So far, I've made the nodes heterogenous, either blue or red, with blue being ~70% of the population:
to make-node [old-node]
create-turtles 1
[
set color red
if random 100 < 70
[ set color blue ]
if old-node != nobody
[ create-link-with old-node [ set color green ]
;; position the new node near its partner
move-to old-node
fd 8
]
]
end
I understand the main preferential attachment code, which has the new node select a partner proportional to its link count:
to-report find-partner
report [one-of both-ends] of one-of links
end
Where I encounter issues is in the following procedure. Sometimes it works as expected, but sometimes I get the following error message: OF expected input to be an agent or agentset but got NOBODY instead - regarding the statement "[one-of both-ends] of one-of links" under my 'while' loop.
to make-node [old-node]
create-turtles 1
[
set color red
set characteristic 0
if random 100 < 70
[ set color blue
set characteristic 1]
if (old-node != nobody) and (random 100 < bias-percent) and (any? turtles with [color = red]) and (any? turtles with [color = blue])
[
while [characteristic != [characteristic] of old-node]
[
set old-node [one-of both-ends] of one-of links
]
]
if old-node != nobody
[ create-link-with old-node [ set color green ]
;; position the new node near its partner
move-to old-node
fd 8
]
]
end
Any help is appreciated!!!

Different results in cv2.omnidir.projectPoints when projecting target-to-image and camera-to-image

I am using a checkerboard target to extrinsically calibrate an omnidirectional camera with another sensor. For that purpose, I utilize the function cv2.omnidir.projectPoints twice, and it unexpectedly returns different results.
Here I show a projection of the outer corners of the checkerboard to the image. In the first case the projection is done from the board coordinate system (CS) to the image CS. In the second case, I manually project the corners from board CS to the camera CS, and then project it to the image, using rvec=tvec=(vector of zeros).
import numpy as np
import cv2
# --- Camera parameters
K = np.array([[939.265, 0. , 965.693],
[ 0. , 942.402, 645.578],
[ 0. , 0. , 1. ]])
D = np.array([-0.156, -0.03 , 0. , 0.001], dtype=float32)
# --- Corners in board coordinate system
outer_corners_bcs = np.array([[-0.175, -0.17 , 0. ],
[ 0.8 , -0.17 , 0. ],
[-0.175, 0.67 , 0. ],
[ 0.8 , 0.67 , 0. ]])
# --- Rotation & Translation vectors from checkerboard target to camera
rvec = np.array([[-1.138, -1.421, 2.827]])
tvec = np.array([[-6.852, -5.473, 4.549]])
# --- Project from board to image
uv, _ = cv2.omnidir.projectPoints(outer_corners_bcs.reshape(1, -1, 3).astype('float64'),
rvec.reshape(1, 3),
tvec.reshape(1, 3), K, 1, D)
print(uv)
[[[556.417 320.275]
[504.347 320.397]
[562.968 272.863]
[509.031 272.395]]]
# --- Convert to camera coordinate system and project to image
R = eulerAnglesToRotationMatrix(rvec)
outer_corners_ccs = (R # outer_corners_bcs.T + tvec).T
uv, _ = cv2.omnidir.projectPoints(outer_corners_ccs.reshape(1, -1, 3),
np.zeros(3).reshape(1, 3),
np.zeros(3).reshape(1, 3), K, 1, D)
print(uv)
[[[699.384 304.698]
[639.438 180.463]
[876.787 527.013]
[752.581 334.091]]]
My expectation is that the results will be identical.
Thanks for any help!
I figured this out:
Instead of:
R = eulerAnglesToRotationMatrix(rvec)
(taken from here btw)
It should be written:
R, _ = cv2.Rodrigues(rvec)

Matrix Transformation for image

I am working on an image processing project in python in which I am required to change the coordinate system
I thought it is analogous to matrix transformation and tried but it is not working, I have taken the coordinates of the red dots
Simply subtract by 256 and divide by 512. The connection is that you see values of 256 get mapped to 0. Therefore, 0 gets mapped to -256, 256 gets mapped to 0 and 512 gets mapped to 256. However, you further need the values to be in the range of [-0.5, 0.5]. Dividing everything by 512 finishes this off.
Therefore the relationship is:
out = (in - 256) / 512 = (in / 512) - 0.5
Try some values from your example input above to convince yourself that this is the correct relationship.
If you want to form this as a matrix multiplication, this can be interpreted as an affine transform with scale and translation, but no rotation:
[ 1/512 0 -0.5 ]
K = [ 0 1/512 -0.5 ]
[ 0 0 1 ]
Take note that you will need to use homogeneous coordinates to achieve the desired result.
For example:
(x, y) = (384, 256)
[X] [ 1/512 0 -0.5 ][384]
[Y] = [ 0 1/512 -0.5 ][256]
[1] [ 0 0 1 ][ 1 ]
[X] [384/512 - 0.5] [ 0.25 ]
[Y] = [256/512 - 0.5] = [ 0 ]
[1] [ 1 ] [ 1 ]
Simply remove the last coordinate to get the final answer of (0.25, 0).

Pure Stohastic Gradient Descent scaling anomaly

I train linear regression (y = b0 + b1 * x) with SGD, batch_size=1 and one data pass (epoch). It converges when scaling the input to unreasonable values (see below). When scaling to "standard" values (calculating unit variance and mean on the input dataset) I cannot get convergence to any reasonable error value, no matter how I change learninig rate/iterations. Both x and y are divided to scale_ and centered to mean_.
Training data:
x = [1 .. 70] y = [15.39 ... 356.76]
Standard scaling:
iterations = 500 learning_rate=0.00003
x.scale_ [ 20.20519735] x.mean_ [ 35.5] y.scale_ [ 100.3283148] y.mean_ [ 187.43898263]
Abnoraml scaling: iterations=500 learning_rate=0.001 x.scale_ [ 2.5] x.mean_ [ 2.5] y.scale_ [ 3.] y.mean_ [ 3.]
You can see that the learning rate is adjusted according to the scales quotient, so this is not just a learning rate problem (I've tried different values as well).

how to generate such an image in Mathematica

I am thinking of process an image to generate in Mathematica given its powerful image processing capabilities. Could anyone give some idea as to how to do this?
Thanks a lot.
Here's one version, using a textures. It of course doesn't act as a real lens, just repeats portions of the image in an overlapping fashion.
t = CurrentImage[];
(* square off the image to avoid distortion *)
t = ImageCrop[t, {240,240}];
n = 20;
Graphics[{Texture[t],
Table[
Polygon[
Table[h*{Sqrt[3]/2, 0} + (g - h)*{Sqrt[3]/4, 3/4} + {Sin[t], Cos[t]},
{t, 0., 2*Pi - Pi/3, Pi/3}
],
VertexTextureCoordinates -> Transpose[{
Rescale[
(1/4)*Sqrt[3]*(g - h) + (Sqrt[3]*h)/2.,
{-n/2, n/2},
{0, 1}
] + {0, Sqrt[3]/2, Sqrt[3]/2, 0, -(Sqrt[3]/2), -(Sqrt[3]/2)}/(n/2),
Rescale[
(3.*(g - h))/4,
{-n/2, n/2},
{0, 1}
] + {1, 1/2, -(1/2), -1, -(1/2), 1/2}/(n/2)
}]
],
{h, -n, n, 2},
{g, -n, n, 2}
]
},
PlotRange -> n/2 - 1
]
Here's the above code applied to the standard image test (Lena)
"I think this could be well approximated with a calculated offset for the image in each cell" - Mr.Wizard
Exactly! As you can see from reconstructed image there is no lens effect and tiles are just displacements.
What you need is a Hexagonal_tessellation and a simple algorithm to calculate displacement for each hexagon from some chosen central point (weight/2, height/2).

Resources