Saturday, April 6, 2024

ENVI: Instructions for converting ENVI format classification maps to GEOTIFF format

[1] Open the ENVI format classification image, such as image 2003; at this point, the image's save format is ENVI Classification, which can be checked by editing the image's header file (Edit Header). The method is to right-click on 2003 in the Available Bands List, and from the pop-up menu select Edit Header, as shown in the image below;
[2] In the Header Info dialog box, select TIFF from the File Type dropdown list and click OK. Afterward, the icon for 2003 changes, as shown below;
[3] In the ENVI main menu, select File->Save File As->TIFF/GEOTIFF, choose the output location in the dialog box, click OK to finish.

Thursday, February 29, 2024

Matlab: a simple approach for classifing two kinds of types on the image

It works well on small areas (here at the Plot level) and only with two types in the image that have significant color differences. File.
I added a loop to minimize the effect of the non-Rgeion of Interest on the binzrization process. 2nd. 3rd.
The conditions for applying the binary process are quite stringent. It's important to ensure that the image to be processed contains fairly typical features of the terrain colors and in appropriate quantities. Only then is it the right image to use, as this minimizes the area of invalid regions. 4th.

Wednesday, February 28, 2024

Matlab: Convert RGB to HSV/XYZ/LAB/Ycbcr/NTSC

File. 2nd.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
% Created by LI Xu
% Version 1.0
% February 22, 2024


% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy

clear;
clc;

timebegin=tic;
cur_data=date;
cur_time=fix(clock);
str1=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

filename='wheat';

% Source Directory
SouDir='input';
% Destination Directory
DesDir='output';

files=dir(SouDir);
files=files(3:end);


