I'm doing a tutorial on tensorflow, and I'm finally starting to understand it, so this time I'm going to use tensorflow for Leaf Classification.
By the way, the ** 192 features that I somehow understood in the previous English paper are not used and are treated as a 99-class classification problem for 990 images **.
I used ** OpenCV ** for preprocessing.
Read.py
import cv2
import matplotlib.pyplot as plt
images = []
for i in range(1,1585):
image = cv2.imread("images/%d.jpg "%i)
grayed = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
images.append(grayed)
First, read each image from the "image" folder in the same folder while grayscale it, and store it in the list called images.
Next, let's try the pre-processing of the previous article. The first one in images is originally almost square, so let's take a look at images [1] here. By the way, images [1] is this.
You can see that it has an elongated shape on the side.
Pretreatment 1.py
plt.imshow(cv2.resize(images[1], (100, 100)), "gray")
plt.show()
After all, ** the feature of slenderness has disappeared **, so it seems not to be good.
Pretreatment 2.py
end_size = 100 #Final vertical and horizontal size
max_ = np.maximum(images[1].shape[0], images[1].shape[1]) #Max the larger one in the vertical and horizontal directions_Assigned to the variable.
scale = end_size / max_
height, width = images[1].shape
size = (int(width*scale), int(height*scale)) #Set the larger one to 100 while keeping the scale.
rescale_image = cv2.resize(images[1], size, interpolation=cv2.INTER_CUBIC) #Resize.
height, width = rescale_image.shape #Then try again.
#If the width is larger,[(Width-height)/2,width]It is a process to make a zero matrix of and add it up and down.
#This will keep the image in the middle.
if width > height:
z_pad = np.zeros([int((width-height)/2), width], dtype=np.uint8)
end_image = np.vstack((z_pad, rescale_image, z_pad))
else:
z_pad = np.zeros([height, int((height - width)/2)], dtype=np.uint8)
end_image = np.hstack((z_pad, rescale_image, z_pad))
#If the width-height value is odd, it will be 99, so it is resized to 100.
end_image = cv2.resize(end_image, (100, 100))
print(end_image.shape)
plt.imshow(end_image, "gray")
Apparently it was successful.
By the way, with some changes
Pretreatment 2_2.py
if width > height:
z_pad = np.zeros([width-height, width], dtype=np.uint8)
attempt_image = np.vstack((z_pad, rescale_image))
M = np.float32([[1,0,0],[0,1,-(width-height)/2]])
end_image = cv2.warpAffine(attempt_image, M,(end_size,end_size))
else:
z_pad = np.zeros([height, height - width], dtype=np.uint8)
attempt_image = np.hstack((z_pad, rescale_image))
M = np.float32([[1,0,-(height-width)/2],[0,1,0]])
end_image = cv2.warpAffine(attempt_image, M,(end_size,end_size))
You can also get the same image by doing. Here first
After creating such an image (attempt_image), it is pulled up to the center by moving in parallel.
Apply this to all and the pre-processing is over.
Another method is to create a 100x100 numpy array filled with 0s and then place an image with a scale changed to $ 100 x K (k ≤ 100) $.
The implementation version is likely to be long again, so I will divide it once.