2

我正在做图像处理,我想对之前抖动的图像进行像素化,但它应该仍然看起来像原始图像。在这里,我向您展示了一些我现在正在做什么以及我希望它看起来像什么的例子:

迈克尔杰克逊 - 正常

那就是我要修改的图像。

迈克尔杰克逊 - 抖动

那是被“floyd steinberg 抖动”抖动后的图像。

迈克尔杰克逊-我希望它看起来像什么

这就是他在被像素化后应该如何看待最后的样子。

迈克尔杰克逊-我的形象是什么样的

这就是我的图像在像素化后的样子。我真的不知道该怎么做,所以它看起来像上面的图像。

我搜索了整个互联网,并尝试了所有像素化算法。那就是我目前使用的课程:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

public final class ImageUtil {

public static BufferedImage pixelate(BufferedImage imageToPixelate, int pixelSize) {
    BufferedImage pixelateImage = new BufferedImage(
        imageToPixelate.getWidth(),
        imageToPixelate.getHeight(),
        imageToPixelate.getType());

    for (int y = 0; y < imageToPixelate.getHeight(); y += pixelSize) {
        for (int x = 0; x < imageToPixelate.getWidth(); x += pixelSize) {
            BufferedImage croppedImage = getCroppedImage(imageToPixelate, x, y, pixelSize, pixelSize);
            Color dominantColor = getDominantColor(croppedImage);
            for (int yd = y; (yd < y + pixelSize) && (yd < pixelateImage.getHeight()); yd++) {
                for (int xd = x; (xd < x + pixelSize) && (xd < pixelateImage.getWidth()); xd++) {
                    pixelateImage.setRGB(xd, yd, dominantColor.getRGB());
                }
            }
        }
    }

    return pixelateImage;
}

public static BufferedImage getCroppedImage(BufferedImage image, int startx, int starty, int width, int height) {
    if (startx < 0) startx = 0;
    if (starty < 0) starty = 0;
    if (startx > image.getWidth()) startx = image.getWidth();
    if (starty > image.getHeight()) starty = image.getHeight();
    if (startx + width > image.getWidth()) width = image.getWidth() - startx;
    if (starty + height > image.getHeight()) height = image.getHeight() - starty;
    return image.getSubimage(startx, starty, width, height);
}

public static Color getDominantColor(BufferedImage image) {
    Map<Integer, Integer> colorCounter = new HashMap<>(100);
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            int currentRGB = image.getRGB(x, y);
            int count = colorCounter.getOrDefault(currentRGB, 0);
            colorCounter.put(currentRGB, count + 1);
        }
    }
    return getDominantColor(colorCounter);
}

private static Color getDominantColor(Map<Integer, Integer> colorCounter) {
    int dominantRGB = colorCounter.entrySet().stream()
        .max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1)
        .get()
        .getKey();
    return new Color(dominantRGB);
}
}

我就是这样开始的:

ImageUtil.pixelate(selectedImage, 3);

感谢您的帮助,如果有不清楚的地方或者我需要在我的问题中添加一些内容,请告诉我。

4

1 回答 1

1

以下是您可以应用的算法阶段:

  • 对原始图像进行下采样 - 将分辨率降低到大约 200x200。
  • 使用 16 色抖动应用调色板。
    有关详细信息,请查看rgb2ind MATLAB 文档。
    在下面的示例中,我使用您帖子中的参考图像找到了调色板。
  • 使用最近邻法将每个轴的大小调整为 x3以创建像素化效果。

MATLAB实现:

% Palette of 16 colors
P = [  0     0     0
     160    80    44
     210   122   170
      14    16    83
     254   254   254
     255   113     0
      99    48    13
       1    86   158
       4    93    13
     192   192   192
      75    75    75
     233   165     0
     167    85   115
      85    15   105
       1   178   255
     116    11     7];

%Read original image
I = imread('I.jpg');

%Resize original image to resolution 200x200
J = imresize(I, [200, 200]);

% Convert shrunk RGB image to indexed image using palette P.
[IND, map] = rgb2ind(J, double(P)/255, 'dither'); %figure;imagesc(IND);colormap(map);axis image

% Convert from indexed image to RGB
RGB = ind2rgb(IND, map); %figure;imshow(RGB);

% Resize RGB by factor of x3 in each axis, use nearest-neighbor interpolation.
K = imresize(RGB, 3, 'nearest');

figure;imshow(K);

结果:
在此处输入图像描述

与您的参考有一些差异(可能是由于不同的颜色减少算法)。

于 2020-02-02T18:17:39.670 回答