Find the most frequently occurring element in a row [closed]

asked 2018-01-23 09:03:01 -0500

teenvan95 gravatar image

I have a vector of 5 mat objects each of size 60 x 1. I need to return a single 60 x 1 matrix where each value in a row corresponds to the most frequently occurring value in the corresponding row in the 5 matrices. How should I do this?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-08 07:59:54.908164


Colour or greyscale?

Are you familiar with the C++ Standard Template Library map container?

sjhalayka gravatar imagesjhalayka ( 2018-01-23 09:12:49 -0500 )edit

Not an image per se. It is just a Mat object of labels generated from the DTree I trained. Specifically, it consists of the class labels.

teenvan95 gravatar imageteenvan95 ( 2018-01-23 09:19:36 -0500 )edit

OK, what is the Mat type? CV_8UC1?

sjhalayka gravatar imagesjhalayka ( 2018-01-23 09:25:22 -0500 )edit

yes CV_8UC1

teenvan95 gravatar imageteenvan95 ( 2018-01-23 09:27:41 -0500 )edit

include <opencv2 opencv.hpp="">

using namespace cv;
#pragma comment(lib, "opencv_world340.lib")

#include <iostream>
#include <map>
using namespace std;

int main(void)
    Mat frame = imread("strip.png");

    if (frame.empty())
        cout << "Error loading image file" << endl;
        return -1;

    map<unsigned char, size_t> pixel_map;

    for (int j = 0; j < frame.rows; j++)
        for (int i = 0; i < frame.cols; i++)
            pixel_map[<unsigned char>(j, i)]++;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
        cout << static_cast<unsigned int>(ci->first) << " -> " << ci->second << endl;

    return 0;
sjhalayka gravatar imagesjhalayka ( 2018-01-23 09:37:10 -0500 )edit

The previous code counts each unique pixel value. ci->first is the pixel value, and ci->second is the count.

sjhalayka gravatar imagesjhalayka ( 2018-01-23 09:41:09 -0500 )edit