1

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;
}
5
  • 2
    You should clarify what look better means, or this question is just opinion based Commented Oct 12, 2015 at 3: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. Commented Oct 12, 2015 at 9:00
  • this image: rd1.ig.com.br/wp-content/uploads/2015/05/… 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... Commented Oct 12, 2015 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 Commented May 17, 2016 at 16:28
  • you can just use the algorithm described above @ManuGupta Commented Mar 14, 2017 at 3:43

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

you might want to look at this: stackoverflow.com/questions/55508615/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.