How to convert Bounding Box coordinates to Yolo Coordinates? - coordinates

I am trying to convert Bounding box coordinates to Yolo coordinates. The bounding box coordinates are not in the typical format. They look like this:
1,-1,855,884,94,195,1,-1,-1,-1
1,-1,1269,830,103,202,0,-1,-1,-1
1,-1,1023,909,86,170,0,-1,-1,-1
1,-1,879,681,76,191,0,-1,-1,-1
How do I use the 1s, -1s, and 0s to convert these coordinates to Yolo format?
I tried this code to convert them to Yolo:
def convert(filename_str, coords):
os.chdir("..")
image = cv2.imread(filename_str + ".jpg")
coords[2] -= coords[0]
coords[3] -= coords[1]
x_diff = int(coords[2]/2)
y_diff = int(coords[3]/2)
coords[0] = coords[0]+x_diff
coords[1] = coords[1]+y_diff
coords[0] /= int(image.shape[1])
coords[1] /= int(image.shape[0])
coords[2] /= int(image.shape[1])
coords[3] /= int(image.shape[0])
os.chdir("Label")
return coords
I get negative Yolo coordinates with this format:
0 0.2871825876662636 0.5 -0.46009673518742444 -0.637962962962963
0 0.4147521160822249 0.4777777777777778 -0.7049576783555018 -0.5814814814814815
0 0.3355501813784764 0.5 -0.5665054413542926 -0.6842592592592592
Thanks in advance

Try this:
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
im=Image.open(img_path)
w= int(im.size[0])
h= int(im.size[1])
print(xmin, xmax, ymin, ymax) #define your x,y coordinates
b = (xmin, xmax, ymin, ymax)
bb = convert((w,h), b)

Related

Wgs to Mercator

We have this function we have been using to convert Wgs coordinates to Mercator. The goal is to have a thin split in latitude towards the poles, and a large one close to the equator to make the 3d match the imagery of the texture, all in an equirectangular projection.
Currently our function looks like this:
WgsToMercator(coord)
{
yRadian = coord.y * Math.PI / 180.0;
sinLat = Math.Sin(yRadian);
y = 0.5 - Math.Log((1 + sinLat) / (1 - sinLat)) / (Math.PI * 4); /// valeur entre 0 et 1, 1 correspondant a -90 degres et 0 a 90 degres
return y;
}
Current result is:
WgsToMercator(90) = 0;
WgsToMercator(45) = 0.36
WgsToMercator(0) = 0.5;
WgsToMercator(-45) = 0.64
WgsToMercator(-90) = 1;
Expected result would be:
WgsToMercator(90) = 0;
WgsToMercator(45) = 0.14
WgsToMercator(0) = 0.5;
WgsToMercator(-45) = 0.86
WgsToMercator(-90) = 1;
My math are rusty and can't find a way to get the expected result. Thanks a lot by advance
I can get approximately your numbers with sine squared, after subtracting your input from 90, taking half of the result, and converting to radians.
Something like (in Octave/Matlab):
function output = WgsToMercator(coord)
rebased_angle = 90 - coord;
half_angle = 0.5 * rebased_angle;
angle_radians = half_angle * (3.1415/180.0);
output = sin(angle_radians)*sin(angle_radians);
end
Which gets:
>> WgsToMercator(90)
ans = 0
>> WgsToMercator(45)
ans = 0.14644
>> WgsToMercator(0)
ans = 0.49998
>> WgsToMercator(-45)
ans = 0.85353
>> WgsToMercator(-90)
ans = 1.00000
In C# it's:
public float WgsToMercator(float coord)
{
var rebasedAngle = 90.0f - coord;
var halfAngle = 0.5f * rebasedAngle;
var angleRadians = halfAngle * (Mathf.PI / 180.0f);
return Mathf.Sin(angleRadians) * Mathf.Sin(angleRadians);
}
:EDIT:
Here's a plot of the function I provided (in blue) and the points you've given (red circles).

Servo motor routing according to Matlab x, y, z coordinates

