How to know the intensity of pixels in a given row or column of a cv::Mat?? [opencv][c++]

Asked

Viewed 60 times

0

I have a project where there is a need to analyze the intensity of the pixels of a certain column or row, that is, my function will be Intensity(cv::Mat m, int ref, Std::string "lico") with a vector-type sadide with the data of the desired column. I’m new to opencv and c++. I’ve been using the following code:

.hpp
     class fooTools
    {
     public: 
         static std::vector<int> intensity(cv::Mat src, int ref, std::string lico);
    };
.cpp
    int footools::intensity(cv::Mat src, int ref, std::string lico)
    {
    //condicional para linha ou coluna
    if (lico == "linha") {
       for (int col = 0; col < (src.cols); col++)
            {
                std::cout << src.at<uchar>(ref,col);
            }
    }
    else if (lico == "coluna") {
        for (int row = 0; row < (src.rows); row++)
            {
                std::cout << src.at<uchar>(row, ref);
            }
        }
        return intensity;
    }
main

    int main() {      
    //load img
    std::string path = "C:/foo/";
    std::string name = "foo.bmp";
    cv::Mat src = cv::imread(path + name, cv::IMREAD_GRAYSCALE);

    fooTools::intensity(src, 44, "linha");
    }

I haven’t had a way out, and I’ve tried a lot, someone can help me have an idea for this situation.

  • friend, format your code that you posted, that I even tried to format, but it’s hard to understand

  • I believe it has improved, the intention is to pass the idea, I have not been able to implement this idea, still, obg by the attention!

  • What would be the ref variable?

  • ref is the row or column that I want to access the pixels, for example intesity(cv:Mat m, 185, "line"); therefore, I would access all pixels of line 185 of the image m. In this case, I would switch to Intensity(cv:Mat m, 185,"column"); I would access all pixels of column 185.

1 answer

0

Okay, I’ll try to answer, but I have no way to test it, so:

First in class:

class fooTools
{
    public: 
        static std::vector<uchar> intensity(cv::Mat src, int ref, std::string lico);
};

Due to the nature that the openCV template is.

Now in function:

<uchar> footools::intensity(cv::Mat src, int ref, std::string lico)
{

    std::vector <uchar> intensity;    

    //condicional para linha ou coluna
    if (lico == "linha") {
       for (int col = 0; col < (src.cols); col++)
           {
               //std::cout << src.at<uchar>(ref,col);
               intensity.push_back(src.at<uchar>(ref,col));
           }
    }
    else if (lico == "coluna") {
        for (int row = 0; row < (src.rows); row++)
           {
               //std::cout << src.at<uchar>(row, ref);
               intensity.push_back(src.at<uchar>(ref,row));
           }
    }
    return intensity;
}

And now in the main function():

int main() 
{      
    //load img
    std::string path = "C:/foo/";
    std::string name = "foo.bmp";
    cv::Mat src = cv::imread(path + name, cv::IMREAD_GRAYSCALE);

    std::vector <uchar> intensity;

    intensity = fooTools::intensity(src, 44, "linha");
 }

Observation 1: the return of intensities is in the openCV, so if you need to do some math on it you will have to convert to uint, or similar to openCV.

Observation 2: My domain in templates is low and rusty, so it may occur that the template may not be used in vector, as I did.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.