索引器错误:索引1080超出大小为1080的轴0的界限

2024-04-20 14:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读python上的一个视频帧,并试图找到每个帧索引的RGB。我需要检测LED(设置一个开/关-红/黑的阈值),但我遇到了索引问题。你知道吗

我需要访问图像左下角的RGB值。你知道吗

# Check if camera opened successfully
if (video.isOpened()== False): 
  print("Error opening video stream or file")

  # Read until video is completed
while(video.isOpened()):
  # Capture frame-by-frame
  ret, frame = video.read()
  frame_read += 1

  if ret == True:

    # Display the resulting frame
    cv2.imshow('Frame',frame)

    height, width, channels = frame.shape

    #Accessing RGB pixel values    
    for x in range(round(width/2), width) :
     for y in range(0, round(height/2)) :
          print(frame[x,y,2], frame[x,y,0], frame[x,y,1], frame_read) #R,B,G Channel Value


    # Press Q on keyboard to exit
    if cv2.waitKey(25) & 0xFF == ord('q'):
      break

  # Break the loop
  else: 
    break

    video.release()
    cv2.destroyAllWindows()

我的错误在行打印上(帧[x,y,2],帧[x,y,0],帧[x,y,1],帧\读取) 索引器错误:索引1080超出大小为1080的轴0的界限


Tags: theinforreadifvideorangergb
1条回答
网友
1楼 · 发布于 2024-04-20 14:22:47

当您将帧切片为frame[x, y, 2]时,您忘记了frame的第一个切片始终是高度(正如您在height, width, channels = frame.shape中正确所做的那样),因此您在帧切片中引用的x的范围不能从0width(在您的示例中为1920),因为第一个切片(您的x)的范围是从0height(1080)。你知道吗

引用宽度的y也是如此(在您的例子中,范围从0到1920)。你知道吗

只需交换框架切片,就可以开始了:

print(frame[y,x,2], frame[y,x,0], frame[y,x,1], frame_read)

相关问题 更多 >