I'm having a problem regarding reading the pixel values (w=30, h=10). Suppose I'm using
int readValue = cvGetReal2D(img,y,x); and
int readValue = data[y*step+x];
Lets say I am trying to access pixel values at w=35, h=5 using the (1) and (2) method.
The (1) will output an error of index out of range. But why (2) does not output an error of index out of range?
After that, I'm trying to use try...catch()...
You have a continuous block of memory of
size = w*h = 300
At w = 35 and h = 5 your equation gives
data[5*30+35] = data[190] < data[300]
so there is no error. If this is c++ then even if your index in data was larger than 299 it wouldn't throw an error. In that case you would be accessing the data beyond its bounds which results in undefined behavior.
I assume cvGetReal2D(img,y,x) is smart enough to tell you that one of your indices is larger than the defined size of that dimension even though it could be resolved to a valid address.
Related
When attempting to solve my mathematical program, drake produces the RuntimeError: PyFunctionConstraint: Output must be of scalar type AutoDiffXd, but unable to infer scalar type.
The constraint responsible for the error is as follows:
def non_penetration_constraint(q):
plant_ad.SetPositions(context_ad, q)
Distances = np.zeros(6, dtype=q.dtype)
i = 0
for name in Claw_name:
g_ids = body_geometries[name]
assert(len(g_ids) == 1)
Distances[i] = query_object.ComputeSignedDistancePairClosestPoints(
Worldbody_id, g_ids[0]).distance
i += 1
return Distances
Called with:
for n in range(N-1):
# Non penetration constraint
prog.AddConstraint(non_penetration_constraint,
lb = [0] * nf,
ub = [0] * nf,
vars = (q[:, n]))
Has anybody had a similar issue before?
I suspect that your query_object (and perhaps other values) are not from the autodiff version of your plant/scene_graph.
Constraints implemented like this can potentially be called with q[0] either a type float or type AutoDiffXd. See the "writing custom evaluator" section of this Drake tutorial. In your case, the return value from your constraint is coming out of the query_object, which is not impacted directly by the plant_ad.SetPositions call (which seems suspicious). You'll want to make sure the query_object is generated from the autodiff scene_graph, and presumably after you set the positions to your constraint value.
The error I get:
query.lua:25: attempt to compare number with string
The rest of my code - I'm not sure why its not working, because I made sure that both values were in fact numbers, and the error still keeps happening, I have tried looking it up, and I cant find a solution, any help would be greatly appreciated.
print("Enter L,W,H values")
x = read()
y = read()
z = read()
length = 5
width = 5
height = 5
length = x
width = y
height = z
volume = length * width * height
print(volume.." blocks to mine")
turtle.refuel()
turtleFuel = turtle.getFuelLevel()
fuelNeeded = turtleFuel - volume
if fuelNeeded >= 0 then
print("Enough Fuel Detected")
else
print("not Enough fuel, error,"..fuelNeeded..", fuel required "..volume)
end
length1 = length
while length1 > 0 do
turtle.forward()
length1 = length1 - 1
end
In Computercraft, read() returns a string. You should change the first few lines of your code:
Instead of
x = read()
y = read()
z = read()
write
x = tonumber(read())
y = tonumber(read())
z = tonumber(read())
Do note, that read() is not the same as io.read(). One is a standard Lua function (which is also implemented in Computercraft) and read() is a function created specifically for CC computers and that can take a few extra parameters, and that you can use to do a few tricks once you get more advanced. (I am saying this because someone in the comments mentioned checking io.read())
Also, you mentioned that you "checked the type of the variable in the editor". Lua is a dynamically typed language, and the type of a variable CANNOT be known before the program is actually run. What your editor is probably doing is guessing the type of the variable based what it expects your program to do. These guesses are almost always inaccurate. That's why you should understand what your program is doing, and not rely on your editor. If you want to be extra sure, you want to run some tests, or the type of the variable simply is not known before the program is actually executed (because it may vary), you can use type(), like you have, to check its type at runtime. type() returns a string, representing the type of the variable:
type(nil) ---> nil
type("helloworld") ---> "string"
type(42) ---> "number"
type(function()print("HELLO")end) ---> "function"
type({1,2,3}) ---> "table"
I have a problem where I need to do a linear interpolation on some data as it is acquired from a sensor (it's technically position data, but the nature of the data doesn't really matter). I'm doing this now in matlab, but since I will eventually migrate this code to other languages, I want to keep the code as simple as possible and not use any complicated matlab-specific/built-in functions.
My implementation initially seems OK, but when checking my work against matlab's built-in interp1 function, it seems my implementation isn't perfect, and I have no idea why. Below is the code I'm using on a dataset already fully collected, but as I loop through the data, I act as if I only have the current sample and the previous sample, which mirrors the problem I will eventually face.
%make some dummy data
np = 109; %number of data points for x and y
x_data = linspace(3,98,np) + (normrnd(0.4,0.2,[1,np]));
y_data = normrnd(2.5, 1.5, [1,np]);
%define the query points the data will be interpolated over
qp = [1:100];
kk=2; %indexes through the data
cc = 1; %indexes through the query points
qpi = qp(cc); %qpi is the current query point in the loop
y_interp = qp*nan; %this will hold our solution
while kk<=length(x_data)
kk = kk+1; %update the data counter
%perform online interpolation
if cc<length(qp)-1
if qpi>=y_data(kk-1) %the query point, of course, has to be in-between the current value and the next value of x_data
y_interp(cc) = myInterp(x_data(kk-1), x_data(kk), y_data(kk-1), y_data(kk), qpi);
end
if qpi>x_data(kk), %if the current query point is already larger than the current sample, update the sample
kk = kk+1;
else %otherwise, update the query point to ensure its in between the samples for the next iteration
cc = cc + 1;
qpi = qp(cc);
%It is possible that if the change in x_data is greater than the resolution of the query
%points, an update like the above wont work. In this case, we must lag the data
if qpi<x_data(kk),
kk=kk-1;
end
end
end
end
%get the correct interpolation
y_interp_correct = interp1(x_data, y_data, qp);
%plot both solutions to show the difference
figure;
plot(y_interp,'displayname','manual-solution'); hold on;
plot(y_interp_correct,'k--','displayname','matlab solution');
leg1 = legend('show');
set(leg1,'Location','Best');
ylabel('interpolated points');
xlabel('query points');
Note that the "myInterp" function is as follows:
function yi = myInterp(x1, x2, y1, y2, qp)
%linearly interpolate the function value y(x) over the query point qp
yi = y1 + (qp-x1) * ( (y2-y1)/(x2-x1) );
end
And here is the plot showing that my implementation isn't correct :-(
Can anyone help me find where the mistake is? And why? I suspect it has something to do with ensuring that the query point is in-between the previous and current x-samples, but I'm not sure.
The problem in your code is that you at times call myInterp with a value of qpi that is outside of the bounds x_data(kk-1) and x_data(kk). This leads to invalid extrapolation results.
Your logic of looping over kk rather than cc is very confusing to me. I would write a simple for loop over cc, which are the points at which you want to interpolate. For each of these points, advance kk, if necessary, such that qp(cc) is in between x_data(kk) and x_data(kk+1) (you can use kk-1 and kk instead if you prefer, just initialize kk=2 to ensure that kk-1 exists, I just find starting at kk=1 more intuitive).
To simplify the logic here, I'm limiting the values in qp to be inside the limits of x_data, so that we don't need to test to ensure that x_data(kk+1) exists, nor that x_data(1)<pq(cc). You can add those tests in if you wish.
Here's my code:
qp = [ceil(x_data(1)+0.1):floor(x_data(end)-0.1)];
y_interp = qp*nan; % this will hold our solution
kk=1; % indexes through the data
for cc=1:numel(qp)
% advance kk to where we can interpolate
% (this loop is guaranteed to not index out of bounds because x_data(end)>qp(end),
% but needs to be adjusted if this is not ensured prior to the loop)
while x_data(kk+1) < qp(cc)
kk = kk + 1;
end
% perform online interpolation
y_interp(cc) = myInterp(x_data(kk), x_data(kk+1), y_data(kk), y_data(kk+1), qp(cc));
end
As you can see, the logic is a lot simpler this way. The result is identical to y_interp_correct. The inner while x_data... loop serves the same purpose as your outer while loop, and would be the place where you read your data from wherever it's coming from.
I have a binary image which contains several separated regions. I want to put a threshold on the Area (number of pixels) that these regions occupy, in the way that: a region would be omitted if it has fewer pixels than the threshold. I already have tried these codes (using bwconncomp):
[...]
% let's assume threshold = 50
CC = bwconncomp(my_image);
L = labelmatrix(CC);
A = cell( size(CC.PixelIdxList,1) , size(CC.PixelIdxList,2) );
A = CC.PixelIdxList;
for column = 1 : size(CC.PixelIdxList,2)
if numel(CC.PixelIdxList{column}) < 50, A{column} = 0;
end
end
But at this point I don't know how to convert cell C back to the shape of my image and then show it! Are there any tricks to do that?
Is there any easier and straighter way to gain information about objects in an image than this one I used in here?
I also need to know length and width of these objects. These objects do not necessarily have any specific geometrical shape!
Thanks
Since no one took the effort to answer my question in here, I found it somewhere else. Now I'm coppying it to here, just in case if anyone novice like me might need to know that.
In order to know length and width of objects in an image:
labeledImage = bwlabel(my_image, 8);
regioninfo = regionprops(labeledImage , 'MajorAxisLength', 'MinorAxisLength');
lengths = [regioninfo.MajorAxisLength]; %array
widths = [regioninfo.MinorAxisLength]; %array
I've come across some rather unusual behaviour in a bit of Delphi code. When using the in keyword to check if an item is in a constant array, I get the following compilation error:
E1012 Constant expression violates subrange bounds
The constants are defined as follows:
type TSomeEnum = (seFoo = 1000,
seBar = 2000,
seBoo = 3000,
seFar = 4000,
seFooBar = 5000,
seBooFar = 6000,
seLow = 1000,
seHigh = 6000,
seCount = 6);
The line that is failing is the following:
if someObj.someProperty in [seFoo, seFar, seFooBar] then
...
Whilst I understand the reasoning behind the error showing in another question posted here, where bounds checking on integer arrays wasn't done at compile time when using a variable, it seems odd that I'm getting the same problem with a constant array which is most certainly within bounds.
For now, I've replaced the line with a (much larger) statement comprising of or clauses. However, this is clearly not ideal. Can anyone shed any light on why I'm getting this problem?
Documentation about Sets says :
The base type can have no more than 256 possible values, and their ordinalities must fall between 0 and 255.
So even if you can have enums of any value, the if xx in [a,b,c] statement will fail here, since a set cannot hold a value larger than 255.
Use a case statement instead:
case xx of
a,b,c : // Make something
end;