# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] papermill={"duration": 0.029826, "end_time": "2021-06-06T18:53:08.497277", "exception": false, "start_time": "2021-06-06T18:53:08.467451", "status": "completed"} tags=[]
# # BUĞDAY BAŞAKLARININ TESPİT EDİLMESİ: GÖRÜNTÜLERİN ANALİZ EDİLEREK BAŞAK YOĞUNLUĞU, SAĞLIK VE OLGUNLUK BİLGİLERİNİN ELDE EDİLMESİ  
#  
#  -----

# + [markdown] papermill={"duration": 0.028482, "end_time": "2021-06-06T18:53:08.554491", "exception": false, "start_time": "2021-06-06T18:53:08.526009", "status": "completed"} tags=[]
# ### ÖZET
#
#
# Bu çalışmamızda buğday başaklarının takibini yapılarak sağlıklı gelişim süreci sağlanması için optik görüntülerden başak yoğunluğu, olgunluk ve temel sağlık bilgilerinin çıkarımı hedeflenmektedir. Bu amaçla görüntü işleme ve makine öğrenimi metotları kullanılarak buğday fotoğraflarından temel sağlık verileri elde edilmesi amaçlanmaktadır. Bu veriler buğday üreticisinin üretim sürecindeki kayıplara ilişkin çözümlerin oluşturulması ve kaliteli ürün elde edilmesi açısından fayda sağlayacaktır.
#
# ### ABSTRACT
#
#
# In this study, it is aimed to extract spike density, maturity and basic health information from optical images in order to provide a healthy development process by monitoring wheat ears. Fort his purpose it is aimed to obtain basic health data from wheat optic images by using image processing and machine learning methods. These data will be beneficial in creating solutions for the losses in the production process of the wheat producer and obtaining quality products.
#
#
# ****

# + _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" papermill={"duration": 0.22996, "end_time": "2021-06-06T18:53:08.813310", "exception": false, "start_time": "2021-06-06T18:53:08.583350", "status": "completed"} tags=[]
# KÜTÜPHANELERİN YÜKLENMESİ / İMPORT LİBRARY

import os
import datetime
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import cv2



def printime(title):
    print("{0}: {1}".format(title, datetime.datetime.now()))


# + papermill={"duration": 0.064661, "end_time": "2021-06-06T18:53:08.897526", "exception": false, "start_time": "2021-06-06T18:53:08.832865", "status": "completed"} tags=[]
# Model Sonuçlarının Yüklenmesi / Load Model Results
latesub = pd.read_csv("../input/wheat-latesub/submission.csv")
print("Trained model summary output shape:",latesub.shape)
imageid_count = latesub['image_id'].value_counts()
print("Example dataset image count: ",len(imageid_count))
print(imageid_count)
latesub.head()

# + papermill={"duration": 0.04508, "end_time": "2021-06-06T18:53:08.972981", "exception": false, "start_time": "2021-06-06T18:53:08.927901", "status": "completed"} tags=[]
latesub["PredictionString"] = latesub["PredictionString"].str.split(' ')
latesub.head()


# + papermill={"duration": 0.074076, "end_time": "2021-06-06T18:53:09.067969", "exception": false, "start_time": "2021-06-06T18:53:08.993893", "status": "completed"} tags=[]
# Yüklenen Sonuçların Düzenlenmesi / Editing Uploaded Results
def preparingdf(df):
    iou = []
    tx = []
    ty = []
    tw = []
    th = []
    for i in df.iterrows():
        idx0 = i[1][1][0]
        idx1 = i[1][1][1]
        idx2 = i[1][1][2]
        idx3 = i[1][1][3]
        idx4 = i[1][1][4]
        iou.append(idx0)
        tx.append(idx1)
        ty.append(idx2)
        tw.append(idx3)
        th.append(idx4)
        
    df[['iou']] = iou
    df[['tx']] = tx
    df[['ty']] = ty
    df[['tw']] = tw
    df[['th']] = th
    

    return df


data = preparingdf(latesub)
data.iou = data.iou.astype(float)
data.tx = data.tx.astype(float)
data.ty = data.ty.astype(float)
data.tw = data.tw.astype(float)
data.th = data.th.astype(float)
data.head()

# + papermill={"duration": 0.035042, "end_time": "2021-06-06T18:53:09.123579", "exception": false, "start_time": "2021-06-06T18:53:09.088537", "status": "completed"} tags=[]
data.groupby(['image_id'])['iou'].max()

# + papermill={"duration": 0.07668, "end_time": "2021-06-06T18:53:09.221084", "exception": false, "start_time": "2021-06-06T18:53:09.144404", "status": "completed"} tags=[]
df = data.groupby(['image_id']).mean()

df[['count']] = imageid_count
df[['iou_min']] = data.groupby(['image_id'])['iou'].min()
df[['iou_max']] = data.groupby(['image_id'])['iou'].max()
df[['tw_min']] = data.groupby(['image_id'])['tw'].min()
df[['tw_max']] = data.groupby(['image_id'])['tw'].max()
df[['th_min']] = data.groupby(['image_id'])['th'].min()
df[['th_max']] = data.groupby(['image_id'])['th'].max()

df['FX'] = (1024*1024)//(df.tw_max*df.th_max)
df['FY'] = df['count']/df.FX

df.head()

# + papermill={"duration": 0.038725, "end_time": "2021-06-06T18:53:09.291557", "exception": false, "start_time": "2021-06-06T18:53:09.252832", "status": "completed"} tags=[]
df.columns

# + papermill={"duration": 0.027599, "end_time": "2021-06-06T18:53:09.340646", "exception": false, "start_time": "2021-06-06T18:53:09.313047", "status": "completed"} tags=[]
df.shape

# + papermill={"duration": 0.043494, "end_time": "2021-06-06T18:53:09.405340", "exception": false, "start_time": "2021-06-06T18:53:09.361846", "status": "completed"} tags=[]
columns = ['iou', 'tx', 'ty', 'tw', 'th']
df.drop(columns, inplace=True, axis=1)
df.head()

# + [markdown] papermill={"duration": 0.021507, "end_time": "2021-06-06T18:53:09.448868", "exception": false, "start_time": "2021-06-06T18:53:09.427361", "status": "completed"} tags=[]
#

# + papermill={"duration": 0.039603, "end_time": "2021-06-06T18:53:09.510123", "exception": false, "start_time": "2021-06-06T18:53:09.470520", "status": "completed"} tags=[]
test_dir = "../input/global-wheat-detection/test/"
testfile = os.listdir(test_dir)

# + papermill={"duration": 0.031184, "end_time": "2021-06-06T18:53:09.563836", "exception": false, "start_time": "2021-06-06T18:53:09.532652", "status": "completed"} tags=[]
len(testfile)

# + papermill={"duration": 0.077734, "end_time": "2021-06-06T18:53:09.664347", "exception": false, "start_time": "2021-06-06T18:53:09.586613", "status": "completed"} tags=[]
# Test görüntülerin yüklenmesi / Uploading test images
img = cv2.imread(test_dir+testfile[2])
img.shape

# + papermill={"duration": 0.175351, "end_time": "2021-06-06T18:53:09.874655", "exception": false, "start_time": "2021-06-06T18:53:09.699304", "status": "completed"} tags=[]
# Görüntün BGR formatından LAB formatına dönüştürülmesi / Converting the image from BGR format to LAB format
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)

l = lab[:,:,0]
a = lab[:,:,1]
b = lab[:,:,2]


# + papermill={"duration": 0.03309, "end_time": "2021-06-06T18:53:09.930482", "exception": false, "start_time": "2021-06-06T18:53:09.897392", "status": "completed"} tags=[]
def summary(test_dir):
    testfiles = os.listdir(test_dir)
    for i in len(testfiles):
        img = cv2.imread(test_dir+testfile[i])
        lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
        l = lab[:,:,0]
        a = lab[:,:,1]
        b = lab[:,:,2]
        clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(16,16))
        clahe_img = clahe.apply(l)
        img_result = cv2.merge((clahe_img,a,b))
        imgCLAHE = cv2.cvtColor(img_result, cv2.COLOR_LAB2RGB)


# + papermill={"duration": 4.372435, "end_time": "2021-06-06T18:53:14.325453", "exception": false, "start_time": "2021-06-06T18:53:09.953018", "status": "completed"} tags=[]
# LAB formatındaki görüntünün histogram çıktısı / Histogram output of image in LAB format
plt.hist(l.flat, bins=100, range=(0,255))

# + papermill={"duration": 4.274734, "end_time": "2021-06-06T18:53:18.624548", "exception": false, "start_time": "2021-06-06T18:53:14.349814", "status": "completed"} tags=[]
# Yukarıdaki LAB formatına CHALE yöntemi uygulanması / Applying the CHALE method to the above LAB format
clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(16,16))
clahe_img = clahe.apply(l)

plt.hist(clahe_img.flat, bins=100, range=(0,255))

# + papermill={"duration": 0.046805, "end_time": "2021-06-06T18:53:18.696128", "exception": false, "start_time": "2021-06-06T18:53:18.649323", "status": "completed"} tags=[]
img_result = cv2.merge((clahe_img,a,b))
imgNORM = cv2.cvtColor(lab, cv2.COLOR_LAB2RGB) # CHALE uygulanmamış / not apply CHALE
imgCLAHE = cv2.cvtColor(img_result, cv2.COLOR_LAB2RGB) # CHALE uygulanmış / apply CHALE

# + papermill={"duration": 0.241493, "end_time": "2021-06-06T18:53:18.963432", "exception": false, "start_time": "2021-06-06T18:53:18.721939", "status": "completed"} tags=[]
# Görüntünün ilk hali... / First State
plt.imshow(imgNORM)

# + papermill={"duration": 0.320261, "end_time": "2021-06-06T18:53:19.324466", "exception": false, "start_time": "2021-06-06T18:53:19.004205", "status": "completed"} tags=[]
# CHALE uygulandıktan sonra... / Last State 
plt.imshow(imgCLAHE)

# + papermill={"duration": 0.34432, "end_time": "2021-06-06T18:53:19.701141", "exception": false, "start_time": "2021-06-06T18:53:19.356821", "status": "completed"} tags=[]
im = imgCLAHE
im = (im/(im.max()-im.min()))*255
im_r = im[:,:,0]
im_g = im[:,:,1]
im_b = im[:,:,2]
im_ = ((im_r**2)+(im_g**2)+(im_b**2))**(1/2)
r=im_r/im_
g=im_g/im_
b=im_b/im_
img__ = cv2.merge((r,g,b))
plt.imshow(img__) #NORMALİZASYON UYGULANMIŞ GÖRÜNTÜ

# + papermill={"duration": 0.266156, "end_time": "2021-06-06T18:53:20.016560", "exception": false, "start_time": "2021-06-06T18:53:19.750404", "status": "completed"} tags=[]
# Sadece yeşil kanala CHALE uygulanması durumunda / AND apply CLAHE ONLY GREEN CHANNEL
clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(64,64))
#clahe_img0 = clahe.apply(img[:,:,0])
clahe_img1 = clahe.apply(img[:,:,1]) # Green Channel
#clahe_img2 = clahe.apply(img[:,:,2])

img_result = cv2.merge((img[:,:,0],clahe_img1,img[:,:,2]))
img_result = cv2.cvtColor(img_result, cv2.COLOR_BGR2RGB)
plt.imshow(img_result)

# + papermill={"duration": 0.061015, "end_time": "2021-06-06T18:53:20.130719", "exception": false, "start_time": "2021-06-06T18:53:20.069704", "status": "completed"} tags=[]
imgCLAHE.shape # Görüntü çözünürlük değeri


# + papermill={"duration": 0.16078, "end_time": "2021-06-06T18:53:20.345077", "exception": false, "start_time": "2021-06-06T18:53:20.184297", "status": "completed"} tags=[]
def inorm(im): #Normalize Fonksiyonu
    im = (im/(im.max()-im.min()))*255
    im_r = im[:,:,0]
    im_g = im[:,:,1]
    im_b = im[:,:,2]
    im_ = ((im_r**2)+(im_g**2)+(im_b**2))**(1/3)
    r=im_r/im_
    g=im_g/im_
    b=im_b/im_
    img__ = cv2.merge((r,g,b))
    return img__

def rgb_density(img): #Renk Yoğunluk Fonksiyonu
    img = inorm(img)
    r = img[:,:,0]
    g = img[:,:,1]
    b = img[:,:,2]
    s = img.sum()
    d1 = r.sum()/s
    d2 = g.sum()/s
    d3 = b.sum()/s
    return [d1,d2,d3]
    
rgb_density(imgNORM)
    

# + papermill={"duration": 0.155032, "end_time": "2021-06-06T18:53:20.553931", "exception": false, "start_time": "2021-06-06T18:53:20.398899", "status": "completed"} tags=[]
rgb_density(imgCLAHE) #CHALE uygulanmış görüntüdeki renk yogunluk değeri sırasıyla (R,G,B)

# + papermill={"duration": 0.158897, "end_time": "2021-06-06T18:53:20.767441", "exception": false, "start_time": "2021-06-06T18:53:20.608544", "status": "completed"} tags=[]
rgb_density(img_result) #Yanlız yeşil kanala CHALE uygulanmış görüntüdeki renk yogunluk değeri sırasıyla (R,G,B)

# + papermill={"duration": 0.153638, "end_time": "2021-06-06T18:53:20.976276", "exception": false, "start_time": "2021-06-06T18:53:20.822638", "status": "completed"} tags=[]
rgb_density(img__)


# + papermill={"duration": 0.050895, "end_time": "2021-06-06T18:53:21.068760", "exception": false, "start_time": "2021-06-06T18:53:21.017865", "status": "completed"} tags=[]
def summary(df, test_dir):
    testfiles = os.listdir(test_dir)
    df[['y_r']] = 0
    df[['y_g']] = 0
    df[['y_b']] = 0

    for i in range(len(testfiles)):
        img = cv2.imread(test_dir+testfile[i])
        lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
        l = lab[:,:,0]
        a = lab[:,:,1]
        b = lab[:,:,2]
        clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(16,16))
        clahe_img = clahe.apply(l)
        img_result = cv2.merge((clahe_img,a,b))
        imgCLAHE = cv2.cvtColor(img_result, cv2.COLOR_LAB2RGB)
        yogunluk = rgb_density(imgCLAHE)
        df['y_r'][i:i+1] = yogunluk[0]
        df['y_g'][i:i+1] = yogunluk[1]
        df['y_b'][i:i+1] = yogunluk[2]

    return df


# + papermill={"duration": 1.400732, "end_time": "2021-06-06T18:53:22.510655", "exception": false, "start_time": "2021-06-06T18:53:21.109923", "status": "completed"} tags=[]
df_ = summary(df, test_dir)
df_

# + [markdown] papermill={"duration": 0.039609, "end_time": "2021-06-06T18:53:22.590459", "exception": false, "start_time": "2021-06-06T18:53:22.550850", "status": "completed"} tags=[]
# YARARLANILAN KAYNAKLAR  
# [1] https://docs.opencv.org/master/d5/daf/tutorial_py_histogram_equalization.html  
# [2] https://medium.com/@ahmetkumas1/histogram-equalization-clahe-69cc9f83670c  
#

# + [markdown] papermill={"duration": 0.039768, "end_time": "2021-06-06T18:53:22.670244", "exception": false, "start_time": "2021-06-06T18:53:22.630476", "status": "completed"} tags=[]
# *Bu çalışma Şems Kurtoğlu tarafından hazırlanmış olup tez danışmanlığını Doç. Dr. <NAME> yapılmaktadır.*

StarcoderdataJupyterScriptsDedupFiltered