将三个chanel图片转换为单通道图片

时间:2019-02-14 04:55:22

标签: python opencv hough-transform

使用霍夫变换检测实时视频帧中的圆圈。在霍夫圆圈功能中,第一个参数是作为单通道图像传递。我不知道该怎么做。连续显示该部分的错误。

代码正在使用:

import cv2
import cv2 as cv
from matplotlib import pyplot as plt
from scipy.ndimage import imread
import numpy as np

fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
cam_capture = cv2.VideoCapture(0)
cv2.destroyAllWindows()
upper_left = (50, 50)
bottom_right = (300, 300)

def sketch_transform(image):
##    print(image)
##    print("2")
##    #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
##    print(image)
##    print("3")
##    gray = 255-gray
##    ret, thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
##    image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
##    print(image)
##    print("4")
##    #image = cv2.drawContours(image, contours, -1,(0,0,255),3)
##    print(image)
##    print("4")
    circle_detect(image)

    return image

def circle_detect(image):
    #image1=str(image)
    #mage = cv2.imread(image, cv2.IMREAD_COLOR)
    print (image)
    print("k")
    image = cv2.cvtColor(image,cv2.COLOR_GRAY2RGB)
    circles = cv2.HoughCircles(image,cv2.HOUGH_GRADIENT,1,20,
                            param1=90,param2=30,minRadius=0,maxRadius=100)
    print(circles)

    circles = np.uint16(np.around(circles))
##    for i in circles[0,:]:
##        # draw the outer circle
##        cv2.circle(image,(i[0],i[1]),i[2],(0,255,0),2)
##        # draw the center of the circle
##        cv2.circle(image,(i[0],i[1]),2,(0,0,255),3)
    return circles




while True:
    _, image_frame = cam_capture.read()

    #Rectangle marker
    r = cv2.rectangle(image_frame, upper_left, bottom_right, (100, 50, 200), 5)
    rect_img = image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]]

    sketcher_rect = rect_img
    print(sketcher_rect)
    print("1")
    sketcher_rect = sketch_transform(sketcher_rect)

    #Conversion for 3 channels to put back on original image (streaming)
    #sketcher_rect_rgb = cv2.cvtColor(sketcher_rect, cv2.COLOR_GRAY2RGB)

    #Replacing the sketched image on Region of Interest
    image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]] = sketcher_rect
    cv2.imshow("Sketcher ROI", image_frame)
    if cv2.waitKey(1) == 13:
        break

cam_capture.release()
cv2.destroyAllWindows()

opencv的新手,我在过去四天一直在努力检测视频帧中的圆圈。作为图像的检测效果不错,但会给视频帧带来麻烦。

输出为:

    Traceback (most recent call last):
  File "/home/pi/Downloads/Pi-tracker-master/fgroi.py", line 62, in <module>
    sketcher_rect = sketch_transform(sketcher_rect)
  File "/home/pi/Downloads/Pi-tracker-master/fgroi.py", line 27, in sketch_transform
    circle_detect(image)
  File "/home/pi/Downloads/Pi-tracker-master/fgroi.py", line 36, in circle_detect
    image = cv2.cvtColor(image,cv2.COLOR_GRAY2RGB)
cv2.error: OpenCV(3.4.4) /home/pi/packaging/opencv-python/opencv/modules/imgproc/src/color.hpp:255: error: (-2:Unspecified error) in function 'cv::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::Set<1>; VDcn = cv::Set<3, 4>; VDepth = cv::Set<0, 2, 5>; cv::SizePolicy sizePolicy = (cv::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 3

0 个答案:

没有答案