Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I think it impossible using + operator. You can:

  • Use convert to float
 
    const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
    const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
    Mat E(A.size(), A.type());
    B.convertTo(E, CV_32FC1);
    Mat C( A.size(), A.type() );
    C = E+A;
  • Per-element sum
    const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
    const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
    Mat C(A.size(), A.type());
    for (int i = 0; i < A.rows; i++)
    {
    for (int j = 0; j < A.cols; j++)
    {
        C.at<float>(i,j) = A.at<float>(i,j)+B.at<uchar>(i,j);
    }
    }
    
    or
    
    const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
    const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
    Mat C(A.size(), A.type());
    for (int i = 0; i < A.rows; i++)
    {
    const float* A_ptr = reinterpret_cast<const float*="">(A.ptr(i));
    const uchar* B_ptr = reinterpret_cast<const uchar*="">(B.ptr(i));
    float* C_ptr = reinterpret_cast<float*>(C.ptr(i));
    for (int j = 0; j < A.cols; j++)
    {
        C_ptr[j] = A_ptr[j]+B_ptr[j];
    }
    }
    

I think it impossible using + operator. You can:

  • Use convert to float
 
    const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
    const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
    Mat E(A.size(), A.type());
    B.convertTo(E, CV_32FC1);
    Mat C( A.size(), A.type() );
    C = E+A;
  • Per-element sum
    const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
    const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
    Mat C(A.size(), A.type());
    for (int i = 0; i < A.rows; i++)
    {
    for (int j = 0; j < A.cols; j++)
    {
        C.at<float>(i,j) = A.at<float>(i,j)+B.at<uchar>(i,j);
    }
    }
    
    or
    
    const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
    const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
    Mat C(A.size(), A.type());
    for (int i = 0; i < A.rows; i++)
    {
    const float* A_ptr Aptr = reinterpret_cast<const float*="">(A.ptr(i));
    const uchar* B_ptr Bptr = reinterpret_cast<const uchar*="">(B.ptr(i));
    float* C_ptr Cptr = reinterpret_cast<float*>(C.ptr(i));
    for (int j = 0; j < A.cols; j++)
    {
        C_ptr[j] = A_ptr[j]+B_ptr[j];
    Cptr[j] = Aptr[j]+Bptr[j];
    }
    }
    

I think it impossible using + operator. You can:

  • Use convert to float
  const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
 const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
 Mat E(A.size(), A.type());
 B.convertTo(E, CV_32FC1);
 Mat C( A.size(), A.type() );
 C = E+A;
  • Per-element sum sum
 const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
Mat C(A.size(), A.type());
for (int i = 0; i < A.rows; i++)
{
 for (int j = 0; j < A.cols; j++)
{
    {
        C.at<float>(i,j) = A.at<float>(i,j)+B.at<uchar>(i,j);
 }
}
or

or


const Mat A( 10, 20, CV_32FC1, Scalar::all(CV_PI) );
const Mat B( A.size(), CV_8UC1, Scalar::all(10) );
Mat C(A.size(), A.type());
for (int i = 0; i < A.rows; i++)
{
 const float* Aptr = reinterpret_cast<const float*="">(A.ptr(i));
reinterpret_cast< const float*>(A.ptr(i));
    const uchar* Bptr = reinterpret_cast<const uchar*="">(B.ptr(i));
reinterpret_cast< const uchar*>(B.ptr(i));
    float* Cptr = reinterpret_cast<float*>(C.ptr(i));
reinterpret_cast< float*>(C.ptr(i));
    for (int j = 0; j < A.cols; j++)
{
    {
        Cptr[j] = Aptr[j]+Bptr[j];
 }
}