Pointer on a float matrix is considered as *uchar - c++

I'm trying to iterare trough a cv::Mat with a pointer.
So I did the following in my function:
template<typename Tin=uchar,typename Tout=float>
inline cv::Mat_<Tout> dct(const cv::Mat_<Tin>& oBlock) {
cv::Mat_<Tout> oOutput(oBlock.size());
Tout *pointeurOut= oOutput.data;
//...
}
However, I'm getting an error at the last line (Tout *pointeurOut= oOutput.data;).
'initializing': cannot convert from 'uchar *' to 'float *'
Where does that come from?

data is always a uchar*.
You should use
oOutput.ptr<Tout>();
to get the Tout pointer to the i-th row (by default is the 0-th row, i.e. the beginning of the image)

Related

C++; How to write to an Vect3b pointer of opencv in a function

I have a small problem. I am having a function using a function with 2 Mat pointer objects given, but im having trouble writing to outImg channels to update the img.
void convolve5(Mat *inputImg, Mat *outImg, int *kernel5) {
int channelvaluepixel = inputImg->at<Vect3b>(x,y)[channel];
// loads of changes happening here
outImg->at<Vect3b>(x,y)[0] = channelvaluepixel;
}
Since outImg is a pointer i cannot point to its adress to change it, so i tried this to change the value within the pointer:
*outImg->at<Vect3b>(x,y)[0] = channelvaluepixel;
But this would not work either since the compiler gives me an:
Error: invalid type argument of unary '*' (have 'unsigned char')
Can somebody help?
You are dereferencing whole expression outImg->at<Vect3b>(x,y)[0] instead of just (*outImg).at<Vect3b>(x,y)[0] = channelvaluepixel;

cannot convert 'double' to 'double (*)[5]' for argument '1' to 'void one_set_avrg(double (*)[5])'

The question wants me to create a two-dimensional array and there will be several tasks which required me to do it in different function. Like count the average for each row, calculate the average for whole array, sort the array in ascending order, and output the largest value that I have keyed in.
The code works if I don't separate it into different functions, but the questions asked me to separate it into different function and I ended up with these errors:
cannot convert 'double' to 'double ()[5]' for argument '1' to 'void
one_set_avrg(double ()[5])'
cannot convert 'double' to 'double ()[5]' for argument '1' to 'void
avrg_allset(double ()[5])'
cannot convert 'double' to 'double ()[5]' for argument '1' to 'double
largest_value(double ()[5])'
I would appreciate it very much if you could tell me what's wrong with my code and how could I solve it.
Here is the error part:
one_set_avrg(A[3][5]);
avrg_allset(A[3][5]);
cout <<endl;
cout <<"Largest number is: \n"<<largest_value(A[3][5])<<endl;
return 0;
}
You don't have to mention the dimensions of the array while calling a function.
So, your code would be:
one_set_avrg( A );
avrg_allset( A );
cout <<"Largest number is: \n"<< largest_value( A ) <<endl;
Here's your live code: http://ideone.com/tGdhDa

error C2440: '=' : cannot convert from 'int *' to 'int **'

#ifndef _grid_h
#define _grid_h
#include<string>
using namespace std;
template<typename T>
class grid{
T** main;
public:
grid<T>(){}
grid<T>(int col, int row){
main = new T[col]; //<-this line gives me error C2440:
//'=' : cannot convert from 'int *' to 'int **'
for(int i =0;i<col;i++)
main[i]=new T[row];
}
};
#endif
I want to create my own version of the Grid class. Basically I want to save the information in a 2 dimensional array of T. I think this is the most efficient way to do it. Now How can I get around this error?
It would need to be
main = new T*[col];
Because main is an array of pointers to T. But there are better ways to create a two-dimensional array, for example
std::vector<std::vector<T>> main(col, std::vector<T>(row));
Allocate an array of correct type: use main = new T*[col]; instead of main = new T[col];.
The answer is in your last code line:
main[i]=new T[row];
For that to work, main[i] needs to be a pointer. But you tried to create main as a new T[col] - an array of Ts. It needs to be an array of pointers-to-T.
main = new T*[col]; // Create an array of pointers

Proper casting of the object in C++, value field is not changing

I have a class:
class Para{
public:
int wrt, liczbaWystapien;
Para(){}
Para(int wrt, int liczbaWystapien){
this->wrt = wrt;
this->liczbaWystapien = liczbaWystapien;
}
Then there is other template class, and I do not know how to cast object to Para, becuase first way does not affect field value at all.
else if (is_same<T, Para>::value){
//dynamic_cast<Node<Para>*>(node)->key.wrt++;//this way no error occured but value of field **wrt** stays the same
node->key.wrt++;//error below
Error 4 error C2039: 'wrt' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
EDIT:
Node* paraNode = static_cast*>(node);
Para para = paraNode->key;
para.wrt = para.wrt + 1;
That gives
Error
4 error C2440: 'static_cast' : cannot convert from 'Node *' to 'Node *'
Something seems weird, because key's type supposed to be Para. What happens if you spell everything out?
Node<Para>* paraNode = dynamic_cast<Node<Para>*>(node);
Para para = paraNode->key;
key.wrt = key.wrt + 1;
Other suggestions: if fore some reason you are sure about the template type, you can use static_cast<> (or reinterpret_cast<>), it's faster than dynamic_cast<>, which really discovers and check the type hierarchy tree.
If you do
Para para = paraNode->key;
you get a copy of the key. Then you increment wrt. If you do a printf following that line, you will probably get the expected value. However if you call this from inside a function then the original paraNode will not be modified.
You need to store key as Para* key then access key.wrt with key->wrt++ then after the function you will get the expected value. I suggest you read on how stack and heap variables work as well as copy constructor in c++

C++ help with error : cannot convert parameter 1 from 'float' to 'float [][2]

and I was doing a program, that isn't so important. Apparently, I cant send parameters to a float function.
the code look something like this
float myfunction(float array[1][2])
{
// ...
return 0;
}
int main()
{
float array[1][2];
int foo = 0;
// assigning values to the array
foo = myfunction(array[1][2]);
return 0;
}
when I try to compile, I get the error "cannot convert parameter 1 from 'float' to 'float [][2]"
What is wrong? And how can I solve it?
Just pass array, without the indexes:
foo = myfunction(array);
A couple of things.
First, a function prototyped float myfunction(float array[1][2]) is confusing (you), since what it actually mean is: float myfunction(float array[][2]) or float myfunction(float (*array)[2]). The function accepts a pointer to (one or more) array(s) of two floats.
Second, the error you get is because the function accepts a pointer to an array, while you're tryinmg to pass it single float - element [1][2] of the two-dimensional array float array[1][2]. Perhaps you meant to pass the entire array to the function?
You've defined the variable: float array[1][2];
Then, you call the function in this way: foo = myfunction(array);
As a parameter, only you have to set the variable name. You shouldn't do this: foo = myfunction(array[1][2]);
When you make a function, do it in this way: type myfunction(float Array[][w]) , being type the type of function (void, float...) and "w" a constant integer.
You are passing a certain cell and not the array