I work to about missile routing. I calculated throuhout the flight x,y,z coordinates of missile . I have data set about missile x,y,z coordinates. My goal is to move the servo motor according to x, y, z coordinates.
My input is 3-dimensional(x,y,z). I want to simulate in two dimensions. And for this I use vectoral calculation. The servo motor can take values between 0-1. But the result larger than 1 . When the results are reduced at the same rate, result is smaller than 0. But I get still the error
Undefined function 'writePosition' for input arguments of type 'matlab.graphics.chart.primitive.Surface'.
I will be grateful if you could help me.
My data example:
missile_x = 0.015
missile_y = 0.054
missile_z = 0.254
missile_flight = 0.00018794
My flight rotation code:
missile_x = vpa(Xval{id}(k)/10,5)
missile_y = vpa(Yval{id}(k)/10,5)
missile_z = vpa(Zval{id}(k)/10,5)
missile_flight = vpa(0.00555556*(missile_x^2+missile_y^2+missile_z^2)^1/2,5)
writePosition(s, missile_flight);
current_pos = readPosition(s);
current_pos = current_pos*missile_flight;
fprintf('Current motor position is %d degrees\n', current_pos);
pause(2);
Missile X,Y,Z calculation code:
dt = 0.005; %time step
g = 9.81; %gravity
ro = 1.2; %air density
A = pi*(0.2)^2; % reference area
Vmag = 0; % missile velocity vectoral value [m/sn]
t = 0;
T(1) = t;
U(1) = 0; %the missile is initially at rest at t = 0; So the velocity is 0
V(1) = 0;
W(1) = 0;
X(1) = X0;
Y(1) = Y0;
Z(1) = Z0;
n = 1;
h = interp2(x_terrain, y_terrain, h_terrain,X(1), Y(1));
while (Z(n) >= h)
[Thx, Thy, Thz] = thrust(t, Thmag0, theta, phi, Tburn, U(n), V(n), W(n));
Vmag = (U(n)^2 + V(n)^2 + W(n)^2)^(1/2);
Thmag = (Thx^2 + Thy^2 + Thz^2)^(1/2);
m = mass(t, m0, mf, Tburn);
[rho,sound_speed] = atmosphere(Z(n));
Cd = drag_coeff(Vmag,sound_speed);
U(n+1) = U(n) + (Thx/m - (Cd*rho*A/(2*m))*(U(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2)))*dt;
V(n+1) = V(n) + (Thy/m - (Cd*rho*A/(2*m)*(V(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2))))*dt;
W(n+1) = W(n) + (Thz/m - (Cd*rho*A/(2*m))*(W(n)*(U(n)^2+V(n)^2+W(n)^2)^(1/2)) - g)*dt;
X(n+1) = X(n) + U(n+1)*dt;
Y(n+1) = Y(n) + V(n+1)*dt;
Z(n+1) = Z(n) + W(n+1)*dt;
h = interp2(x_terrain, y_terrain, h_terrain, ...
X(end), Y(end));
t = t + dt;
T(n+1) = t;
n = n+1 ;
end
Tval = T;
Xval = X;
Yval = Y;
Zval = Z;

Convert Fisheye Video into regular Video

I have a video stream coming from a 180 degree fisheye camera. I want to do some image-processing to convert the fisheye view into a normal view.
After some research and lots of read articles I found this paper.
They describe an algorithm (and some formulas) to solve this problem.
I used tried to implement this method in a Matlab. Unfortunately it doesn't work, and I failed to make it work. The "corrected" image looks exactly like the original photograph and there's no any removal of distortion and secondly I am just receiving top left side of the image, not the complete image but changing the value of 'K' to 1.9 gives mw the whole image, but its exactly the same image.
Input image:
Result:
When the value of K is 1.15 as mentioned in the article
When the value of K is 1.9
Here is my code:
image = imread('image2.png');
[Cx, Cy, channel] = size(image);
k = 1.5;
f = (Cx * Cy)/3;
opw = fix(f * tan(asin(sin(atan((Cx/2)/f)) * k)));
oph = fix(f * tan(asin(sin(atan((Cy/2)/f)) * k)));
image_new = zeros(opw, oph,channel);
for i = 1: opw
for j = 1: oph
[theta,rho] = cart2pol(i,j);
R = f * tan(asin(sin(atan(rho/f)) * k));
r = f * tan(asin(sin(atan(R/f))/k));
X = ceil(r * cos(theta));
Y = ceil(r * sin(theta));
for k = 1: 3
image_new(i,j,k) = image(X,Y,k);
end
end
end
image_new = uint8(image_new);
warning('off', 'Images:initSize:adjustingMag');
imshow(image_new);
This is what solved my problem.
input:
strength as floating point >= 0. 0 = no change, high numbers equal stronger correction.
zoom as floating point >= 1. (1 = no change in zoom)
algorithm:
set halfWidth = imageWidth / 2
set halfHeight = imageHeight / 2
if strength = 0 then strength = 0.00001
set correctionRadius = squareroot(imageWidth ^ 2 + imageHeight ^ 2) / strength
for each pixel (x,y) in destinationImage
set newX = x - halfWidth
set newY = y - halfHeight
set distance = squareroot(newX ^ 2 + newY ^ 2)
set r = distance / correctionRadius
if r = 0 then
set theta = 1
else
set theta = arctangent(r) / r
set sourceX = halfWidth + theta * newX * zoom
set sourceY = halfHeight + theta * newY * zoom
set color of pixel (x, y) to color of source image pixel at (sourceX, sourceY)

Rotate line over a circle - Matlab

