Rococo-W 2021-03-19 21:09 采纳率: 100%
浏览 54
已结题

cv2对图像进行反向处理,显示高度超纲怎么办?axis 0

源代码:

import cv2 as cv
import numpy as num

def access_pixels(image):
    print(image.shape)
    height=image.shape[0]
    width=image.shape[1]
    channels=image.shape[2]
    print('height:%s width:%s channels:%s'%(height,width,channels))
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                pv=image[height,width,channels]
                image[row,col,c]= 255 - pv
    cv.imshow('pixels_demo',image)

src=cv.imread('D:\girl.jpg')
src1=cv.resize(src,(1,200),)
cv.namedWindow('input image',cv.WINDOW_AUTOSIZE)
cv.imshow('input image',src1)
access_pixels(src1)
cv.waitKey(0)
cv.destroyAllWindows()

运行后,出现的问题:

C:\ProgramData\Anaconda3\python.exe D:/pythonProject3/main.py
(200, 1, 3)
height:200 width:1 channels:3
Traceback (most recent call last):
  File "D:/pythonProject3/main.py", line 21, in <module>
    access_pixels(src1)
  File "D:/pythonProject3/main.py", line 13, in access_pixels
    pv=image[height,width,channels]
IndexError: index 200 is out of bounds for axis 0 with size 200

Process finished with exit code 1

 

即IndexError,200这个数值怎么调整都不行。

  • 写回答

2条回答 默认 最新

  • 对象被抛出 2021-03-19 21:33
    关注

    如果你想修改像素值为原来的与255的差, 那么13行应该改成pv = img[row, col, c]

    不过提醒一下, 三个for循环速度非常慢, 而且也影响观感

    广播能很好的解决这个机制, 整个函数可以写成

    def access_pixels(image):
        image = 255-image
        cv.imshow('pixels_demo',image)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月29日
  • 已采纳回答 9月21日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分