Ask Your Question
0

How do I properly add two Matrices?

asked 2018-04-28 18:25:55 -0600

TeamAR gravatar image

updated 2018-04-28 23:24:47 -0600

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 


 }
edit retag flag offensive close merge delete

Comments

Opencv version? platform?

may be type are not equal :

try :

   cout <<LoadedImage.type()<<"\t"<<;LoadedImage2.type()
   cout <<LoadedImage.rows()<<"\t"<<;LoadedImage2.rows()
   cout <<LoadedImage.cols()<<"\t"<<;LoadedImage2.cols()
    Mat add= LoadedImage +LoadedImage;
    add= LoadedImage2 +LoadedImage2;
LBerger gravatar imageLBerger ( 2018-04-29 03:34:35 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-04-29 15:25:41 -0600

bennog gravatar image

The result can not be a CV_8U3C because it can not hold a value above 255, try converting to CV_32F3C before adding up.

edit flag offensive delete link more

Comments

Don't forget about CV_16U3C!

sjhalayka gravatar imagesjhalayka ( 2018-04-29 18:58:37 -0600 )edit

Just tested it but it don't get promoted to CV_16U3C The output is simply clipped to 255 so if you add 200 + 200 the result will be 255 See below example

cv::Mat img1 = cv::Mat::zeros(300, 255, CV_8UC3);
cv::Mat img2 = cv::Mat::zeros(300, 255, CV_8UC3);
cv::line(img1, cv::Point(0, 0), cv::Point(50, 70), cv::Scalar(200,200,200), 10);
cv::line(img2, cv::Point(0, 0), cv::Point(50, 50), cv::Scalar(200,200,0), 10);
cv::Mat res = img1 + img2;
bennog gravatar imagebennog ( 2018-04-30 00:39:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-28 18:25:55 -0600

Seen: 7,395 times

Last updated: Apr 29 '18