How can I rotate the line 360 degrees (every 20 degrees) over the circle and find the intersecting coordinates?
r = 1;
xc = 5;
yc = 5;
theta = linspace(0,2*pi);
x = r*cos(theta) + xc;
y = r*sin(theta) + yc;
plot(x,y)
axis equal
hold on
plot([xc xc-2],[yc yc])
You only need to construct a coarse linspace:
how_many_point = 360 / 20
coarse_theta = linspace(0, 2*pi, how_many_point + 1)
xs = xc + cos(coarse_theta)
ys = yc + sin(coarse_theta)
for i = 1:how_many_point
plot([xs(i) xc], [ys(i) yc]); hold on
end

Best way to get the bounding rectangle of a set of 3D points on a plane in Matlab

I need to get the four edges of the bounding rectangle of a set of 3D points stored as a 3xN matrix (tt). N >=4. The points lies on a plane.
Code sample:
% Simulate some points
deltaXY = 20;
[xx,yy] = meshgrid(-100:deltaXY:100,-100:deltaXY:100);
XYZ = [xx(:)'; yy(:)'; zeros(1,numel(xx))];
% Add some imperfection to data removing the top rigth point
maxXids = find(XYZ(1,:) == max(XYZ(1,:)));
maxYids = find(XYZ(2,:) == max(XYZ(2,:)));
id = intersect(maxXids,maxYids);
XYZ = removerows(XYZ',id)';
% Lets rotate a bit
XYZ = roty(5)*rotx(7)*rotz(0)*XYZ;
% Plot points
figure;
grid on;
rotate3d on;
axis vis3d;
hold on;
plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'.r');
% Find bounding rectangle
% ??? :(
%Currently I'm using this code:
tt = XYZ;
%Get the max and min indexes
minX = find(tt(1,:) == min(tt(1,:)));
minY = find(tt(2,:) == min(tt(2,:)));
maxX = find(tt(1,:) == max(tt(1,:)));
maxY = find(tt(2,:) == max(tt(2,:)));
%Intersect to find the common index
id1 = intersect(minX,minY);
id2 = intersect(maxX,minY);
id3 = intersect(maxX,maxY);
id4 = intersect(minX,maxY);
%Get the points
p1 = tt(:,id1(1));
p2 = tt(:,id2(1));
p3 = tt(:,id3(1));
p4 = tt(:,id4(1));
Sample points plot:
The problem is that intersect some times can be null, eg: if the points does not form a rectangle. Resulting this error:
Index exceeds matrix dimensions.
First solution : Use logical indexing to get rid of the find calls
p1=tt(:,tt(1,:)==min(tt(1,:))&tt(2,:)==min(tt(2,:)));
p2=tt(:,tt(1,:)==max(tt(1,:))&tt(2,:)==min(tt(2,:)));
p3=tt(:,tt(1,:)==max(tt(1,:))&tt(2,:)==max(tt(2,:)));
p4=tt(:,tt(1,:)==min(tt(1,:))&tt(2,:)==max(tt(2,:)));
Second solution : Use convhull to get the corners :
k=convhull(tt(1,:),tt(2,:));
Corners=[tt(:,k(1:end-1))];
Ok found a solution:
% Find bounding rectangle
tt = XYZ;
%Get the max and min indexes
minXids = find(tt(1,:) == min(tt(1,:)));
minYids = find(tt(2,:) == min(tt(2,:)));
maxXids = find(tt(1,:) == max(tt(1,:)));
maxYids = find(tt(2,:) == max(tt(2,:)));
%Intersect to find the common index
id1 = intersect(minXids,minYids);
id2 = intersect(maxXids,minYids);
id3 = intersect(maxXids,maxYids);
id4 = intersect(minXids,maxYids);
%Get the points
% Find affine plane on points
[np,~,pp] = affine_fit(tt');
% Converts to cart. eq.
% ax + yb + cz + d = 0
% Find d
a = np(1); b = np(2); c = np(3);
x = pp(1); y = pp(2); z = pp(3);
d = - (a*x + y*b + c*z);
% Get only one value
minX = min(tt(1,minXids)); maxX = max(tt(1,maxXids));
minY = min(tt(2,minYids)); maxY = max(tt(2,maxYids));
if numel(id1) == 0
x = minX; y = minY;
% Calculate z at xy.
z = - (d + a*x + y*b)/c;
p1 = [x y z]';
else
p1 = tt(:,id1(1));
end
if numel(id2) == 0
x = maxX; y = minY;
z = - (d + a*x + y*b)/c;
p2 = [x y z]';
else
p2 = tt(:,id1(1));
end
if numel(id3) == 0
x = maxX; y = maxY;
z = - (d + a*x + y*b)/c;
p3 = [x y z]';
else
p3 = tt(:,id1(1));
end
if numel(id4) == 0
x = minX; y = maxY;
z = - (d + a*x + y*b)/c;
p4 = [x y z]';
else
p4 = tt(:,id1(1));
end
ps = [p1 p2 p3 p4];
plot3(ps(1,:),ps(2,:),ps(3,:),'ob');