影像的放大,需要補充沒有的像素,常用的方法有
1.最臨近點插值演算法(Nearest Neighbor)
2.雙線性插值演算法(Bilinear Interpolation)
3.雙立方插值演算法( Bicubic Interpolation)
等等,詳細介紹請看(影像放大演算法)
現在給出用最臨近點插值方法將影像放大兩倍的程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <span style= "font-size:14px;" ><span style= "font-size:10px;" >p<span style= "font-family:Courier New;" >ublic void Todouble(){
int[] doubleData = new int[2*w*2*h];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
doubleData[2*x + 2*y *2* w] = data[x + y * w];
}
}
this.h = 2*h;
this.w = 2*w;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (y%2 == 1)
doubleData[x + y*w] = doubleData[x + (y-1)*w];
if (x%2 == 1)
doubleData[x + y*w] = doubleData[x-1 + y*w];
}
}
this.data = doubleData;
}</span></span></span>
|
登入後複製
效果如下:

效果如下:
就比較簡單,縮小就是去掉一些像素點。
縮小的程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <span style= "max-width:90%" ><span style= "font-size:10px;" > public void reduce(int a){
int nw = w/a;
int nh = h/a;
int[] d = new int[nw*nh];
for (int y = 0; y < nh; y++) {
for (int x = 0; x < nw; x++) {
d[x + y*nw] = data[a*x + a*y * w];
}
}
this.h = nh;
this.w = nw;
this.data = d;
}</span></span>
|
登入後複製
運作效果如下:
以上就是java 影像的放大與縮小的內容,更多相關內容請關注PHP中文網(www.php.cn)!
🎜🎜