ユーザ用ツール

サイト用ツール


numericalipb:ans5

【数値情報処理b】第5回の答え

課題

label.R を書き換えて以下の画像から「公」の文字のみを抜き出すプログラムを作成しなさい。

https://www.kitp.org/numericalipb/nameplate1.jpg

ただし、画像は人によって違う。


解答例

report5.R
  1. # レポート
  2.  
  3. library(imager)
  4.  
  5. # グレースケール画像
  6. im <- grayscale(load.image("https://www.kitp.org/numericalipb/nameplate1.jpg"))
  7.  
  8.  
  9. # 2値化
  10. im.bin <- threshold(im, "auto")
  11.  
  12. # ラベリング
  13. x <- label(im.bin)
  14.  
  15. # ラベル付けされた図形の面積(画素数)
  16. areas <- table(x)
  17.  
  18. # 面積が100以上の図形のラベルと面積を出力
  19. print(areas[areas >= 100])
  20.  
  21. # ラベルを指定して図形を抜き出す
  22. im.ex <- im
  23. im.ex[x != 6 & x != 7 & x != 14] <- 1
  24.  
  25. layout(t(1 : 2))
  26. plot(im, main = "元画像")
  27. plot(im.ex, main = "抜き出した図形")

人によって 6 行目と 23 行目が異なる。

numericalipb/ans5.txt · 最終更新: 2025/07/16 14:22 by yuki