Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Don't forget to get the entropy of an image. For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);
imread("puppets.png");

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. It goes to show that an image is a discrete signal consisting of pixels.

For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

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

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. It goes to show that an image is a discrete signal consisting of pixels.pixels. You will note that we use the same entropy calculation as Shannon; it's called the Shannon entropy. So, if the entropy calculation for a signal and for an image are the same, then so must the energy calculations be the same as well.

For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

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

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. It goes to show that an image is a discrete signal consisting of pixels. pixels (messages). You will note that we use the same entropy calculation as Shannon; it's called the Shannon entropy.

So, if the entropy calculation for a signal and for an image are the same, then so must the energy calculations be the same as well.same, or at least proportionate to each other like: intensity = c * amplitude^2, where c is some constant. The energy of the image is the sum of the intensity of all pixels. The intensity of one pixel is 0 through 255 times some constant number of red photons, 0 through 255 times some constant number of blue photons, and 0 through 255 times some constant number of green photons, per second per unit area.

For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

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

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. It goes to show that an image is a discrete signal consisting of pixels (messages). You will note that we use the same entropy calculation as Shannon; it's called the Shannon entropy.

So, if the entropy calculation for a signal and for an image are the same, then so must the energy calculations be the same, or at least proportionate to each other like: intensity = c * amplitude^2, where c is some constant. The energy of the image is the sum of the intensity of all pixels. The intensity of one pixel is 0 through 255 times some constant number of red photons, plus 0 through 255 times some constant number of blue photons, and plus 0 through 255 times some constant number of green photons, per second per unit area.

For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

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

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}

Don't forget to get the entropy of an image. It goes to show that an image is a discrete signal consisting of pixels (messages). You will note that we use the same entropy calculation as Shannon; it's called the Shannon entropy.

So, if the entropy calculation for a signal and for an image are the same, then so must the energy calculations be the same, or at least proportionate to each other like: intensity = c * amplitude^2, where c is some constant. The energy of the image is the sum of the intensity of all pixels. The intensity of one pixel is 0 through 255 times some constant number of red photons, plus 0 through 255 times some constant number of blue photons, plus 0 through 255 times some constant number of green photons, per second per unit area. The intensity is also handily known as power per unit area.

For example, here is the C++ code to calculate the entropy of a greyscale image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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


int main(void)
{
    Mat frame = imread("puppets.png", CV_LOAD_IMAGE_GRAYSCALE);

    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[frame.at<unsigned char>(j, i)]++;

    double entropy = 0;

    for (map<unsigned char, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << entropy << endl;

    return 0;
}

Also for example, here is the C++ code to calculate the entropy of a BGR image:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world331.lib")

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

class vector3b : public Vec3b
{
public:
    inline bool operator<(const vector3b &right) const
    {
        if (right.val[0] > val[0])
            return true;
        else if (right.val[0] < val[0])
            return false;

        if (right.val[1] > val[1])
            return true;
        else if (right.val[1] < val[1])
            return false;

        if (right.val[2] > val[2])
            return true;
        else if (right.val[2] < val[2])
            return false;

        return false;
    }
};

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

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

    map<vector3b, size_t> pixel_map;

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

    double entropy = 0;

    for (map<vector3b, size_t>::const_iterator ci = pixel_map.begin(); ci != pixel_map.end(); ci++)
    {
        double probability = ci->second / static_cast<double>(frame.rows*frame.cols);
        entropy += probability * log(probability);
    }

    entropy = -entropy;

    cout << frame.rows*frame.cols << endl;
    cout << pixel_map.size() << endl;
    cout << entropy << endl;

    return 0;
}