zoukankan      html  css  js  c++  java
  • opencv对图片每个像素操作

    1.遍历图像对其每个像素取反

    import cv2 as cv
    import numpy as np
    
    img = cv.imread("d:/girl.jfif")
    cv.imshow("img", img)
    
    height = img.shape[0]
    width = img.shape[1]
    channels = img.shape[2]
    t1 = cv.getTickCount()
    for h in range(height):
        for w in range(width):
            for c in range(channels):
                img[h, w, c] = 255 - img[h, w, c]
    t2 = cv.getTickCount()
    t = (t2 - t1)/cv.getTickFrequency()*1000
    print("%f ms"%t)
    cv.imshow("img2", img)
    cv.waitKey(0)

    注意cv.getTickCount()的使用可以获得程序运行时间。

    t = (t2 - t1)/cv.getTickFrequency()*1000

    其中t的单位是毫秒

     顺便一提,可以使用cv.bitwise_not(image)来实现对图像取反,比上面的代码要快几千倍。事实上,还有bitwise_and, bitwise_or, bitwise_xor等位操作的函数

    2.构造一张蓝色的图

    '''
    构造一张蓝色的图
    '''
    import cv2 as cv
    import numpy as np
    
    img = np.zeros(shape=[400, 400, 3], dtype=np.uint8)
    img[:,:,0] = np.ones([400, 400])*255
    cv.imshow("img", img)
    cv.waitKey(0)

    三通道,bgr,所以只需要对第三维的索引0赋值。

    3.构造单通道灰度图

    '''
    构造一张灰色的图
    '''
    import cv2 as cv
    import numpy as np
    
    img = np.ones(shape=[400, 400, 1], dtype=np.uint8)
    height = img.shape[0]
    width = img.shape[1]
    channels = img.shape[2]
    for h in range(height):
        for w in range(width):
            for c in range(channels):
                img[h, w, c] = (h+w)%255
    cv.imshow("img", img)
    cv.waitKey(0)

    结果很漂亮惹,懒得截图了

  • 相关阅读:
    Step by step Dynamics CRM 2013安装
    SQL Server 2012 Managed Service Account
    Step by step SQL Server 2012的安装
    Step by step 活动目录中添加一个子域
    Step by step 如何创建一个新森林
    向活动目录中添加一个子域
    活动目录的信任关系
    RAID 概述
    DNS 正向查找与反向查找
    Microsoft Dynamics CRM 2013 and 2011 Update Rollups and Service Packs
  • 原文地址:https://www.cnblogs.com/loubin/p/12271412.html
Copyright © 2011-2022 走看看