日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图像马赛克原理及实现

發布時間:2024/1/23 编程问答 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图像马赛克原理及实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ??圖像打碼其實也是圖像卷積操作中,空間域濾波的一種方式,用一定大小的濾波器對馬賽克范圍內像素進行操作。實現過程:將需要打馬范圍按照濾波器大小劃分為多個區塊,取濾波器范圍內像素,求取均值,再將均值賦值給范圍內每一個像素,濾波器再滑到下一個區塊。從原理上看,圖像馬賽克特效非常簡單,下面就來看一下具體實現過程。

? ? ? ??對整張圖像打碼。對整張圖片打碼也叫做像素格特效,我們只需要考慮濾波器大小即可。

? ? ? ? 代碼:

/*** 對整個圖片打碼* @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 橫坐標起始位置* @param yCoordinate 縱坐標起始位置* @param xLength x方向長度* @param yLength y方向長度* @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);}}}}}

? ? ? ??完整的馬賽克實現代碼:

public class MosaicFilter {/*** 對整個圖片打碼* @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 橫坐標起始位置* @param yCoordinate 縱坐標起始位置* @param xLength x方向長度* @param yLength y方向長度* @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);} }

? ? ? ? ? 原圖:

? ? ? ? 全局打碼:

? ? ? ? 局部打碼:

? ? ? ?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的图像马赛克原理及实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。