# [C_AR161-易] PSNR計算 - [題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29310) - [峰值信噪比(Peak Signal o Noise Ratio, PSNR)](https://zh.wikipedia.org/wiki/%E5%B3%B0%E5%80%BC%E4%BF%A1%E5%99%AA%E6%AF%94) - 公式(已修正原圖中數字8被錯置為6的問題)  - 範例(已修正範例2輸出結果)  ## 程式碼(Java): ```java= import java.util.*; //[C_AR161-易] PSNR計算 //2019,12,29;19:18 public class Main { static Scanner sc = new Scanner(System.in); final static double x2peak = Math.pow(255, 2); public static void arrInput(int[][] arr) { for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[i].length; j++) arr[i][j] = sc.nextInt(); } public static double getMSE(int[][] x, int[][] y) { double MSE = 0; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) MSE += Math.pow(x[i][j] - y[i][j], 2); } return MSE / (x.length * x[0].length); } public static double getPSNR(double MSE) { double PSNR = 10 * (Math.log10(x2peak / MSE)); return PSNR; } public static void main(String[] args) { int m = sc.nextInt(); int n = sc.nextInt(); int[][] pic_1 = new int[n][m]; int[][] pic_2 = new int[n][m]; arrInput(pic_1); arrInput(pic_2); double MSE = getMSE(pic_1, pic_2); if (MSE == 0) System.out.println("0.00 infinity"); else { double PSNR = getPSNR(MSE); System.out.printf("%.3f %.3f\n", MSE, PSNR); } } } ``` ## 結果:  > [name=風思] > 已修正題目本身提供的錯誤資訊,程式的部分,則善用Math類別,注意出輸入。 [](https://sites.google.com/s/15xXm2t0rfZvyJwO6DutZkT_6ZPtEUnuR/p/19D0GWbNFWSxtc0CVt2g4AFB9s8hlDpUc/edit) ###### tags: `e tutor` `黎明高中` `LMcps` `[C_AR161-易] PSNR計算`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up