1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

#画像読み込みから面積算出までの関数を定義

scratch.py
import cv2
import math
import numpy as np
import os

def analyze (path, image):
    #入力画像(カラー)の読み込み
    img_src = cv2.imread(path + "/" +image, 1)     
    
    #エッジ検出、canny法
    img_dst = cv2.Canny(img_src, 30, 100) #出力画像 = cv2.Canny(img_src, 閾値1, 閾値2)

    #平滑化
    i=0
    while i < 13:
        img_dst2 = cv2.GaussianBlur(img_dst, (11, 11), 1)
        i = i + 1


    #膨張収縮処理
    #一回膨張処理
    img_dst3 = cv2.dilate(img_dst2, element8, iterations = 1) 

    #5回収縮処理
    i = 0
    while i < 5:
        img_dst3 = cv2.erode(img_dst3, element8, iterations = 1)
        i = i + 1

    #5回膨張処理        
    i=0
    while i < 5:
        img_dst3 = cv2.dilate(img_dst3, element8, iterations = 1)
        i = i + 1

    #2値化
    thresh = 60
    ret, img_dst4 = cv2.threshold(img_dst3, thresh, 255, cv2.THRESH_BINARY)
    #出力画像 = cv2.thresh(img_src, 閾値, 最大値、閾値処理の種類)
    #処理の種類(THRESH_BINARY_INV、THRESH_TRUNC、THRESH_TOZERO、THRESH_TOZERO_INV)

    #反転
    img_dst5 = 255 - img_dst4
    
    #面積を取得
    whitearea = cv2.countNonZero(img_dst5)
    return whitearea
    
    #輪郭座標取得
    contours = cv2.findContours(img_dst5, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]
    
    #取得した輪郭を描く
    analyzed_img = cv2.drawContours(img_src, contours, -1, (0,255,0), 3)
    cv2.imwrite(path2 + "/" + "analyzed_" + image , analyzed_img)

#バッチ処理
フォルダの中の画像を解析して、エクセルにまとめます。

batch.py
#ファイル名取得 ()
path = "/Users/hoge" #hoge:画像が入っているフォルダ
path2 = "/Users/hogehoge" #hogehoge:輪郭を合成した画像、および面積集計エクセルファイルを保存するフォルダ
files = os.listdir(path)

#面積取得
areas = []
for i in files:
    areas.append(analyze(path, i))
    
#処理した画像の名前(.jpgの前)取得 
samples = []
for i in files:
    samples.append(i.replace('.jpg', ''))
    
#エクセルに出力
import pandas as pd
import openpyxl
df = pd.DataFrame(areas, index=samples, columns=["Area"])
df.to_excel(path2 + "/" + "Result.xlsx")

#最後に
改良したいけど、もうrdkitとかdeepchemの方に心が移ってしまいました。
NGSの解析のためにR言語も学ばねば!

いろいろ勉強する時間が欲しいですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?