cv2.addweight() throws error: functions have different types, the output array type must be explicitly specified in function 'cv::arithm_op' - opencv

I'm using opencv-python in VSCODE, I create 2 2-dimensions arrays of the same type and same size. but I got this error when it executes the method cv2.addWeighted, I see other people are asking the same question, but their problem is the different data type. However, I have double checked the data type in my code, they are the same (np.uint8).
img01 = np.ones((200, 200), dtype=np.uint8) * 1000
img02 = np.ones((200, 200), dtype=np.uint8) * 100
weightedImg = cv2.addWeighted(img01,0.6,img02,0.4,3)
error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'cv::arithm_op'
I would like to know what's wrong in my code. thanks.

Related

What does 'RuntimeError: PyFunctionConstraint: Output must be of scalar type AutoDiffXd, but unable to infer scalar type.' mean?

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.

Invalid type conversion while using ANSI c

I am facing this problem as when I am trying to build the code using ANSI C, as I was practicing writing in it and dealing with its rules, it tells me invalid type conversion and I don't know what to do.
this is the code line that makes the error, it is a pointer to function:
((CanIf_FuncTypeCanSpecial)(entry->CanIfUserRxIndication))(
entry->CanIfCanRxPduHrhRef->CanIfCanControllerHrhIdRef,
entry->CanIfCanRxPduId,
CanSduPtr,
CanDlc,
CanId);
and this is howentry->CanIfUserRxIndication is declared, as void *CanIfUserRxIndication;
and this is how CanIf_FuncTypeCanSpecial is declared, as
typedef void (*CanIf_FuncTypeCanSpecial)
(uint8 channel, PduIdType pduId, const uint8 *sduPtr, uint8 dlc, Can_IdType canId);
every parameter in the conversion type is the same type as the input parameters except the first one entry->CanIfCanRxPduHrhRef->CanIfCanControllerHrhIdRef it is from type enum not uint8.
You can find the code on GitHub.
and also the MISRA Rule is telling me this:
#1398-D (MISRA-C:2004 11.1/R) Conversions shall not be performed between a pointer to a function and any type other than an integral type
I tried to convert from enum to uint8 to make all of the parameters as what the type conversion CanIf_FuncTypeCanSpecial takes, but nothing happened.
If I understand correctly, you are trying to cast an existing function to match a function pointer declaration that has a differing argument type. You can cast the parameters and call such a function, but because function pointers themselves may be used anywhere in the program, at the places where they would be used the code would not know what to cast (which may result in a size difference) this is illegal.

how to cv2.imencode and imdecode (jpeg) for diffret QF values?

I need to use open cv functions: cv2.imencode,cv2.imdecode to compress (jpeg) and decompress (jpeg) for different QF values.
The picture is 'bridge.ppm' from https://imagecompression.info/test_images/
I've tried:
bridge = cv2.imread('./bridge.ppm')
bridge_en = cv2.imencode('.jpeg', bridge)
bridge_de = cv2.imdecode('.jpeg', bridge_en)
cv2.imshow('image',bridge_de)
but I'm getting an error in the 2nd line saying: "Expected Ptr<cv::UMat> for argument 'buf'".
Also, how can I change and test different QF values?
Please take a look to the documentation for imencode and imdecode
imencode returns two values, the encoded buffer is the second one. And imdecode accepts the encoded buffer and a flag. So:
bridge = cv2.imread('./bridge.ppm')
bridge_en = cv2.imencode('.jpeg', bridge)[1] # you need the second value
bridge_de = cv2.imdecode(bridge_en, cv2.IMREAD_UNCHANGED) # or any other flag, same as 'imread'
cv2.imshow('image',bridge_de)

scilab - Implementing log function on images

I want to apply a log function to images. But it fails showing this error: function is not defined on this type of argument.
uk=imread('image.jpg');
result=log(uk(:,:,1));
I think your problem is that imread returns a matrix of uint8 type. To apply log, you should convert it to double. There are at least 2 ways to do this, one built in, and one from SIVP:
clc;
clear;
im = imread("d:\Attila\PROJECTS\Scilab\Stackoverflow\mixer_crop.jpg");
//imshow(im);
disp(typeof(im(:,:,1)),"Original type:");
//use double
M = double(im(:,:,1));
disp(typeof(M),"Modified type:");
result=log(M);
//imshow(uint8(M));
//use im2double
M2 = im2double(im);
disp(typeof(M2(:,:,1)),"Modified type 2:");
result=log(M2(:,:,1));
//imshow(im2uint8(M2));

Using the `in` keyword causes "E1012 Constant expression violates subrange bounds" in Delphi

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;

Resources