0

everyone.

I'd like to know if there is any improvement to a pixelizing algorithm I'm working on.

The algorithm it's written in C++ using OpenCV library and works like this:

  • Increase the contrast of the Mat a little bit;
  • Resize the Mat to D% of its size, using nearest-neighbor interpolation;
  • Resize the Mat back to its original size, also using nni;

[D = density, a parameter of the function]

Is there any way to make the result look better?

Mat pixelize(Mat src, int density){
    Size s(src.cols, src.rows);

    src.convertTo(src, -1, 1.1, 0);

    resize(src, src, percent(s, density), 1, 1, INTER_NEAREST);
    resize(src, src, s, 1, 1, INTER_NEAREST);

    resize(src, src, Size(640, 480));
    return src;
}
  • 2
    You should clarify what _look better_ means, or this question is just opinion based – Miki Oct 12 '15 at 03:11
  • Yes. Best to provide a source image and the image after you applied your algorithm and then point out what you don't like about it. – HenningJ Oct 12 '15 at 09:00
  • this image: http://rd1.ig.com.br/wp-content/uploads/2015/05/zd8ld4yaqczsytdpkiyop5gkafex6mfq4d6qr5zw7n7amcyazxjfnfdgo5rzbq5d.jpg the result wasn't that good for any parameter I tried, and what I wanted was a pixel art image but the result doesn't look like one... –  Oct 12 '15 at 20:13
  • I was working on openCV face detection and after i detect the face i want to pixelate. So do you have any idea how i can achieve this @DoCoO – Manu Gupta May 17 '16 at 16:28
  • you can just use the algorithm described above @ManuGupta –  Mar 14 '17 at 03:43

1 Answers1

2

2 years and 9 months after, but i think its worth sharing, what I use to pixelate images is this:

int size = 7;//only odd!
int max_step = (size - 1) / 2;
Mat m = imread("test.jpg");
for (int i = max_step; i < m.rows- max_step; i+=size) {
    for (int j = max_step; j < m.cols- max_step; j+=size) {
        Vec3b colour = m.at<Vec3b>(Point(j, i));
        for (int k = -max_step; k <= max_step; k++) {
            for (int l = -max_step; l <= max_step; l++) {
                m.at<Vec3b>(Point(j - k, i - l)) = colour;
            }
        }
    }
}
imshow("pixeled", m);
waitKey(0);

With this you go every odd number of pixels (size variable) and sourround its neigbours with the same colour as the chosen one.

Note that this doesn't handle the edge, but you get the idea of this algorithm

Here I left some images with parameters 1(none), 7(medium) and 21(very). To improve you may chose the "size" var to vary in function of an abstract parameter and the size of the image... Well, hope it helps, even almost 3 years late!

petacreepers23
  • 164
  • 1
  • 9
  • you might want to look at this: https://stackoverflow.com/questions/55508615/how-to-pixelate-image-using-opencv-in-python – Greg0ry May 28 '21 at 10:58