Ask Your Question
6

how to sum a 3 channel matrix to a one channel matrix?

asked 2012-10-13 09:32:27 -0600

501 Not Implemented gravatar image

updated 2020-11-30 03:01:38 -0600

Hi, i want to sum a 3 channel (lab) matrix to a one channel matrix. Is there a simple way to do this or must i split the image into 3 different CvMat-Objects?

It would to be nice, if there is a parameter for cvCvtColor that add all 3 channels with the same weight.

greetings

edit retag flag offensive close merge delete

Comments

4 answers

Sort by » oldest newest most voted
5

answered 2015-06-16 15:15:06 -0600

Oliver Keller gravatar image

updated 2016-12-04 07:46:21 -0600

Adi gravatar image

Hi,

here's a code sample without cv::reshape() or cv::reduce() :

cv::transform(m_in, m_out, cv::Matx13f(1,1,1))

edit flag offensive delete link more
2

answered 2012-10-13 10:20:40 -0600

Rui Marques gravatar image

One way to do it would be to split the channels with split and to add them with addWeighted.

edit flag offensive delete link more

Comments

yeah in time i splitt the image and use the cvAdd-feature. i thought there is a faster method

501 Not Implemented gravatar image501 Not Implemented ( 2012-10-13 10:43:53 -0600 )edit
8

answered 2012-10-14 01:10:38 -0600

sammy gravatar image

updated 2012-10-14 01:11:53 -0600

You can reshape() it to a 3 cols* (initial_rows*initial_cols) rows, then use cv::reduce() by summing up the cols, then reshape it back to initial_rows-by-initial_cols. You won't need the split.

Note that reshape() does not reallocate or move elements, just inteprets their order in a different way, so it it very fast.

edit flag offensive delete link more

Comments

Hmm, I like this one.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-10-14 02:17:50 -0600 )edit

great solution! thanks :)

501 Not Implemented gravatar image501 Not Implemented ( 2012-10-14 09:17:08 -0600 )edit

hi, Can anyone provide me small code sample to sum a 3 channel matrix into one channel matrix using reshape and reduce functions using the above idea which explained by sammy ....

Sharath gravatar imageSharath ( 2015-06-05 08:56:18 -0600 )edit

This took me way too long to figure out:

cv::Mat reduceChannels(cv::InputArray _src, int _rtype)
{
    cv::Mat src = _src.getMat();

    cv::Mat tmp;
    cv::reduce(src.reshape(1, src.rows*src.cols), tmp, 1, _rtype);
    return tmp.reshape(1, src.rows);
}

Note that cv::reduce() does not work on all data types! CV_32S (32bit int) for example is not supported, as there is no data type that would be able to hold the result.

iliis gravatar imageiliis ( 2020-06-25 12:11:03 -0600 )edit
-1

answered 2012-10-14 03:25:20 -0600

rotating_image gravatar image

use cvMerge after converting the CvMat to CvArr

edit flag offensive delete link more

Comments

cvMerge is not performing sum operation. It is only rearranges data from multiple single-channel images to one multi-channel.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-10-14 03:59:21 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-10-13 09:32:27 -0600

Seen: 13,368 times

Last updated: Dec 04 '16