Because after the video project to use clustering, before been achieved with ENVI, now wants to learn under python.
Learning this one: a small cluster project
import cv2
import matplotlib.pyplot as plt
import numpy as np
====== ====== preparations
The original image path
imPath = 'C:\Users\lp\Desktop\photo.jpg'
Read image
origin = cv2.imread(imPath)
Converts the image to grayscale
origin = cv2.cvtColor(origin, cv2.COLOR_BGR2GRAY)
Image size
H, W = origin.shape
Print ( "the original image size is: {} * {}". format (h, w))
Set the zoom factor, the greater the degree of scaling coefficient, the stronger
alpha = int (input ( "\ n Enter scaling factor:"))
Image scaling
int = newH (H / Alpha)
neww = int (W / Alpha)
Print ( "\ n After scaling the image size: {} * {}." the format (newH, neww))
Origin = cv2.resize (Origin, (neww , newH))
Settings are divided into several categories
numOfClass = int (input ( "\ n color classification number:"))
Sets the maximum number of iterations
roundForLoop = int (input ( "\ n defines the maximum number of iterations:"))
Print ()
====== ====== cluster
print ( "---- ---- start clustering")
Aliquots maximum and minimum intervals to initialize the center points numOfClass
= origin.max valueArange () - origin.min ()
keyValueList = [] # numOfClass for storing values of the center point
for i in range(1, numOfClass + 1):
keyValueList.append(valueArange / numOfClass * i / 2)
Cluster update keyValueList
flagMatrix = np.zeros((newH, newW))
for r in range(roundForLoop):
for row in range(newH):
for col in range(newW):
temp = []
for i in range(numOfClass):
temp.append(np.abs(origin[row, col] - keyValueList[i]))
# 获得该像素点最近的类
index = temp.index(min(temp))
# 存入flag矩阵
flagMatrix[row, col] = index
# 更新keyValueList
temp = np.zeros(numOfClass)
ct = np.zeros(numOfClass)
for row in range(newH):
for col in range(newW):
temp[int(flagMatrix[row, col])] += origin[row, col]
ct[int(flagMatrix[row, col])] += 1
for i in range(numOfClass):
keyValueList[i] = temp[i] / ct[i]
print("完成 : {} / {}".format(r + 1, roundForLoop))
The display image to be processed scaled
plt.subplot(121)
plt.imshow(origin)
plt.title("Origin Image")
Show clustering results
plt.subplot(122)
plt.imshow(flagMatrix)
plt.title("Result1 Image\nRound = {}\nclass = {}".format(roundForLoop, numOfClass))
plt.show()
Iteration:
1, clustering is an iterative process through each pixel, and calculating the difference between each pixel of each element are keyValueList, the smaller the absolute value of the difference, indicating that the closer to the pixel from the class . Ordered to return to this element from its nearest class.
2. After traversing all the pixels, the pixels required for each category the average value, and to update the value of the element corresponding to keyValueList, and complete an iteration. keyValueList storage space of a length classification number, from low to high whose elements represent the mean of each class of image pixels.
3, will eventually get a picture of the same size flag matrix, each element of the matrix corresponds to a pixel location in the original category.
4, the flag output matrix, is obtained classification results obtained by the clustering.