|
| 1 | +package borno; |
| 2 | + |
| 3 | +import image.ImageProcessing; |
| 4 | + |
| 5 | +import java.awt.image.BufferedImage; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Stack; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by Jobayer on 8/6/2016. |
| 11 | + */ |
| 12 | +public class LetterSeparation { |
| 13 | + |
| 14 | + public static void separateLetter() throws IOException { |
| 15 | + String basePath = "F:\\Academic\\semester 8\\Image Processing\\Workspace\\Assignments\\src\\borno\\resources\\"; |
| 16 | + BufferedImage image = ImageProcessing.readImage(basePath + "alo.jpg"); |
| 17 | + |
| 18 | + int[][][] rgb = ImageProcessing.getRGB(image); |
| 19 | + int[][] gray = ImageProcessing.rgbToGray(rgb); |
| 20 | + int[][] blackAndWhite = ImageProcessing.grayToBlackAndWhite(gray, 200); |
| 21 | + int width = blackAndWhite.length; |
| 22 | + int height = blackAndWhite[0].length; |
| 23 | + int[][] binary = new int[width][height]; |
| 24 | + int[] frequencyPerRow = new int[height]; |
| 25 | + |
| 26 | + // find frequency per row |
| 27 | + for (int i = 0; i < width; i++) { |
| 28 | + for (int j = 0; j < height; j++) { |
| 29 | + if (blackAndWhite[i][j] == 255) |
| 30 | + frequencyPerRow[j] += 1; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // find max frequency |
| 35 | + int maxFrequency = 0; |
| 36 | + for (int i = 0; i < height; i++) { |
| 37 | + if (frequencyPerRow[i] > maxFrequency) |
| 38 | + maxFrequency = frequencyPerRow[i]; |
| 39 | + } |
| 40 | + |
| 41 | + // remove matra |
| 42 | + for (int j = 0; j < height; j++) { |
| 43 | + if (frequencyPerRow[j] > 500) { |
| 44 | + for (int i = 0; i < width; i++) { |
| 45 | + blackAndWhite[i][j] = 0; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // binary |
| 51 | + for (int i = 0; i < width; i++) { |
| 52 | + for (int j = 0; j < height; j++) { |
| 53 | + if (blackAndWhite[i][j] == 255) |
| 54 | + binary[i][j] = 1; |
| 55 | + else |
| 56 | + binary[i][j] = 0; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // find region |
| 61 | + for (int i = 1; i < width-1; i++) { |
| 62 | + for (int j = 1; j < height-1; j++) { |
| 63 | + if (binary[i][j] == 1 && (binary[i][j-1] == 0 || binary[i][j+1] == 0 || binary[i-1][j] == 0 || binary[i+1][j] == 0)) { |
| 64 | + blackAndWhite[i][j] = 100; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +// print2dMatrix(blackAndWhite); |
| 69 | + |
| 70 | + // find imin, imax, jmin, jmax |
| 71 | + Stack<Integer> stack = new Stack<Integer>(); |
| 72 | + int imin = 0, imax = 0, jmin = 0, jmax = 0; |
| 73 | + int[] itemp = new int[width]; |
| 74 | + int[] jtemp = new int[height]; |
| 75 | + for (int i = 0; i < width; i++) { |
| 76 | + for (int j = 0; j < height; j++) { |
| 77 | + if (blackAndWhite[i][j] == 100) { |
| 78 | + stack.push(i); |
| 79 | + stack.push(j); |
| 80 | +// imin = 50000; imax = 0; jmin = 50000; jmax = 0; |
| 81 | + itemp = zeros(width); |
| 82 | + jtemp = zeros(height); |
| 83 | + int countTemp = 0; |
| 84 | + while(!stack.empty()) { |
| 85 | + int indexJ = stack.pop(); |
| 86 | + int indexI = stack.pop(); |
| 87 | + blackAndWhite[indexI][indexJ] = 1; |
| 88 | + |
| 89 | + /*imin = getMin(imin, indexI); |
| 90 | + imax = getMax(imax, indexI); |
| 91 | + jmin = getMin(jmin, indexJ); |
| 92 | + jmax = getMax(jmax, indexJ);*/ |
| 93 | + |
| 94 | + itemp[countTemp] = indexI; |
| 95 | + jtemp[countTemp] = indexJ; |
| 96 | + countTemp++; |
| 97 | + |
| 98 | + if (blackAndWhite[indexI-1][indexJ] == 100) { |
| 99 | + stack.push(indexI-1); |
| 100 | + stack.push(indexJ); |
| 101 | + } |
| 102 | + if (blackAndWhite[indexI+1][indexJ] == 100) { |
| 103 | + stack.push(indexI+1); |
| 104 | + stack.push(indexJ); |
| 105 | + } |
| 106 | + if (blackAndWhite[indexI][indexJ-1] == 100) { |
| 107 | + stack.push(indexI); |
| 108 | + stack.push(indexJ-1); |
| 109 | + } |
| 110 | + if (blackAndWhite[indexI][indexJ+1] == 100) { |
| 111 | + stack.push(indexI); |
| 112 | + stack.push(indexJ+1); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // crop image |
| 117 | + imin = findMin(itemp); |
| 118 | + imax = findMax(itemp); |
| 119 | + jmin = findMin(jtemp); |
| 120 | + jmax = findMax(jtemp); |
| 121 | + System.out.println(imin + " " + imax + " " + jmin + " " + jmax); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + BufferedImage grayImage = ImageProcessing.grayToImage(gray); |
| 128 | + BufferedImage rgbImage = ImageProcessing.rgbToImage(rgb); |
| 129 | + BufferedImage blackAndWhiteImage = ImageProcessing.grayToImage(blackAndWhite); |
| 130 | +// ImageProcessing.writeImage(grayImage, basePath + "grayImage.png"); |
| 131 | +// ImageProcessing.writeImage(rgbImage, basePath + "flowerOut.png"); |
| 132 | +// ImageProcessing.writeImage(blackAndWhiteImage, basePath + "blackAndWhiteImage.png"); |
| 133 | +// System.out.println(maxFrequency); |
| 134 | + } |
| 135 | + |
| 136 | + public static int getMax(int x, int y) { |
| 137 | + return x > y ? x : y; |
| 138 | + } |
| 139 | + |
| 140 | + public static int getMin(int x, int y) { |
| 141 | + return x < y ? x : y; |
| 142 | + } |
| 143 | + |
| 144 | + public static int findMax(int[] data) { |
| 145 | + int size = data.length; |
| 146 | + int max = data[0]; |
| 147 | + for (int i = 1; i < size; i++) { |
| 148 | + if (max < data[i]) |
| 149 | + max = data[i]; |
| 150 | + } |
| 151 | + return max; |
| 152 | + } |
| 153 | + |
| 154 | + public static int findMin(int[] data) { |
| 155 | + int size = data.length; |
| 156 | + int min = data[0]; |
| 157 | + for (int i = 1; i < size; i++) { |
| 158 | + if (min > data[i]) |
| 159 | + min = data[i]; |
| 160 | + } |
| 161 | + return min; |
| 162 | + } |
| 163 | + |
| 164 | + public static void getRegionIndex(int[][] data, int indexI, int indexJ) { |
| 165 | + int size = data.length; |
| 166 | + int[][] regionIndex = zeros2d(2, size); |
| 167 | + } |
| 168 | + |
| 169 | + public static int dfs(int[][] data, int indexI, int indexJ) { |
| 170 | + return 0; |
| 171 | + } |
| 172 | + |
| 173 | + public static int[][] zeros2d(int width, int height) { |
| 174 | + int[][] matrix = new int[width][height]; |
| 175 | + |
| 176 | + for (int i = 0; i < width; i++) { |
| 177 | + for (int j = 0; j < height; j++) { |
| 178 | + matrix[i][j] = 0; |
| 179 | + } |
| 180 | + } |
| 181 | + return matrix; |
| 182 | + } |
| 183 | + |
| 184 | + public static int[] zeros(int size) { |
| 185 | + int[] array = new int[size]; |
| 186 | + for (int i = 0; i < size; i++) { |
| 187 | + array[i] = 0; |
| 188 | + } |
| 189 | + return array; |
| 190 | + } |
| 191 | + |
| 192 | + public static void print2dMatrix(int[][] mat) { |
| 193 | + int width = mat.length; |
| 194 | + int height = mat[0].length; |
| 195 | + for (int i = 0; i < width; i++) { |
| 196 | + for (int j = 0; j < height; j++) { |
| 197 | + System.out.print(mat[i][j] + " "); |
| 198 | + } |
| 199 | + System.out.println(); |
| 200 | + } |
| 201 | + System.out.println(); |
| 202 | + } |
| 203 | +} |
0 commit comments