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.
The calculation process completely excludes the background area. 5th.

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. Delete the soil and everything else from the image.
[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()