20.2-20.3
import numpy as np
import cv2
import matplotlib.pyplot as plt
x = np.random.randint(0,100,(50,2))
x = np.float32(x)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, label, center = cv2.kmeans(x, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
print("距离:", ret)
print("标签:", np.reshape(label, -1))
print("分类中心点:\n", center)
A = x[label.ravel()==0]
B = x[label.ravel()==1]
plt.scatter(A[:,0], A[:,1], c = 'g', marker = 's')
plt.scatter(B[:,0], B[:,1], c = 'r', marker = 'o')
plt.scatter(center[0,0], center[0,1],s = 200,c = 'b', marker = 's')
plt.scatter(center[1,0], center[1,1],s = 200,c = 'b', marker = 'o')
plt.show()
import numpy as np
import cv2
img = cv2.imread('cat.jpg')
data = img.reshape((-1,3))
data = np.float32(data)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 2
ret, label, center = cv2.kmeans(data, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
print("距离:", ret)
print("标签:\n", label)
print("分类中心点:\n", center)
center = np.uint8(center)
res1 = center[label.flatten()]
res2 = res1.reshape((img.shape))
cv2.imshow("original", img)
cv2.imshow("result", res2)
cv2.waitKey()
cv2.destroyAllWindows()
列24.1
import numpy as np
import cv2
image = cv2.imread("tower.jpg")
config = "model/bvlc_googlenet.prototxt"
model = "model/bvlc_googlenet.caffemodel"
net = cv2.dnn.readNetFromCaffe(config, model)
blob = cv2.dnn.blobFromImage(image, 1, (244, 244), (104, 117, 123))
net.setInput(blob)
prob = net.forward()
classes = open("model/label.txt", "rt").read().strip().split("\n")
rowIndex = np.argsort(prob[0])[::-1][0]
result = "result:{},{:.0f}%".format(classes[rowIndex],prob[0][rowIndex]*100)
cv2.putText(image, result, (25, 45), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 2)
cv2.imshow("result", image)
cv2.waitKey()
cv2.destroyAllWindows()
24.2-24.3
import numpy as np
import cv2
image = cv2.imread("test2.jpg")
classes = open('coco.names', 'rt').read().strip().split("\n")
net = cv2.dnn.readNetFromDarknet("yolov3.cfg","yolov3.weights")
blob = cv2.dnn.blobFromImage(image, 1.0 / 255.0, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outInfo = net.getUnconnectedOutLayersNames()
outs = net.forward(outInfo)
resultIDS = []
boxes = []
confindences = []
(H, W) = image.shape[:2]
for out in outs:
for candidate in out:
scores = candidate[5:]
classID = np.argmax(scores)
confindence = scores[classID]
if confindence > 0.5:
box = candidate[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerX - (height / 2))
boxes.append([x, y, int(width), int(height)])
confindences.append(float(confindence))
resultIDS.append(classID)
indexes = cv2.dnn.NMSBoxes(boxes, confindences, 0.5, 0.4)
classesCOLORS = np.random.randint(0, 255, size = (len(classes), 3), dtype="uint8")
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
color = [int(c) for c in classesCOLORS[resultIDS[i]]]
cv2.rectangle(image, (x, y), (x+w, y+h), color, 2)
result = "{}: {:.0f}%".format(classes[resultIDS[i]], confindences[i]*100)
cv2.putText(image, result, (x, y+35), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
cv2.imshow('result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np import cv2 image = cv2.imread("test2.jpg") (H, W) = image.shape[:2] classes = open('object_detection_classes_pascal_voc.txt', 'rt').read().strip().split("\n") classesCOLORS = (np.random.uniform(0, 255, size=(len(classes), 3))) config = "MobileNetSSD_deploy.prototxt.txt" model = "MobileNetSSD_deploy.caffemodel" net = cv2.dnn.readNetFromCaffe(config, model) blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5) net.setInput(blob) outs = net.forward() print(outs.shape)
import cv2
import numpy as np
image = cv2.imread("a.jpg")
H, W = image.shape[:2]
classes = open("object_detection_classes_pascal_voc.txt",
"rt").read().strip().split("\n")
classesCOLORS = np.random.randint(0, 255, size=(len(classes), 3), dtype = "uint8")
classesCOLORS[0] = (0,0,0)
rowHeight = 30
colorChart = np.zeros((rowHeight * len(classesCOLORS), 200, 3), np.uint8)
for i in range(len(classes)):
row = colorChart[i * rowHeight:(i + 1) * rowHeight]
row[:,:] = classesCOLORS[i]
cv2.putText(row, classes[i], (0, rowHeight//2),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
cv2.imshow('colorChart', colorChart)
model = "fcn8s-heavy-pascal.caffemodel"
config = "fcn8s-heavy-pascal.prototxt"
net = cv2.dnn.readNet(model, config)
blob = cv2.dnn.blobFromImage(image, 1.0, (W, H), (0, 0, 0), False, crop=False)
net.setInput(blob)
score = net.forward()
classIDS = np.argmax(score[0], axis=0)
print(classIDS.shape)
mask = np.stack([classesCOLORS[index] for index in classIDS.flatten()])
mask = mask.reshape(H, W, 3)
result = cv2.addWeighted(image, 0.2, mask, 0.8, 0)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyWindow()
import cv2
import numpy as np
image = cv2.imread('e.jpg')
(H, W) = image.shape[:2]
background = np.zeros((H, W, 3), np.uint8)
background[:] = (100, 100, 0)
LABELS = open('object_detection_classes_coco.txt').read().strip().split('\n')
net = cv2.dnn.readNetFromTensorflow('dnn/frozen_inference_graph.pb',
'dnn/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt')
blob = cv2.dnn.blobFromImage(image, swapRB=True)
net.setInput(blob)
boxes, masks = net.forward(['detection_out_final', 'detection_masks'])
number = boxes.shape[2]
for i in range(number):
classID = int(boxes[0, 0, i,1])
confidence = boxes[0, 0, i, 2]
if confidence > 0.5:
box = boxes[0, 0, i, 3:7] * np.array([W, H, W, H])
(x1, y1, x2, y2) = box.astype('int')
box = background[y1: y2, x1: x2]
boxHeight = y2 - y1
boxWidth = x2 - x1
mask = masks[i, int(classID)]
mask = cv2.resize(mask, (boxWidth, boxHeight))
rst, mask = cv2.threshold(mask, 0.5, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(np.array(mask, np.uint8),
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
color = np.random.randint(0, 255, 3)
color = tuple([int(x) for x in color])
cv2.drawContours(box, contours, -1, color, -1)
msg = '{}:{:.0f} %'.format(LABELS[classID], confidence * 100)
cv2.putText(background, msg, (x1 + 50, y1 + 45),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
result = cv2.addWeighted(image, 0.2, background, 0.8, 0)
cv2.imshow('original', image)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
image = cv2.imread("tute.jpg")
H, W = image.shape[:2]
net = cv2.dnn.readNetFromTorch('model/eccv16/starry_night.t7')
blob = cv2.dnn.blobFromImage(image, 1.0, (W, H), (0, 0, 0), swapRB=False, crop=False)
net.setInput(blob)
out = net.forward()
print(out)
out = out.reshape(out.shape[1], out.shape[2], out.shape[3])
cv2.normalize(out, out, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX)
result = out.transpose(1, 2, 0)
cv2.imshow("original", image)
cv2.imshow('result', result)
cv2.waitKey()
cv2.destroyWindow()