Here you can find the source of sectorHistorgramCompare(BufferedImage a, BufferedImage b)
Parameter | Description |
---|---|
a | a parameter |
b | a parameter |
public static int sectorHistorgramCompare(BufferedImage a, BufferedImage b)
//package com.java2s; //License from project: Apache License import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; public class Main { /**//ww w . j ava 2s. c o m * Hardcoded to 4 sectors at the moment * * @param a * @param b */ public static int sectorHistorgramCompare(BufferedImage a, BufferedImage b) { int result = Integer.MAX_VALUE; int filter = 10; int sqrtSectors = 3; Rectangle[] aSectors = buildSectors(a, sqrtSectors); Rectangle[] bSectors = buildSectors(b, sqrtSectors); int[][] aAverages = buildPixelAverages(a, aSectors); int[][] bAverages = buildPixelAverages(b, bSectors); int totalDelta = 0; int totalSectors = sqrtSectors * sqrtSectors; for (int i = 0; i < totalSectors; i++) { int[] aAverage = aAverages[i]; int[] bAverage = bAverages[i]; int redDelta = Math.abs(aAverage[0] - bAverage[0]); int greenDelta = Math.abs(aAverage[0] - bAverage[0]); int blueDelta = Math.abs(aAverage[0] - bAverage[0]); if (redDelta > filter || greenDelta > filter || blueDelta > filter) { totalDelta = Integer.MAX_VALUE; break; } else { totalDelta += redDelta + greenDelta + blueDelta; } } result = totalDelta / 3 / totalSectors; return result; } private static Rectangle[] buildSectors(BufferedImage a, int sqrtSectors) { int rows = sqrtSectors; int columns = sqrtSectors; int sectorWidth = a.getWidth() / columns; int sectorHeight = a.getHeight() / columns; Rectangle[] sectors = new Rectangle[sqrtSectors * sqrtSectors]; for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { int sectorStartX = x * sectorWidth; int sectorStartY = y * sectorHeight; int index = x + y * columns; sectors[index] = new Rectangle(sectorStartX, sectorStartY, sectorWidth, sectorHeight); } } return sectors; } private static int[][] buildPixelAverages(BufferedImage a, Rectangle[] sectors) { WritableRaster raster = a.getRaster(); int[][] result = new int[sectors.length][]; for (int s = 0; s < sectors.length; s++) { Rectangle sector = sectors[s]; int[] total = new int[4]; int[] pixel = new int[4]; for (int x = sector.x; x < sector.x + sector.width; x++) { for (int y = sector.y; y < sector.y + sector.height; y++) { pixel = raster.getPixel(x, y, pixel); total[0] += pixel[0]; total[1] += pixel[1]; total[2] += pixel[2]; } } int pixels = sector.width * sector.height; int[] average = new int[] { total[0] / pixels, total[1] / pixels, total[2] / pixels }; result[s] = average; } return result; } }