トップ «前の日記(2012/01/28 [土]) 最新 次の日記(2012/02/01 [水])» 編集

その他雑多なこと(tDiary版)

2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|12|
2012|01|02|03|04|05|06|07|

2012/01/31 [火] [長年日記]

_ [PC][自炊] コラムページテキスト2値化フィルタ

GIMP2.6.11向けにscript-fuで作成。写真とテキストが配置されているページでテキスト部分は2値化して、写真部分はそのまま残す。

写真部分の判定は2値化して黒くなった部分が多いところを写真と判定している。写真と判定された部分はFuzzy選択して選択部分に外接する矩形として処理。写真部分の判定で細かく領域選択して領域内の黒の割合を見ているので、時間がかかるのが難点。また、白っぽいところも写真と判定されない。

BWthreshold:2値化するときの閾値

check-step:写真判定用の領域サイズ(pixel)

flame-size:周りの余白部分のサイズ(pixel)白で塗りつぶす

(define (docscan-file-text-enhance filename bw_threshold check-step flame)
  (let* (
      (outfilename (string-append filename ".png"))
      (xcffilename (string-append filename ".xcf"))
      (img (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
      (layer (car (gimp-image-get-active-layer img)))
    )
    (docscan-text-enhance img bw_threshold check-step flame)
    (gimp-xcf-save 0 img layer xcffilename xcffilename)
    (set! layer (car (gimp-image-merge-visible-layers img CLIP-TO-BOTTOM-LAYER)))
    (file-png-save-defaults RUN-NONINTERACTIVE img layer outfilename outfilename)
  )
)
(script-fu-register
 "docscan-file-text-enhance"
 "/Xtns/DocScan/file-text-enhance..."
 "load file, Picture->no process Text->Black and White"
 "Kazunori Takeuchi "
 "Kazunori Takeuchi"
 "2012-01-11"
 ""
 SF-FILENAME "filename" "t0001.bmp"
 SF-ADJUSTMENT "BWthreshold" '(192 0 255 1 8 0 SF-SPINNER)
 SF-VALUE "check-step" "50"
 SF-VALUE "flame-size" "20"
)
(define (docscan-cut-largeblack img white-layer check-x check-y check-step)
  (let* (
      (hist 0)
      (none 0)
      (wariai 0)
    )
    (gimp-rect-select img check-x check-y check-step check-step CHANNEL-OP-REPLACE TRUE 0)
    (set! hist (cdr (gimp-histogram white-layer HISTOGRAM-VALUE 223 255)))
    (set! none (car hist)) ; mean
    (set! hist (cdr hist))
    (set! none (car hist)) ; std-dev
    (set! hist (cdr hist))
    (set! none (car hist)) ; median
    (set! hist (cdr hist))
    (set! none (car hist)) ; pixels
    (set! hist (cdr hist))
    (set! none (car hist)) ; count
    (set! wariai (car hist)) ; percentile
    (if (= wariai 0) 
      (begin 
      (gimp-fuzzy-select white-layer (+ check-x (/ check-step 2)) (+ check-y (/ check-step 2)) 0 CHANNEL-OP-REPLACE FALSE FALSE 0 TRUE)
      (let*
        (
          (bounds (cdr (gimp-selection-bounds img)))
          (x1 (car bounds))
          (bounds (cdr bounds))
          (y1 (car bounds))
          (bounds (cdr bounds))
          (x2 (car bounds))
          (bounds (cdr bounds))
          (y2 (car bounds))
        )
        (gimp-rect-select img x1 y1 (- x2 x1) (- y2 y1) CHANNEL-OP-ADD FALSE 0)
        (gimp-edit-fill white-layer BACKGROUND-FILL)
        (gimp-edit-cut white-layer)
      )
      )
    )
  )
)
(define (docscan-text-enhance img bw_threshold check-step flame)
  (gimp-palette-set-foreground `(0 0 0))
  (gimp-palette-set-background `(255 255 255))
  (let* (
      (layer (car (gimp-image-get-active-layer img)))
      (white-layer layer)
      (layer-height (car (gimp-drawable-height layer)))
      (layer-width (car (gimp-drawable-width layer)))
      (check-x 0)
      (check-y 0)
    )
    ;真っ白レイヤー作成
    (set! white-layer (car (gimp-layer-copy layer 1)))
    (gimp-image-add-layer img white-layer -1)
    (gimp-drawable-set-visible layer FALSE)
    ;真っ白レイヤー、正規化/2値化
    (gimp-selection-all img)
    (gimp-levels-stretch white-layer)
    (gimp-threshold white-layer bw_threshold 255)
    ;枠、余白、白塗りつぶし
    (docscan-flame-clear img flame)
    ;写真部分透明化
    (set! check-x 0)
    (while (<= check-x layer-width)
      (set! check-y 0)
      (while (<= check-y layer-height)
	(docscan-cut-largeblack img white-layer check-x check-y check-step)
        (set! check-y (+ check-y check-step))
      )
      (set! check-x (+ check-x check-step))
    )
    (gimp-selection-all img)
    (gimp-drawable-set-visible layer TRUE)
  )
)
(script-fu-register
 "docscan-text-enhance"
 "/Xtns/DocScan/text-enhance..."
 "Picture->no process Text->Black and White"
 "Kazunori Takeuchi "
 "Kazunori Takeuchi"
 "2012-01-11"
 ""
 SF-IMAGE "Image" 0
 SF-ADJUSTMENT "BWthreshold" '(192 0 255 1 8 0 SF-SPINNER)
 SF-VALUE "check-step" "50"
 SF-VALUE "flame-size" "20"
)
(define (docscan-flame-clear img check)
  (let* (
      (draw (car (gimp-image-get-active-layer img)))
      (layer-height (car (gimp-drawable-height draw)))
      (layer-width (car (gimp-drawable-width draw)))
    )
    ;裏当て除去
    (gimp-rect-select img check check (- layer-width (+ check check)) (- layer-height (+ check check)) CHANNEL-OP-REPLACE FALSE 0)
    (gimp-selection-invert img)
    (gimp-edit-fill draw BACKGROUND-FILL)
    (gimp-displays-flush)
    (gimp-selection-all img)
    ;余白選択
    (gimp-fuzzy-select draw (+ check check) (+ check check) 15 CHANNEL-OP-REPLACE FALSE FALSE 0 TRUE)
    ;ゴミ除去
    (gimp-selection-invert img)
    (gimp-selection-shrink img 1)
    (gimp-selection-grow img 1)
    (gimp-displays-flush)
    (let* (
        (bounds (cdr (gimp-selection-bounds img)))
        (x1 (car bounds))
        (bounds (cdr bounds))
        (y1 (car bounds))
        (bounds (cdr bounds))
        (x2 (car bounds))
        (bounds (cdr bounds))
        (y2 (car bounds))
      )
      (gimp-rect-select img x1 y1 (- x2 x1) (- y2 y1) CHANNEL-OP-REPLACE FALSE 0)
    )
    (gimp-selection-invert img)
    (gimp-edit-fill draw BACKGROUND-FILL)
  )
  (gimp-displays-flush)
)
(script-fu-register
 "docscan-flame-clear"
 "/Xtns/DocScan/flame-clear..."
 "scan image flame clear"
 "Kazunori Takeuchi "
 "Kazunori Takeuchi"
 "2012-01-11"
 ""
 SF-IMAGE "Image" 0
; SF-IMAGE "Drawable" 0
 SF-VALUE "check-step" "20"
)
本日のリンク元
その他のリンク元
検索

辞書