img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) center = (int((match[0] + match[2])/2), int((match[1] + match[3])/2)) # 指定圆心坐标 radius = int((((match[2] - match[0]) + (match[3] - match[1]))/4)) # 指定半径大小 result = cv2.circle(img, center, radius, color, -1) # 绘制圆形这样有什么问题
时间: 2023-10-31 08:04:19 浏览: 65
这段代码的问题在于将原始图像 `img` 直接转换为了 BGR 彩色图像,而原始图像类型可能并不是灰度图像。如果原始图像本身已经是彩色图像,则直接使用 `cv2.cvtColor()` 函数进行转换会导致图像颜色失真。因此,在进行颜色空间转换之前,应该先判断原始图像的类型,如果是灰度图像,则进行转换,否则直接使用原始图像。另外,对于圆心坐标和半径大小的计算,可以使用更简洁的表达式,如下所示:
```
center = ((match[0] + match[2]) // 2, (match[1] + match[3]) // 2)
radius = ((match[2] - match[0]) + (match[3] - match[1])) // 4
```
这样可以避免使用 `int()` 函数进行类型转换。最终的代码应该如下所示:
```
if len(img.shape) == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
center = ((match[0] + match[2]) // 2, (match[1] + match[3]) // 2)
radius = ((match[2] - match[0]) + (match[3] - match[1])) // 4
result = cv2.circle(img, center, radius, color, -1)
```
相关问题
Python cv 几个常用信息提取的功能,给出示例代码
Python cv(OpenCV)提供了许多信息提取的功能,以下是几个常用的示例代码:
1. 图像特征匹配
```
import cv2
img1 = cv2.imread('img1.jpg',0)
img2 = cv2.imread('img2.jpg',0)
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None,flags=2)
cv2.imshow("Image", img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. 人脸检测
```
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('img.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
3. 目标检测
```
import cv2
net = cv2.dnn.readNetFromDarknet("yolov4.cfg", "yolov4.weights")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0]-1] for i in net.getUnconnectedOutLayers()]
img = cv2.imread("person.jpg")
height, width, channels = img.shape
blob = cv2.dnn.blobFromImage(img, 0.00392, (416,416), (0,0,0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w/2)
y = int(center_y - h/2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
if i in indexes:
x,y,w,h = boxes[i]
label = str(classes[class_ids[i]])
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
cv2.putText(img, label, (x,y+30), font, 3, (0,255,0), 3)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文