简体   繁体   中英

Calculate RGB Image Matrix with several custom matrices in C++ OpenCV

Helllo, I'm newbie in C++ OpenCV programming. I have one colour image, and i want that image multiplied with several matrices (masking). This is the outline of code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
char* hai;
int main()
{

hai = "c:/Dalton/grayscale.jpg";
Mat im = imread(hai);
Mat Result;
const int nChannels = im.channels(); //get channel sum from the image (it should be 3 channel,because RGB)
Result.create(im.size(), im.type()); //create new mat (for the result) which have same size and type with first mat
double rgb2lms[3][3] = { 17.8824, 43.5161, 4.11935,
    3.45565, 27.1554, 3.86714,
    0.0299566, 0.184309, 1.46709 };
double lms2lmsp[3][3] = { 0, 2.02344, -2.52581,
                        0, 1, 0,
                        0, 0, 1 };
if (im.empty())
{
    cout << "Cannot load image!" << endl;
    return -1;
}
//access pixel
for (int i = 0; i < im.rows; i++)
{
    for (int j = 0; j < im.cols; j++)
    {
        for (int k = 0; k < nChannels; k++)
        {

            //main program
            //calculating matrix    
            //i want calculate im(RGB matrix) multiply with rgb2lms
            //after that, the result is multiplied again with lms2lmsp matrix
        }
    }
}   
imshow("Image", im);
imshow("Image Result", Result);
waitKey(0);

}

How i can multiply this RGB mat matrix with another matrix? Should i access every pixel? I want to multiplied original image with rgb2lms matrix and then the result multiplied again with lms2lmsp matrix. Anyone can help me? Thanks

If you are asking how to multiply matrices, OpenCV API does it:

Mat::mul Performs an element-wise multiplication or division of two matrices.

EDIT:

This tutorial explains how to apply a mask matrix on an image. I think that's what you are looking for..

http://docs.opencv.org/doc/tutorials/core/mat-mask-operations/mat-mask-operations.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM