Home > Mobile >  Convert multi class classification to binary image classification
Convert multi class classification to binary image classification

Time:01-14

I would like to convert a multi-class image to binary, to result in two bati / non-bati classes for that I started by writing this little code, but I don't know how to assign the value 0 to my classes ( 2,3,4,5,6) and the value 1 to class 1.

here is my code

import georasters as gr
import pandas as pd
img = gr.from_file('D:/Thèse/Partie 2/ZMU/filtre_classification_2018_7classes_fin.tif')
img=img.to_pandas()
img.head()

img =img.loc[:,['value','x','y']]
img = img.rename(columns = {'value':'class', 'x':'Latitude', 'y':"Longitude"})
img.head()


#df['class'] = df['class'].map({'first_element':1, 'second_element':2,'third_element':3})

df = pd.DataFrame([img], columns=["1","3","5","7","9","11"])

CodePudding user response:

You could simply use df.loc to replace your classes

df.loc[df.class!=1,'class']=0
df.loc[df.class==1,'class']=1

This should set everything with class=1 to 0 and everything else to 1.

  •  Tags:  
  • Related