I am trying to write a simple mesh exporter in maxscript. It's nothing fancy, it just has to export faces, vertices and tvertices. I have the code as good as working, but sometimes I get really weird values in vertex positions (-1.1234e-005 for example). I understand it is some kind of really big number, but the problem is, my verts aren't anywhere near the position that number indicates (I have seen this happen with a 1m*1m*1m box). I have found that when it happens with a mesh, it always happens with that mesh and with the same vertex, untill I move that specific vertex (scaling/moving the whole thing doesn't work). I use this code to export the vertex positions:
num_verts = sel_mesh.numverts
for i=1 to num_verts do (
v = getVert sel_mesh i
format "v %\n" v to:out_file
)
format "\n" to:out_file
I have tried Googling the problem, but no one seems to have the same issue. I use the same code for my tvertices and those are exported perfectly fine. I can post the whole exporter if neccesary. Please let me know if you need to see more code :).
This is infact a very small number.
-1.1234e-005 is -1.1234 * (10 ^ -5), which is very small.
Contrary to your comment, formattedPrint does 'fix' this.
formattedPrint -1.1234e-005 format:".6f"
output: "-0.000011"
You can use it as such in your exporter:
num_verts = sel_mesh.numverts
for i=1 to num_verts do (
v = getVert sel_mesh i
format "v %\n" (formattedPrint v format:".6f") to:out_file
)
format "\n" to:out_file
Related
Ok, so what I want to do is go through an entire array of pixels, and for each pixel grab the green value, blue value and red value. This is so later I can see patterns between them.
So, this is what my code looks like.
for frame in camera.capture_continuous(rawCapture, format='bgr', use_video_port =True):
data=frame.array
rawCapture.truncate(0)
ColourCount = Calculations(data)
Ok, now that was just to show you what the general code is. This is where it gets tricky. Also, if that doesn't work for you guys on Pc, just use any opencv, I'm sure they save the data the same.
Calculations(data):
for n in data:
for s in n
B=s[0]
G=s[1]
R=s[2]
Then when I print these, it doesn't yield the result I want.
So s is like [0, 14, 0]
And B is like [0, 0, 0] as is the others. (those are just examples, although the values aren't getting high for some reason.
Now what I would like, is to split it so B is the blue channel, G is the green channel, etc.
So for the N example:
B= 0
G= 14
R = 0
So how do I achieve this?
I want single integers, but it's sending me back an entire pixel nearly?
EDIT: I believe part of my mistake before was that the item I was collecting was in fact a row instead of a pixel. So I put another for loop in, as seen above.
You can use the split() function.
Calculations(data):
bgr = cv2.split(data)
B = bgr[0]
G = bgr[1]
R = bgr[2]
I just discover gnuplot 4.6 and the beautiful loop tool.
I want to plot a curve with different x axis, but it doesnt work.
I have a file called file.txt, where there is a list of data like :
E002 = ...
E003 = ...
.
.
.
E021 = ...
The point is to shift the x axis of each plot with the corresponding data, something like this :
load 'file.txt'
plot for [a=2:21] 'my_data_file.dat' u ($1+'E00'.a ):a w l
But this doesn't work, and I have the error : 'Non-numeric string found where a numeric expression was expected'.
I do not know how to bypass this issue.
Second question,
i would like after to sum all the column but shifted like before. Something like :
($1+E002):$2 + ($1+E003):$3 +...
Is there a way to do that ?
For the first question, you need to use the value to get the value of a variable.
I suggest to use a more versatile sprintf command to manipulate strings:
plot for [a=2:21] 'my_data_file.dat' u ($1+value(sprintf('E%03d',a))):a w l
Type help value and help sprintf to get more info about those commands
I don't understand very well the second question, maybe something like this could help?
my_sum=0
plot for [a=2:21] my_val=value(sprintf('E%03d',a)), my_sum=my_sum+my_val, 'my_data_file.dat' u ($1+my_val):a w l
print my_sum
the last line, should print the sum of all you Exxx values.
This seems like a trivial problem, though I've been hitting myself over the head with it for too long.
This doesn't even plot just the (0,0) -- I can't seem to find much about plotting from arrays -- rather just matrix plots (and only columns at that).
The data is properly in these arrays, I just need to make plots! Doesn't seem so complicated. I don't even need separate colors for the different sets...just all one big scatter plot.
Any suggestions?
pdf(mypath)
# Plot first point
plot(0,0, col = "blue", type = "n", xlab="PES", ylab=""%eff")
#Loop to Plot remaining points
for(rows in 1:nrowX)
{
for(cols in 1:ncolX)
{
points(X[rows,cols],Y[rows,cols], col = "blue", type = "p")
}
}
dev.off
I have also tried using plot.new() to have an empty plot...but no such luck.
SOLUTION!!
Turns out I'm just a fool. Code is acurate and the suggestions below do indeed work.
R happened to be open in another tab and since it was open, never let go of the plot (why? I don't know). As soon as it was closed, the plot appeared. Now I can get my plot again and again...
Thanks to everyone who tried helping a problem that wasn't a problem!
I like this place already!
When you set type = "n", the plot function will not plot anything at all. It is used to set up a basis for the rest of the plot (like axis labels, limits etc). That is why the first point at (0, 0) does not show up.
The rest of the points are probably outside the range. Use xlim and ylim to set up the ranges properly. I'm going to assume X and Y have the same size and dimension. Try this:
pdf(mypath)
# Set up the plot
plot(0, type="n", xlab="PES", ylab="%eff", xlim=range(X), ylim=range(y))
# Now plot
points(X,Y, col="blue")
dev.off
Of course you could let the plot function take care of the limits for you:
pdf(mypath)
plot(X, Y, xlab="PES", ylab="%eff")
dev.off()
Your initial plot will set up the coordinates, but since you only give it one point it does not know how much room to leave around the 0,0 point (so it does not leave very much). I expect that the rest of your points fall outside of that range which is why they don't show up on the plot (you can use par("usr") to see what the extents are).
When you create the initial plot you should include xlim and ylim arguments so that the plot includes the area where the new points will be added, something like:
plot(0,0, type='n', xlim=range(X), ylim=range(Y))
You may also be interested in the matplot function which will take a matrix as either or both the x and/or y argument and plot accordingly.
Edit
The following works for me:
X <- matrix( runif(390), nrow=10 )
Y <- matrix( rnorm(390), nrow=10 )
plot(0,0, col = "blue", type = "n", xlab="PES", ylab="%eff",
xlim=range(X), ylim=range(Y))
#Loop to Plot remaining points
for(rows in 1:nrow(X))
{
for(cols in 1:ncol(X))
{
points(X[rows,cols],Y[rows,cols], col = "blue", type = "p")
}
}
I did remove an extra " from the ylab, was that your problem?
But
plot(X,Y)
also worked without the looping.
Check with just the console to see if it works before worrying about sending to a pdf file. If this has not fixed it yet, we still need more details.
I have a code to crop connected components of input image, input, by finding the boundary conditions from a binary image's labelled map, labelledmap ([labelledmap, labelcount] = bwlabel(hvedged, 8);)
I'm new to matlab so this might sound stupid..
The problem is, I am unable to store different cropped images in the same variable, Because matlab seems to merge the ends of the already existing image and the new cropped image, i.e, it is storing the complete map between the two cropped images, the way i see it :/
This is the output Using different variables for storing cropped image (the kind of output i want)
Output Using different variables for storing cropped image
This is the output i'm getting by storing the cropped image in the same variable(not helpful)
Output when storing cropped image in the same varible
I tried using an array of size equal to total number of labels produced but it's giving the same result.. also i tried clearvars for clearing the output token image, ltoken, after every iteration of the loop but it's not helping
So, is there any possible way to display individual cropped images.. also the number of cropped images might be in thousands so i want to use a loop to code their cropping mechanism
here is a part of the code attached.. thanks in advance ;)
for h=1:labelcount
for i=1:r
for j=1:c
if labelledmap(i,j)==h
if i<ltop
ltop=i;
end
if i>lbottom
lbottom=i;
end
if j<lleft
lleft=j;
end
if j>lright
lright=j;
end
end
end
end
if ltop>5
ltop=ltop-5;
end
if lbottom<r-5
lbottom=lbottom+5;
end
if lleft>5
lleft=lleft-5;
end
if lright<c-5
lright=lright+5;
end
lwidth=lright-lleft;
lheight=lbottom-ltop;
ltoken=imcrop(input,[lleft ltop lwidth lheight]);
figure('Name', 'Cropped Token'), imshow(ltoken);
clearvars ltoken;
end
you need to initialize ltop lbottom lleft and lright for each iteration of label h. I think this is the reason why you get the cropped images "glued" together.
It is EXTREMELY inefficient to go through all the pixels for each and every one of your labels. Especially when you are expected to have many labels.
Use regionprops to get the 'BoundingBox' property for each label.
Here's an example
st = regionprops( labelledmap, 'BoundingBox' );
imlist = cell( 1, numel(st) ); % pre-allocate
for ii=1:numel(st)
r = st(ii).BoundingBox;
% I understand you want to increase the BB by 5 pixels at each side:
r(1:2) = r(1:2) - 5; % start point moves -5
r(3:4) = r(3:4) + 10; % width and height increases by 10
imlist{ii} = imcrop( input, r );
end
I'm still a bit in shock by your code that explicitly loops through all pixels just for finding the bouding box. This is NOT the matlab way of doing things.
If you insist on NOT using regionprops here's a more Matlab-ish way of finding the ii-th bounding box:
imsk = (labeledmap == ii); % create a binary map with True for ii-th region
xFlat = any(imsk,1); % "flattening" imsk on the x-axis
lleft = find( xFlat, 1, 'first' );
lright = find( xFlat, 1, 'last' );
yFlat = any(imsk, 2);
ltop = find( yFlat, 1, 'first' );
lbottom = find( yFlat, 1, 'last' );
No loops at all over image coordinates.
The problem set asks me to create two triangles, defining them using points, and then checking if they're similar.
I did first part: created a struct point and a struct triangle, as the profesor told us to. To solve the problem of checking similarity, I thought I could use the points to define vectors, and them use the law of cosines to calculate its angles, together with some if sentences to check if the triangles are similar.
Which codes could help me achieve that? I could not find anything that I'd be able to turn into a partial solution.
What you said does the trick!
For the first triangle, take some measures, like as you said: an angle (or its cosine - easy to calculate with a dot product) on any vertex and the lengths of the sides next to it.
For another triangle, use if-conditions to see if the angle (or its cosine) is the same, and if the ratios of the lengths are also the same. You'd have to do this check from all 3 vertices in this way (if at least one fits, then the triangles are similar).
A faster way would be to always start with (for instnace) the vertex with the smallest angle, then you'd need to only compare once.
Now go code it! :-)
You are given coordinates of all three points of each triangle. Let us consider two triangles T1 A(a1,a2) B(b1,b2) C(c1, c2), T2 P(p1,p2) Q(q1,q2) R(r1,r2).
a = length of opposite side of vertex A
b = length of opposite side of vertex B
c = length of opposite side of vertex C
similarly p,q,r of triangle T2
So, for the two triangles to be similar, it has to follow the following conditions
1. AB = PQ; BC = QR; CA = RP
(We don't need their directions, So I am considering only magnitudes)
2. angle (A) = angle(B) i.e angle(BAC) = angle(QPR);
angle(B) = angle(Q) i.e angle(CBA) = angle (RQP) and
angle(C) = angle(R).
Now, you got to use coordinate geometry/ spherical geometry here.
COS (A) = ( b^2 + c^2 - a^2 )/2bc
COS (B) = ( c^2 + a^2 - b^2 )/2ac
COS (C) = (a^2 + b^2 - c^2)/2ab
Note:: As cosine is periodic with 2*pi, please make sure that you have exact angle. So, why don't you think of using inverse cosine functions where you get principle angles.(I am not sure of them, as how they work. please do check)
(Similarly for P,Q,R of triangle T2).
Actually there is another rule by which its easy to do.
law: a/sin(A) = b/sin(B) = c/sin(C).
I think you have to go through Spherical Geometry
I hope this helps you to do the program.
How to do the program:
Actually, its fine if you want to use structures. Create a structure with fields of 3 sides and 3 angles. Thus you need to take two variables under structure type and compare those quantities mentioned above.
If they satisfy, they are similar triangles.
I hope this helps you.