for ii=1:numel(files)
    % construct the input file path
    filepath=fullfile(SouDir, files(ii).name);

    [image, geo]=readgeoraster(filepath);
    image=double(image);

    try
        info=geotiffinfo(filepath);
    catch
        info=georasterinfo(filepath);
    end

    Num_bands=size(image, 3);

    if Num_bands==3
        RGB=image;
    else
        RGB=zeros(size(image, 1), size(image, 2), 3);
        RGB(:, :, 1)=image(:, :, 4);
        RGB(:, :, 2)=image(:, :, 2);
        RGB(:, :, 3)=image(:, :, 1);
    end

    % Normalise the RGB bands [0, 255]
    RGB=GenNormalise(RGB);


    % https://www.mathworks.com/help/images/understanding-color-spaces-and-color-space-conversion.html
    xyz=rgb2xyz(RGB);
    lab=rgb2lab(RGB);
    ycbcr=rgb2ycbcr(RGB);
    ntsc=rgb2ntsc(RGB);



    HSV=rgb2hsv(RGB);
    % ExG = 2 * G - R - B
    ExG=2*RGB(:, :, 2)-RGB(:, :, 1)-RGB(:, :, 3);



    otpath=strsplit(files(ii).name, '_');
    otpath=otpath(2:end);
    otpath=strjoin(otpath, '_');
    otpath_rgb=[filename, '_rgb_', otpath];
    otpath_hsv=[filename, '_hsv_', otpath];

    otpath_xyz=[filename, '_xyz_', otpath];
    otpath_lab=[filename, '_lab_', otpath];
    otpath_ycbcr=[filename, '_ycbcr_', otpath];
    otpath_ntsc=[filename, '_ntsc_', otpath];

    otpath_exg=[filename, '_exG_', otpath];


    otpath_rgb=fullfile(DesDir, otpath_rgb);
    otpath_hsv=fullfile(DesDir, otpath_hsv);
    otpath_xyz=fullfile(DesDir, otpath_xyz);
    otpath_lab=fullfile(DesDir, otpath_lab);
    otpath_ycbcr=fullfile(DesDir, otpath_ycbcr);
    otpath_ntsc=fullfile(DesDir, otpath_ntsc);
    otpath_exg=fullfile(DesDir, otpath_exg);


    try
        geotiffwrite(otpath_rgb, RGB, geo);
        geotiffwrite(otpath_hsv, HSV, geo);
        geotiffwrite(otpath_xyz, xyz, geo);
        geotiffwrite(otpath_lab, lab, geo);
        geotiffwrite(otpath_ycbcr, ycbcr, geo);
        geotiffwrite(otpath_ntsc, ntsc, geo);
        geotiffwrite(otpath_exg, ExG, geo);
    catch
        strcmd=['gdalinfo ', filepath];
        [~, cmdout]=system(strcmd);
        epsg=extractBetween(cmdout, "EPSG:"," got from GeoTIFF keys");
        epsg=epsg{1};
        % geotiffwrite(otpath, uint8(class_imag), geo, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
        geotiffwrite(otpath_rgb, RGB, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_hsv, HSV, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_xyz, xyz, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_lab, lab, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_ycbcr, ycbcr, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_ntsc, ntsc, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
        geotiffwrite(otpath_exg, ExG, geo, 'CoordRefSysCode', ['EPSG:', epsg], 'TiffType', 'bigtiff');
    end

    disp(['[', num2str(ii), '/', num2str(numel(files)), ']~', files(ii).name]);


end




fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

cur_data=date;
cur_time=fix(clock);
str2=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time End: ');
disp(str2);
timespan=toc(timebegin);
fprintf('Time Span: %.4f s\n', timespan);


disp('***********************************************');

function output=GenNormalise(input)

    output=input*0;

    for ii=1:size(input, 3)
        image=input(:, :, ii);
        % red_normalized = (red - red.min()) / (red.max() - red.min()) * 255
        aa=image(:);
        image_normalized=(image-min(aa))./(max(aa)-min(aa))*255.0;
        output(:, :, ii)=image_normalized;



    end


end

Matlab: Decision Tree Tool for Image

This is a simple tool of decison tree for the image analysis. It can export the compared image and the ruleset file at once. File.

Tuesday, February 20, 2024

Canopeo: Green and Non-Green

I am using Canopeo here to calculate the percentage of green and non-green (RGB) in photos, and also to extract the green parts of the photos. Files.
[1] Canopeo.
[2] Andres Patrignani, Tyson E. Ochsner. 2015. Canopeo: A Powerful New Tool for Measuring Fractional Green Canopy Cover. Agronomy Journal, 107(6): 2312~2320.

Wednesday, February 7, 2024

Py+eCognition: Set up and tips

  1. Set up
  2. Hexagon_esgmentation_python_cp.dcp
  3. segment-anything.dcp
  4. How to download the latest version of eCognition? Click this link.
  5. How to install eCognition on Windows? Click this link for License Manager, click this link for Developer
  6. If you do not have a valid license, please request a trial version. Trial software access is not limited to a specific time period, but export and save functions, and the workspace environment are restricted. Rulesets saved in trial software cannot be opened in a fully-licensed version of eCognition software.
  7. From the Ground Up videos

Monday, February 5, 2024

ML: A simple SAM program from input image to output mask, saving as .tif format

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# https://github.com/facebookresearch/segment-anything/issues/221

import cv2, os
import matplotlib.pyplot as plt
sam_checkpoint='D:/PyTest/kkk/sam_vit_l_0b3195.pth'
model_type="vit_l"
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)

inpath='D:/PyTest/kkk/dog.jpg'
img_arr=cv2.imread(inpath)
img_arr=cv2.cvtColor(img_arr,cv2.COLOR_BGR2RGB)

mask_generator=SamAutomaticMaskGenerator(sam)
# mask_generator = SamAutomaticMaskGenerator(
#     model=sam,
#     points_per_side=32,
#     pred_iou_thresh=0.86,
#     stability_score_thresh=0.92,
#     crop_n_layers=1,
#     crop_n_points_downscale_factor=2,
# #     # min_mask_region_area=100,  # Requires open-cv to run post-processing
# )
predictor=mask_generator.generate(img_arr)

# Choose the first mask
# mask=predictor[0]['segmentation']
# # Remove background by turn it to white
# img_arr[mask==False]=[255, 255, 255]


newimg = img_arr[:, :, 0] * 0
for ii in range(len(predictor)):
    # print(ii)
    mask=predictor[ii]['segmentation']
    # newimg = img_arr[:, :, 0] * 0
    newimg[mask == True] = ii+1
    # filename = os.path.join('D:/PyTest/kkk/export',str(ii+1)+'.tif')
    # cv2.imwrite(filename, newimg)

# plt.imshow(img_arr)
# plt.axis('off')
# plt.show()
filename='D:/PyTest/kkk/dog_new.tif'
cv2.imwrite(filename, newimg)

ML: SAM changed the background to white or other colors

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# https://github.com/facebookresearch/segment-anything/issues/221
import cv2
import matplotlib.pyplot as plt
sam_checkpoint='D:/PyTest/kkk/sam_vit_l_0b3195.pth'
model_type="vit_l"
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)

