顯示具有 程式 標籤的文章。 顯示所有文章
顯示具有 程式 標籤的文章。 顯示所有文章

2012年7月30日 星期一

[724] Wavelet transform in Python

I was recently finished the job that making the wavelet transform (2dwavelet97lift.py) appliable for any size and any aspect ratio of images. Before that, of course, I’ve spent much time for understading basics of descrete wavelet transformation. Using the sample code of CDF 9/7 Discrete Wavelet Transform which the file named “2dwavelet97lift.py”. Indeed, it is a 2D wavelet transform but only for square image and the length must be 23 while it does 3 level transform. My job is split the 2D transform into two 1D transform while its 2D transform transpose the image two times which only works on square image. This can solve the problem and be applied for any aspect ratio of images. Let’s see the result of sample image which in size of 1600x1000.

2d_a1 original image

2d_a1_fwt image after fwt

2d_a1_iwt image after iwt

How about the other problem? In this, I use a stupid but really working way: resize it! Resize to what size? It depends on the level of transformation. We can resize the length of the image to the multiplication of (2 << level) and must larger than the original.

You can download the sample program here.
Python 2.7 and PIL are required to be installed.
Click the cmd file to run and you can also edit the parameters:
  pyc_filename [image_filename] [transform_level]

2012年6月29日 星期五

[624][629] JPEG in DCT vs DWT

 

在6/24只花兩三天完成的JPEG in DCT

花了兩天完成的jpeg壓縮

並沒有進行包裝,
只有進行 DCT –> quantization –> dequantization –> IDFT 這些步驟而已。
使用的quantization table為:

16  11  10  16  24   40   51   61  
12  12  14  19  26   58   60   55  
14  13  16  24  40   57   69   56  
14  17  22  29  51   87   80   62  
18  22  37  56  68   109  103  77  
24  35  55  64  81   104  113  92  
49  64  78  87  103  121  120  101 
72  92  95  98  112  100  103  99  

而新出的JPEG2000則是使用DWT來進行轉換,
據說可以達到良好的壓縮效果以及品質。
於是我使用discrete biorthogonal CDF 9/7 wavelet transform內的程式碼
來做個實驗,只將DCT調換成DWT。

結果比較如下圖(上為DCT,下為DWT):

DCT_vs_DWT

放大來比較(上為DCT,下為DWT),可見DWT明顯出現方格條紋。

DCT_vs_DWT

實驗結果如果用以下步驟:

DWT –> quantization –> dequantization –> IDWT

其效果不甚理想,可見JPEG2000仍隱藏一些奧秘在裡頭。

2012年3月20日 星期二

[321程式] C版的explode() 函式–explode() in C

/*

* explode function like in PHP (realloc version)

* completed in 2012/3/21

* since 2012/3/21

*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

char **explode(const char *del, const char *src, int *size);

 

int main(int argc, char *argv[]) {

     char *str = "We will we will rock you!";

     char **new_str;

     int i, len;

 

     printf("The original str: %s\n", str);

     new_str = explode("* ", str, &len);

     printf("The length: %d\n", len);

     printf("After explode:\n");

     for(i = 0; i < len; i++){

         printf("%s\n", new_str[i]);

     }

 

     system("pause");

 

     return 0;

}

 

char **explode(const char *del, const char *src, int *size) {

     int i = 0;

     int idx_del;

     int idx_src = 0;

     int num_token = 0;

     int last_pos = 0;

     char **des = NULL;

 

     // get the token and doing explode

     while(*(src+idx_src) != '\0') {

         idx_del = 0;

         while(*(del+idx_del) != '\0') {

              if(*(src+idx_src) == *(del+idx_del)){

                  // increase the num of token

                  num_token++;

                  // re-allocate the space for 'des'

                  des = (char**)realloc(des, (num_token)*sizeof(char*));

                  // allocate in real time for the new string

                  des[i] = (char*)malloc((idx_src-last_pos+1)*sizeof(char));

                  // put the 'end of string' in the last item

                  des[i][idx_src-last_pos] = '\0';

                  // copy the specific section to be the new string

                  strncpy(des[i++], src+last_pos, idx_src-last_pos);

                  // record the position for the next new string

                  last_pos = idx_src+1;

                  // exit the current loop

                  break;

              }

              idx_del++;

         }

         idx_src++;

     }

     num_token += 1;

     // assign the num of token to the variable from outside

     *size = num_token;

 

     // don't forget the last one if there is

     des[num_token-1] = (char*)malloc((idx_src-last_pos+1)*sizeof(char));

     des[num_token-1][idx_src-last_pos] = '\0';

     strncpy(des[num_token-1], src+last_pos, idx_src-last_pos);

 

     return des;

}

2021年11月 6N6P真空管耳擴

  今年某月看到下面這位大大的貼文就想造出來一台真空管耳擴。 OTL WCF headphone amp build + awesome prototype material 從規劃製作、蒐集元件到動手製作前前後後大約花了三個月吧! 真正動手組裝大概是11月初的事情,短短一兩的禮...