opencv视频处理:cv2.HoughCircles出现问题

时间:2020-02-12 09:02:02

标签: python opencv video-processing

当我在相机上使用cv2.HoughCircles时遇到问题,因为它永远找不到圆。

这是我的代码:

import pyrealsense2 as rs
import numpy as np
import cv2

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
valeur_image=0

# Start streaming
pipeline.start(config)

try:
    while True:

        # Wait for a coherent pair of frames: color
        frames = pipeline.wait_for_frames()
        color_frame = frames.get_color_frame()
        if not color_frame:
            continue
        valeur_image=valeur_image+1
        if valeur_image>20:

            # Convert images to numpy arrays
            color_image = np.asanyarray(color_frame.get_data())

            # Load an color image in grayscale

            hsv= cv2.cvtColor(color_image,cv2.COLOR_BGR2HSV);
            lower_red=np.array([169,100,100])
            upper_red=np.array([189,255,255])

            mask=cv2.inRange(hsv,lower_red,upper_red)
            image_cercle=cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 50,  50, 35, 1, 1)

            print(image_cercle)
            while True:

                image_cercle = np.uint16(np.around(image_cercle)) 

                for pt in image_cercle[0,0:2]:

                    #centre_x,centre_y,rayon=pt[0],pt[1],pt[2]
                    cv2.circle(color_image,(pt[0],pt[1]),pt[2],(255,0,255),4)
                    cv2.circle(color_image,(pt[0],pt[1]),1,(255,0,0),3)
                    # show image
                    cv2.namedWindow('image mask ', cv2.WINDOW_AUTOSIZE)
                    cv2.imshow('image mask ', mask)
                    cv2.namedWindow('color image', cv2.WINDOW_AUTOSIZE)
                    cv2.imshow('color image', color_image)
                    cv2.waitKey(1)


finally:

    #stop streaming
    pipeline.stop()

这是我得到的错误:

 cv2.circle(color_image,(pt[0],pt[1]),pt[2],(255,0,255),4)
IndexError: invalid index to scalar variable.

我尝试与image一起使用,并且效果很好,这就是image_cercle中的功能:

[[[453.5 223.5 103.2]
  [143.5 226.5 122.8]
  [149.5 246.5 141.6]
  ...
  [619.5 102.5  97.2]
  [581.5 278.5  42.2]
  [587.5 168.5  43. ]]]

但是在这里用相机我能得到什么:

[[50.]
 [ 0.]
 [ 0.]
 [ 0.]]

您知道为什么它不起作用吗?谢谢

0 个答案:

没有答案