图像马赛克原理及实现
生活随笔
收集整理的這篇文章主要介紹了
图像马赛克原理及实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? ??圖像打碼其實(shí)也是圖像卷積操作中,空間域?yàn)V波的一種方式,用一定大小的濾波器對(duì)馬賽克范圍內(nèi)像素進(jìn)行操作。實(shí)現(xiàn)過(guò)程:將需要打馬范圍按照濾波器大小劃分為多個(gè)區(qū)塊,取濾波器范圍內(nèi)像素,求取均值,再將均值賦值給范圍內(nèi)每一個(gè)像素,濾波器再滑到下一個(gè)區(qū)塊。從原理上看,圖像馬賽克特效非常簡(jiǎn)單,下面就來(lái)看一下具體實(shí)現(xiàn)過(guò)程。
? ? ? ??對(duì)整張圖像打碼。對(duì)整張圖片打碼也叫做像素格特效,我們只需要考慮濾波器大小即可。
? ? ? ? 代碼:
/*** 對(duì)整個(gè)圖片打碼* @param image* @param length 馬賽克大小*/ public void imageMosaic(BufferedImage image,int length) {int width = image.getWidth();int height = image.getHeight();int size = length * length;int x = width / length;int y = height / length;for(int i = 0; i < x; i++) {for(int j = 0; j < y; j++) {int R=0,G=0,B=0;for(int m = 0; m < length; m++) {for(int n = 0; n < length; n++) {int xCoordinate = i * length + m;int yCoordinate = j * length + n;int rgb = image.getRGB(xCoordinate, yCoordinate);R += (rgb >> 16) & 0xff;G += (rgb >> 8) & 0xff;B += rgb & 0xff;}}R = R / size;G = G / size;B = B / size;int RGB = (255 & 0xff) << 24 | (clamp(R) & 0xff) << 16 | (clamp(G) & 0xff) << 8 | (clamp(B) & 0xff);for(int m = 0; m < length; m++) {for(int n = 0; n < length; n++) {int xCoordinate = i * length + m;int yCoordinate = j * length + n;image.setRGB(xCoordinate, yCoordinate, RGB);}}}}}局部打碼。局部打碼需要先定義好打碼起始位置,打碼范圍,馬賽克大小。
代碼:
/*** 局部打碼* @param image* @param xCoordinate 橫坐標(biāo)起始位置* @param yCoordinate 縱坐標(biāo)起始位置* @param xLength x方向長(zhǎng)度* @param yLength y方向長(zhǎng)度* @param length 馬賽克大小*/public void mosaic(BufferedImage image,int xCoordinate,int yCoordinate,int xLength,int yLength,int length) {if ((xCoordinate + xLength) > image.getWidth() || (yCoordinate + yLength) > image.getHeight()) {System.out.println("馬賽克范圍大于圖像范圍!");return;}int size = length * length;int x = xLength / length;int y = yLength / length;for(int m = 0; m < x; m++) {for(int n = 0; n < y; n++) {int R=0,G=0,B=0;for(int i = 0; i < length; i++) {for(int j = 0; j < length; j++) {int x1 = xCoordinate + m * length + i;int y1 = yCoordinate + n * length + j;int rgb = image.getRGB(x1, y1);R += (rgb >> 16) & 0xff;G += (rgb >> 8) & 0xff;B += rgb & 0xff;}}R = R / size;G = G / size;B = B / size;int RGB = (255 & 0xff) << 24 | (clamp(R) & 0xff) << 16 | (clamp(G) & 0xff) << 8 | (clamp(B) & 0xff);for(int i = 0; i < length; i++) {for(int j = 0; j < length; j++) {int x1 = xCoordinate + m * length + i;int y1 = yCoordinate + n * length + j;image.setRGB(x1, y1, RGB);}}}}}? ? ? ??完整的馬賽克實(shí)現(xiàn)代碼:
public class MosaicFilter {/*** 對(duì)整個(gè)圖片打碼* @param image* @param length 馬賽克大小*/public void imageMosaic(BufferedImage image,int length) {int width = image.getWidth();int height = image.getHeight();int size = length * length;int x = width / length;int y = height / length;for(int i = 0; i < x; i++) {for(int j = 0; j < y; j++) {int R=0,G=0,B=0;for(int m = 0; m < length; m++) {for(int n = 0; n < length; n++) {int xCoordinate = i * length + m;int yCoordinate = j * length + n;int rgb = image.getRGB(xCoordinate, yCoordinate);R += (rgb >> 16) & 0xff;G += (rgb >> 8) & 0xff;B += rgb & 0xff;}}R = R / size;G = G / size;B = B / size;int RGB = (255 & 0xff) << 24 | (clamp(R) & 0xff) << 16 | (clamp(G) & 0xff) << 8 | (clamp(B) & 0xff);for(int m = 0; m < length; m++) {for(int n = 0; n < length; n++) {int xCoordinate = i * length + m;int yCoordinate = j * length + n;image.setRGB(xCoordinate, yCoordinate, RGB);}}}}}/*** 局部打碼* @param image* @param xCoordinate 橫坐標(biāo)起始位置* @param yCoordinate 縱坐標(biāo)起始位置* @param xLength x方向長(zhǎng)度* @param yLength y方向長(zhǎng)度* @param length 馬賽克大小*/public void mosaic(BufferedImage image,int xCoordinate,int yCoordinate,int xLength,int yLength,int length) {if ((xCoordinate + xLength) > image.getWidth() || (yCoordinate + yLength) > image.getHeight()) {System.out.println("馬賽克范圍大于圖像范圍!");return;}int size = length * length;int x = xLength / length;int y = yLength / length;for(int m = 0; m < x; m++) {for(int n = 0; n < y; n++) {int R=0,G=0,B=0;for(int i = 0; i < length; i++) {for(int j = 0; j < length; j++) {int x1 = xCoordinate + m * length + i;int y1 = yCoordinate + n * length + j;int rgb = image.getRGB(x1, y1);R += (rgb >> 16) & 0xff;G += (rgb >> 8) & 0xff;B += rgb & 0xff;}}R = R / size;G = G / size;B = B / size;int RGB = (255 & 0xff) << 24 | (clamp(R) & 0xff) << 16 | (clamp(G) & 0xff) << 8 | (clamp(B) & 0xff);for(int i = 0; i < length; i++) {for(int j = 0; j < length; j++) {int x1 = xCoordinate + m * length + i;int y1 = yCoordinate + n * length + j;image.setRGB(x1, y1, RGB);}}}}}// 判斷r,g,b值,大于256返回256,小于0則返回0,0到256之間則直接返回原始值private int clamp(int rgb) {if (rgb > 255)return 255;if (rgb < 0)return 0;return rgb;}public static void main(String[] args) throws Exception{File in = new File("C:/Users/admin/Desktop/1.jpg");File out = new File("C:/Users/admin/Desktop/3.jpg");BufferedImage image = ImageIO.read(in);new MosaicFilter().imageMosaic(image, 5);//new MosaicFilter().mosaic(image, 100, 70, 50, 50,10);ImageIO.write(image, "jpg", out);} }? ? ? ? ? 原圖:
? ? ? ? 全局打碼:
? ? ? ? 局部打碼:
? ? ? ?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的图像马赛克原理及实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 单变量离散傅里叶变换DFT原理及实现
- 下一篇: 高动态范围图像HDR