我正在做图像处理,我想对以前抖动过的图像进行像素化处理,但它仍然应该看起来像原始图像。在这里,我向您展示一些示例,我现在正在做什么以及我希望它看起来像什么:
那是我要修改的图像。
那是蜜蜂被“弗洛伊德·施泰因贝格抖动”抖动后的图像。
这就是他在像素化之后应该如何看待结尾。
这就是像素化后我的图像的样子。我真的不知道该怎么办,就像上图一样。
我搜索了整个互联网,并尝试了所有像素化算法。这就是Iam目前使用的课程:
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);
感谢您的帮助,并告诉我是否有不清楚的地方,或者我需要在问题中添加一些内容。
答案 0 :(得分:1)
以下是您可以应用的算法阶段:
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);
与您的参考文献有一些差异(可能是由于不同的色彩还原算法)。