Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I properly add two Matrices?

I am having a problem making addition of two images, both are 255X255, and both are of type 8UC3 , but still I am getting this error:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file

This is my code:

std::string type2str(int type) {
  std::string r;

  uchar depth = type & CV_MAT_DEPTH_MASK;
  uchar chans = 1 + (type >> CV_CN_SHIFT);

  switch ( depth ) {
    case CV_8U:  r = "8U"; break;
    case CV_8S:  r = "8S"; break;
    case CV_16U: r = "16U"; break;
    case CV_16S: r = "16S"; break;
    case CV_32S: r = "32S"; break;
    case CV_32F: r = "32F"; break;
    case CV_64F: r = "64F"; break;
    default:     r = "User"; break;
  }

  r += "C";
  r += (chans+'0');

  return r;
}


int main(int argc, const char** argv)
{
 //
 //  Load the image from file
 //
 Mat LoadedImage,LoadedImage2;

 LoadedImage = imread(argv[1], IMREAD_COLOR);
  LoadedImage2 = imread(argv[2], IMREAD_COLOR);
Mat add= LoadedImage +LoadedImage2; // this is the runtime error
std::cout <<LoadedImage.size << "and 2 = "<< LoadedImage2.size; //outputs 255 x255 and 2= 255 x 255


std::cout << type2str( LoadedImage.type())<<"and 2= "<<type2str( LoadedImage2.type()); //outputs 8UC3  and 2= 8UC3 


 }

How do I properly add two Matrices?

I am having a problem making addition of two images, both are 255X255, and both are of type 8UC3 , but still I am getting this error:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file
file.

Update: One strange thing is that LoadedImage.cols == LoadedImage2.cols returns 0 (false) but they both return the same number (255), how does C++ not returning true for 255==255 is mindblowing for me at this point.

This is my code:

std::string type2str(int type) {
  std::string r;

  uchar depth = type & CV_MAT_DEPTH_MASK;
  uchar chans = 1 + (type >> CV_CN_SHIFT);

  switch ( depth ) {
    case CV_8U:  r = "8U"; break;
    case CV_8S:  r = "8S"; break;
    case CV_16U: r = "16U"; break;
    case CV_16S: r = "16S"; break;
    case CV_32S: r = "32S"; break;
    case CV_32F: r = "32F"; break;
    case CV_64F: r = "64F"; break;
    default:     r = "User"; break;
  }

  r += "C";
  r += (chans+'0');

  return r;
}


int main(int argc, const char** argv)
{
 //
 //  Load the image from file
 //
 Mat LoadedImage,LoadedImage2;

 LoadedImage = imread(argv[1], IMREAD_COLOR);
  LoadedImage2 = imread(argv[2], IMREAD_COLOR);
Mat add= LoadedImage +LoadedImage2; // this is the runtime error
std::cout <<LoadedImage.size << "and 2 = "<< LoadedImage2.size; //outputs 255 x255 and 2= 255 x 255


std::cout << type2str( LoadedImage.type())<<"and 2= "<<type2str( LoadedImage2.type()); //outputs 8UC3  and 2= 8UC3 


 }

How do I properly add two Matrices?

I am having a problem making addition of two images, both are 255X255, and both are of type 8UC3 , but still I am getting this error:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file.

Update: One strange thing is that LoadedImage.cols == LoadedImage2.cols returns 0 (false) but they both return the same number (255), how does C++ not returning true for 255==255 is mindblowing for me at this point.

This is my code:

std::string type2str(int type) {
  std::string r;

  uchar depth = type & CV_MAT_DEPTH_MASK;
  uchar chans = 1 + (type >> CV_CN_SHIFT);

  switch ( depth ) {
    case CV_8U:  r = "8U"; break;
    case CV_8S:  r = "8S"; break;
    case CV_16U: r = "16U"; break;
    case CV_16S: r = "16S"; break;
    case CV_32S: r = "32S"; break;
    case CV_32F: r = "32F"; break;
    case CV_64F: r = "64F"; break;
    default:     r = "User"; break;
  }

  r += "C";
  r += (chans+'0');

  return r;
}


int main(int argc, const char** argv)
{
 //
 //  Load the image from file
 //
 Mat LoadedImage,LoadedImage2;

 LoadedImage = imread(argv[1], IMREAD_COLOR);
  LoadedImage2 = imread(argv[2], IMREAD_COLOR);
Mat add= LoadedImage +LoadedImage2; // this is the runtime error
std::cout <<LoadedImage.size << "and 2 = "<< LoadedImage2.size; //outputs 255 x255 and 2= 255 x 255


std::cout << type2str( LoadedImage.type())<<"and 2= "<<type2str( LoadedImage2.type()); //outputs 8UC3  and 2= 8UC3 


 }