fuckfuckeveryday 2022-01-06 15:48 采纳率: 42.9%
浏览 29
已结题

opencv python截取图像部分区域,发现截取后的矩阵的列数为0

问题遇到的现象和发生背景

opencv python截取图像部分区域,发现截取后的矩阵的列数为0,截取的范围没有超过图像的范围。
并非是所有的都是0,当x+w超过450左右的时候就会出现这种情况。

问题相关代码,请勿粘贴截图
        for cnt in contours:
            x,y,w,h=cv2.boundingRect(cnt)
            tmp3=frame.copy()
            if x>0 and x+w<640 and w*h>300:
                c=c+1
                rest=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
                rest1=cv2.rectangle(opening,(x,y),(x+w,y+h),(255,255,255),1)
                rest2=cv2.rectangle(opening1,(x,y),(x+w,y+h),(255,255,255),1)
                gray = cv2.cvtColor(rest, cv2.COLOR_BGR2GRAY)
                img_cut = frame[x:(x + w), y:(y + h)]
                print("img_cut的shape是",np.shape(img_cut))
                print("w*h=",w*h)
                print("x:",x,"x+w:",x+w)
                print("w:",w,"h:",h)

                img_cut = frame[x:(x + w), y:(y + h)]
                print("img_cut的shape是",np.shape(img_cut))
                print("w*h=",w*h)
                print("x:",x,"x+w:",x+w)
                print("w:",w,"h:",h)

运行结果及报错内容

img_cut的shape是 (0, 103, 3)
w*h= 15759
x: 482 x+w: 635
w: 153 h: 103

img_cut的shape是 (0, 15, 3)
w*h= 390
x: 462 x+w: 488
w: 26 h: 15

img_cut的shape是 (0, 40, 3)
w*h= 2680
x: 390 x+w: 457
w: 67 h: 40

img_cut的shape是 (0, 50, 3)
w*h= 5150
x: 384 x+w: 487
w: 103 h: 50

还有一些不为零的:

img_cut的shape是 (16, 23, 3)
wh= 368
x: 193 x+w: 209
w: 16 h: 23
img_cut的shape是 (85, 32, 3)
w
h= 2720
x: 149 x+w: 234
w: 85 h: 32
img_cut的shape是 (72, 21, 3)
wh= 1512
x: 111 x+w: 183
w: 72 h: 21
img_cut的shape是 (204, 77, 3)
w
h= 15708
x: 133 x+w: 337
w: 204 h: 77

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 爱晚乏客游 2022-01-06 15:59
    关注

    frame[y1:y2,x1:x2],y在前,x在后

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月14日
  • 已采纳回答 1月6日
  • 创建了问题 1月6日

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