inpath='D:/PyTest/kkk/girl.png'
img_arr=cv2.imread(inpath)
img_arr=cv2.cvtColor(img_arr,cv2.COLOR_BGR2RGB)

mask_generator=SamAutomaticMaskGenerator(sam)
predictor=mask_generator.generate(img_arr)

# Choose the first mask
mask=predictor[0]['segmentation']
# Remove background by turn it to white
img_arr[mask==False]=[255, 255, 255]

# plt.imshow(img_arr)
# plt.axis('off')
# plt.show()
filename='D:/PyTest/kkk/girl_new.png'
img_arr=cv2.cvtColor(img_arr,  cv2.COLOR_BGR2RGB)
cv2.imwrite(filename, img_arr)

Friday, February 2, 2024

ML: Automatically generating object masks with SAM

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Automatically generating object masks with SAM
# https://github.com/facebookresearch/segment-anything/blob/main/notebooks/automatic_mask_generator_example.ipynb


import numpy as np
import torch
import matplotlib.pyplot as plt
import cv2

def show_anns(anns):
    if len(anns)==0:
        return
    sorted_anns=sorted(anns, key=(lambda x: x['area']), reverse=True)
    ax=plt.gca()
    ax.set_autoscale_on(False)

    img=np.ones((sorted_anns[0]['segmentation'].shape[0], sorted_anns[0]['segmentation'].shape[1], 4))
    img[:, :, 3]=0
    for ann in sorted_anns:
        m=ann['segmentation']
        color_mask=np.concatenate([np.random.random(3), [0.35]])
        img[m]=color_mask
    ax.imshow(img)

image=cv2.imread('D:/PyTest/kkk/dog.jpg')
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

# plt.figure(figsize=(20,20))
# plt.imshow(image)
# plt.axis('off')
# plt.show()
# plt.axis('off')

import sys
sys.path.append("..")
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor

sam_checkpoint='D:/PyTest/kkk/sam_vit_h_4b8939.pth'
model_type="vit_h"

# device = "cuda"

sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
# sam.to(device=device)

mask_generator = SamAutomaticMaskGenerator(sam)

masks=mask_generator.generate(image)

print(len(masks))
# print(masks[0].keys())

# plt.figure(figsize=(20, 20))
# plt.imshow(image)
# show_anns(masks)
# plt.axis('off')
# plt.show()

mask_generator_2 = SamAutomaticMaskGenerator(
    model=sam,
    points_per_side=32,
    pred_iou_thresh=0.86,
    stability_score_thresh=0.92,
    crop_n_layers=1,
    crop_n_points_downscale_factor=2,
    min_mask_region_area=100,
)

mask2=mask_generator_2.generate(image)
print(len(mask2))

# plt.figure(figsize=(20, 20))
# plt.imshow(image)
# show_anns(mask2)
# plt.axis('off')
# plt.show()

Thursday, February 1, 2024

ML: Deep Learning for Image Segmentation with TensorFlow

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# https://www.analyticsvidhya.com/blog/2023/04/deep-learning-for-image-segmentation-with-tensorflow/#

import cv2
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import matplotlib as mpl
from tqdm import tqdm
from sklearn.model_selection import train_test_split


# a list to collect paths of 1000 images
image_path=[]
for root, dirs, files in os.walk('D:/PyTest/kkk/png_images'):
    # iterate over 1000 images
    for file in files:
        path=os.path.join(root, file)
        image_path.append(path)
print(len(image_path))

# a list to collect paths of 1000 masks
mask_path=[]
for root, dirs, files in os.walk('D:/PyTest/kkk/png_masks'):
    # iterate over 1000 masks
    for file in files:
        # obtain the path
        path=os.path.join(root, file)
        # add path to the list
        mask_path.append(path)
print(len(mask_path))

# Create a list to store images
images=[]
for path in tqdm(image_path):
    # read file
    file=tf.io.read_file(path)
    # decode png file into a tensor
    image=tf.image.decode_png(file, channels=3, dtype=tf.uint8)
    # append to the list
    images.append(image)


# Create a list to store masks
masks=[]
for path in tqdm(mask_path):
    file=tf.io.read_file(path)
    # decode png file into a tensor
    mask=tf.image.decode_png(file, channels=1, dtype=tf.uint8)
    masks.append(mask)



def resize_image(image):
    image=tf.cast(image, tf.float32)
    image=image/255.0
    # resize image
    image=tf.image.resize(image, (128, 128))
    return image

def resize_mask(mask):
    mask=tf.image.resize(mask, (128, 128))
    mask=tf.cast(mask, tf.uint8)
    return mask

X=[resize_image(i) for i in images]
y=[resize_mask(m) for m in masks]
print(len(X))
print(len(y))


# split data into 80/20 ratio
train_X, val_X, train_y, val_y=train_test_split(X, y,
                                                test_size=0.2, random_state=0)
# Develop tf Dataset objects
train_X=tf.data.Dataset.from_tensor_slices(train_X)
val_X=tf.data.Dataset.from_tensor_slices(val_X)

train_y=tf.data.Dataset.from_tensor_slices(train_y)
val_y=tf.data.Dataset.from_tensor_slices(val_y)

# verify the shapes and data types
train_X.element_spec, train_y.element_spec, val_X.element_spec, val_y.element_spec

# adjust brightness of image
# don't alter in mask
def brightness(img, mask):
    img=tf.image.adjust_brightness(img, 0.1)
    return img, mask

def gamma(img, mask):
    img=tf.image.adjust_gamma(img, 0.1)
    return img, mask

def hue(img, mask):
    img=tf.image.adjust_hue(img, -0.1)
    return img, mask

def crop(img, mask):
    img=tf.image.central_crop(img, 0.7)
    img=tf.image.resize(img, (128,128))
    mask=tf.image.central_crop(mask, 0.7)
    mask=tf.image.resize(mask, (128,128))
    # cast to integers as they are class numbers
    mask=tf.cast(mask, tf.uint8)
    return img, mask

def flip_hori(img, mask):
    img=tf.image.flip_left_right(img)
    mask=tf.image.flip_up_down(mask)
    return img, mask

def flip_vert(img, mask):
    img=tf.image.flip_up_down(img)
    mask=tf.image.flip_up_down(mask)
    return img, mask

# rotate both image and mask identically
def rotate(img, mask):
    img=tf.image.rot90(img)
    mask=tf.image.rot90(mask)
    return img,mask

# zip images and masks
train=tf.data.Dataset.zip((train_X, train_y))
val=tf.data.Dataset.zip((val_X, val_y))


# perform augmentation on train data only
a=train.map(brightness)
b=train.map(gamma)
c=train.map(hue)
d=train.map(crop)
e=train.map(flip_hori)
f=train.map(flip_vert)
g=train.map(rotate)

# concatenate every new augmented sets
train=train.concatenate(a)
train=train.concatenate(b)
train=train.concatenate(c)
train=train.concatenate(d)
train=train.concatenate(e)
train=train.concatenate(f)

# Setting the batch size
BATCH=64

AT=tf.data.AUTOTUNE

# Buffer size
BUFFER=1000

STEPS_PER_EPOCH=800//BATCH
VALIDATION_STEPS=200//BATCH

train=train.cache().shuffle(BUFFER).batch(BATCH).repeat()
train=train.prefetch(buffer_size=AT)
val=val.batch(BATCH)


# Use pre-trained DenseNet21 without head
base=keras.applications.DenseNet121(input_shape=[128, 128, 3],
                                    include_top=False, weights='imagenet')

skip_names=[
    'conv1_relu',
    'pool2_relu',
    'pool3_relu',
    'pool4_relu',
    'relu'
]


skip_outputs=[base.get_layer(name).output for name in skip_names]
# Building the downstack with the above layers.
# We use the pre-trained model as much, without any fine-tuning
downstack=keras.Model(inputs=base.input, outputs=skip_outputs)
# freeze the downstack layers
downstack.trainable=False


from tensorflow_examples.models.pix2pix import pix2pix
upstack=[
    pix2pix.upsample(512, 3),
    pix2pix.upsample(256, 3),
    pix2pix.upsample(128, 3),
    pix2pix.upsample(64, 3)
]

# define the input layer
inputs=keras.layers.Input(shape=[128, 128, 3])
# downsample
down=downstack(inputs)
out=down[-1]

# prepare skip connection
skips=reversed(down[:-1])

# upsample with skip-connections
for up,skip in zip(upstack, skips):
    out=up(out)
    out=keras.layers.Concatenate()([out,skip])


# define the final transpose conv layer
out=keras.layers.Conv2DTranspose(
    59, 3, strides=2, padding='same',
)(out)

unet=keras.Model(inputs=inputs, outputs=out)

def Compile_Model():
    unet.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                 optimizer=keras.optimizers.RMSprop(learning_rate=0.001),
                 metrics=['accuracy'])

Compile_Model()

# training and fine-tuning
hist_1=unet.fit(
    train,
    validation_data=val,
    steps_per_epoch=STEPS_PER_EPOCH,
    validation_steps=VALIDATION_STEPS,
    epochs=20,
    verbose=2
)

# select a validation data batch
img, mask=next(iter(val))
# make prediction
pred=unet.predict(img)
plt.figure(figsize=(20,28))

NORM = mpl.colors.Normalize(vmin=0, vmax=58)


k=0
for i in pred:
    plt.subplot(4, 3, 1+k*3)
    i=tf.argmax(i, axis=-1)
    plt.imshow(i, cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Prediction')

    # plot the ground truth mask
    plt.subplot(4, 3, 2+k*3)
    plt.imshow(mask[k], cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Ground Truth')

    # plot the actual image
    plt.subplot(4, 3, 3+k*3)
    plt.imshow(img[k])
    plt.axis('off')
    plt.title('Actual Image')
    k=k+1
    if k==4:
        break
plt.suptitle('Prediction After 20 Epochs (No Fine-tuning)', color='red',
             size=20)
# plt.show()


downstack.trainable=True
# compile again
Compile_Model()
# train from epoch 20 to 40
hist_2=unet.fit(
    train,
    validation_data=val,
    steps_per_epoch=STEPS_PER_EPOCH,
    epochs=40,
    initial_epoch=20,
    verbose=2
)

# select a validation data batch
img, mask=next(iter(val))
# make prediction
pred=unet.predict(img)
plt.figure(figsize=(20, 30))

k=0
for i in pred:
    plt.subplot(4, 3, 1+k*3)
    i=tf.argmax(i, axis=-1)
    plt.imshow(i, cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Prediction')

    plt.subplot(4, 3, 2+k*3)
    plt.imshow(mask[k], cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Ground Truth')

    plt.subplot(4, 3, 3+k*3)
    plt.imshow(img[k])
    plt.axis('off')
    plt.title('Actual Image')
    k=k+1

    if k==4: break

plt.suptitle('Predition After 40 Epochs (By Fine-tuning from 21th Epoch)', color='red', size=20)
plt.show()

Wednesday, January 31, 2024

ML: SVM vs. Random Forest for image segmentation

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# https://github.com/bnsreenu/python_for_microscopists/blob/master/068b-ML_06_04_TRAIN_ML_segmentation_All_filters_RForest_SVM.py


import numpy as np
import cv2
import pandas as pd

img=cv2.imread("D://Mask//IMG_0686.JPG")
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

img2=img.reshape(-1)
df=pd.DataFrame()
df['Original Image']=img2

# Generate Gabor features
# To count numbers up in order to give Gabor features a label in the data frame
num=1
kernels=[]

for theta in range(2):  # Define number of thetas
    theta=theta/4.*np.pi
    for sigma in (1, 3):
        for lamda in np.arange(0, np.pi, np.pi/4):
            for gamma in (0.05, 0.5):
                gabor_label='Gabor'+str(num)
                ksize=9
                kernel=cv2.getGaborKernel((ksize, ksize),sigma,theta,lamda,gamma, 0, ktype=cv2.CV_32F)
                kernels.append(kernel)
                fimg=cv2.filter2D(img2, cv2.CV_8UC3, kernel)
                filtered_img=fimg.reshape(-1)
                df[gabor_label]=filtered_img
                print(gabor_label, ': theta=', theta, ': sigma, ', sigma, ': lamda=', lamda, ':gamma=', gamma)
                num=num+1


# Canny Edge
edges=cv2.Canny(img, 100, 200)  # Image, min and max values
edges1=edges.reshape(-1)
df['Canny Edge']=edges1

from skimage.filters import roberts, sobel, scharr, prewitt


# Robert Edge
edge_roberts=roberts(img)
edge_roberts1=edge_roberts.reshape(-1)
df['Roberts']=edge_roberts1


# Sobel
edge_sobel=sobel(img)
edge_sobel1=edge_sobel.reshape(-1)
df['Sobel']=edge_sobel1

# Scharr
edge_scharr=scharr(img)
edge_scharr1=edge_scharr.reshape(-1)
df['Scharr']=edge_scharr1

# Prewitt
edge_prewitt=prewitt(img)
edge_prewitt1=edge_prewitt.reshape(-1)
df['Prewitt']=edge_prewitt1

# Gaussian with sigma=3
from scipy import ndimage as nd
gaussian_img=nd.gaussian_filter(img, sigma=3)
gaussian_img1=gaussian_img.reshape(-1)
df['Gaussian s3']=gaussian_img1

# Gaussian with sigma=7
gaussian_img2=nd.gaussian_filter(img, sigma=7)
gaussian_img3=gaussian_img2.reshape(-1)
df['Gaussian s7']=gaussian_img3

# Median with sigma=3
median_img=nd.median_filter(img, size=3)
median_img1=median_img.reshape(-1)
df['Median s3']=median_img1


# Variance with size=3
variance_img=nd.generic_filter(img, np.var, size=3)
variance_img1=variance_img.reshape(-1)
df['Variance s3']=variance_img1

# Now, add a column in the data frame for the labels
# For this, we need to import the labeled image
labeled_img=cv2.imread("D://Mask//mask.tif")
# Remember that you can load an image with partial labels
# But, drop the rows with unlabeled data

labeled_img=cv2.cvtColor(labeled_img, cv2.COLOR_BGR2GRAY)
labeled_img1=labeled_img.reshape(-1)
df['Labels']=labeled_img1

print(df.head())



# Define the dependent variable that needs to be predicted (labels)
Y=df["Labels"].values
# Define the independent variables
X=df.drop(labels=["Labels"], axis=1)

# Split data into train and test to verify accuracy after fitting the model
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=20)

# Import the model we are using
# RandomForestRegressor is for regression type of problems
# For classification we use RandomForestClassifier
# Both yield similar results except for regressor the result is float
# and for classifier it is an integer

from sklearn.ensemble import RandomForestClassifier
model=RandomForestClassifier(n_estimators=100, random_state=42)


# Train the model on training data
model.fit(X_train, y_train)


# Testing the model by predicting on test data
# and Calculate the accuracy score
# First test predication on the training data itself. Should be good
prediction_test_train=model.predict(X_train)

# Test prediction on testing data
prediction_test=model.predict(X_test)


# Let us check the accuracy on test data
from sklearn import metrics

# First check the accuracy on training data. This will be higher than test data predication accuracy
print("Accuracy on training data= ", metrics.accuracy_score(y_train, prediction_test_train))
print("Accuracy= ", metrics.accuracy_score(y_test, prediction_test))