Permalink
Browse files

update convolution

  • Loading branch information...
1 parent 78d34aa commit 83d3ca0c6caa506f7af61906acbc20297202ad8d @brilliun committed May 8, 2012
Showing with 89 additions and 36 deletions.
  1. +60 −36 src/theorems/ConvolutionTheorem.java
  2. +29 −0 src/util/ImgCommonUtil.java
@@ -39,7 +39,13 @@
private static int startY_64 = 81;
- public void setPlaneImg(BufferedImage img){
+
+ private Complex[][] patchFourier;
+
+ private Complex[][] filterFourier;
+
+
+ public void setTargetImg(BufferedImage img){
this.planeImg = img;
}
@@ -62,9 +68,13 @@ public void setFilter(IFilter filter){
public void generatePatchFourier(){
- BufferedImage patchFourier = ImgCommonUtil.getFourierImage(patchImg);
+ this.patchFourier = FourierTransformUtil.FFT2D(patchWidth, patchHeight, false, ImgCommonUtil.readGrayscaleImageData(patchImg));
+
- ImgCommonUtil.writeToFile(patchFourier, "patchFourier");
+
+ BufferedImage patchFourierImg = ImgCommonUtil.getFourierImage(patchImg);
+
+ ImgCommonUtil.writeToFile(patchFourierImg, "patchFourier");
}
@@ -77,12 +87,12 @@ public void generateFilter(int size){
gaborFilter.buildKernel(size, size);
- double[][] kernelDataRe = gaborFilter.getKernel().getRealKernelMatrix();
-
- BufferedImage kernelImg = ImgCommonUtil.writeToImageNormalize(size, size, false, kernelDataRe);
+// double[][] kernelDataRe = gaborFilter.getKernel().getRealKernelMatrix();
+//
+// BufferedImage kernelImg = ImgCommonUtil.writeToImageNormalize(size, size, false, kernelDataRe);
- ImgCommonUtil.writeToFile(kernelImg, "kernelImg");
+// ImgCommonUtil.writeToFile(kernelImg, "kernelImg");
setFilter(gaborFilter);
@@ -94,20 +104,22 @@ public void generateFilter(int size){
public void generateFilterFourier(){
- Complex[][] filterFourier = FourierTransformUtil.FFT2D(filterWidth, filterHeight, false, filter.getKernel().getComplexKernelMatrix());
+ this.filterFourier = FourierTransformUtil.FFT2D(filterWidth, filterHeight, false, filter.getKernel().getComplexKernelMatrix());
- BufferedImage filterFourierImg = ImgCommonUtil.writeToImageNormalize(filterWidth, filterHeight, true, filterFourier);
+
+
+ BufferedImage filterFourierImg = ImgCommonUtil.writeToImageNormalize(filterWidth, filterHeight, true, this.filterFourier);
ImgCommonUtil.writeToFile(filterFourierImg, "filterFourierImg");
}
- public void generateConvolveResultFourier(){
+ public Complex[][] generateConvolveResultFourier(int startX, int startY){
int conWidth = 64;
int conHeight = 64;
- Complex[][] convolveResult = doSpatialConvolution(startX_32, startY_32, conWidth, conHeight);
+ Complex[][] convolveResult = doSpatialConvolution(startX, startY, conWidth, conHeight);
// ImgCommonUtil.writeToFile(ImgCommonUtil.writeToImage(conWidth, conHeight, false, convolveResult), "convolve");
@@ -116,10 +128,12 @@ public void generateConvolveResultFourier(){
BufferedImage convolveResultFourierImg = ImgCommonUtil.writeToImage(conWidth, conHeight, true, convolveResultFourier);
ImgCommonUtil.writeToFile(convolveResultFourierImg, "convolveResultFourier");
+
+ return convolveResultFourier;
}
- public Complex[][] doSpatialConvolution(int startX, int startY, int width, int height){
+ private Complex[][] doSpatialConvolution(int startX, int startY, int width, int height){
int endX = startX + width;
int endY = startY + height;
@@ -143,35 +157,45 @@ public void generateConvolveResultFourier(){
public static void main(String[] args){
-// String imgPath = "img/patch64.bmp";
-// BufferedImage patchImg = null;
-//
-// String targetPath = "img/5gray.bmp";
-// BufferedImage targetImg = null;
-//
-// try {
-// patchImg = ImageIO.read(new File(imgPath));
-// targetImg = ImageIO.read(new File(targetPath));
-// } catch (IOException e1) {
-// // TODO Auto-generated catch block
-// e1.printStackTrace();
-// }
-//
+ String imgPath = "img/patch64.bmp";
+ BufferedImage patchImg64 = null;
+
+ String targetPath = "img/5gray256.bmp";
+ BufferedImage targetImg = null;
+
+ try {
+ patchImg64 = ImageIO.read(new File(imgPath));
+ targetImg = ImageIO.read(new File(targetPath));
+ } catch (IOException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
ConvolutionTheorem convolution = new ConvolutionTheorem();
-// convolution.setPlaneImg(targetImg);
-//
-// convolution.setPatchImg(patchImg);
-//
-// convolution.generatePatchFourier();
+ convolution.setTargetImg(targetImg);
- convolution.generateFilter(64);
-// convolution.generateFilterFourier();
-//
-//
-// convolution.generateConvolveResultFourier();
+ convolution.setPatchImg(patchImg64);
+
+ convolution.generatePatchFourier();
+
+
+
+ int filterSize = 64;
+
+ convolution.generateFilter(filterSize);
+
+ convolution.generateFilterFourier();
+
+
+ Complex[][] fourierProduct = ImgCommonUtil.fourierProduct(filterSize, filterSize, convolution.filterFourier, convolution.patchFourier);
+
+
+ Complex[][] convolveResultFourier = convolution.generateConvolveResultFourier(startX_64, startY_64);
+
+ System.out.println(fourierProduct + "," + convolveResultFourier);
}
@@ -483,6 +483,35 @@ public static void writeToFile(BufferedImage img, String fileName){
}
+
+ public static Complex[][] fourierProduct(int width, int height, Complex[][] target, Complex[][] kernel){
+
+ Complex[][] product = new Complex[width][height];
+
+ if(target.length < width || kernel.length < width)
+ return null;
+
+ for(int i = 0; i < width; i++){
+
+ if(target[i].length < height || kernel[i].length < height)
+ throw new IllegalArgumentException();
+
+
+ for(int j = 0; j < height; j++){
+
+ product[i][j] = target[i][j].mul(kernel[i][j]);
+
+
+ }
+
+
+ }
+
+ return product;
+
+ }
+
+
private static int round(int v, int l, int h){
if(v < l)

0 comments on commit 83d3ca0

Please sign in to comment.