• 【Python 常用脚本及命令系列 12.1 -- OpenCV 设置图片区域为某个颜色】


    cv2 设置图片区域颜色

    你可以使用numpy的切片操作来选择图像的一部分,并使用赋值操作来设置颜色。以下是一个简单的Python脚本示例:

    import cv2 
    import numpy as np 
    
    # 读取图像 
    img = cv2.imread('input.jpg') 
    
    # 设置图像的一部分颜色,例如设置左上角100x100区域为红色 
    img[0:100, 0:100] = [0, 0, 255] 
    
    # 保存修改后的图像 
    cv2.imwrite('modified.jpg', img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这个脚本中,我们首先读取了一张名为 ‘input.jpg’ 的图像,然后使用 numpy 的切片操作选取了图像左上角的 100x100 像素区域,然后使用赋值操作将这一部分的颜色设置为红色(在OpenCV中,颜色的表示顺序为BGR,所以红色表示为[0, 0, 255])。最后,我们将修改后的图像保存为’modified.jpg’。

    请注意,numpy的切片操作中,第一个索引是y坐标(垂直方向),第二个索引是x坐标(水平方向)。所以img[0:100, 0:100]选取的是图像左上角的区域。如果你想选取其他区域,可以调整切片操作的参数。例如,img[50:150, 50:150]会选取图像从(50, 50)开始的100x100像素区域。

    以设置图片右下角100x100 区域为白色为例
    要将图像的右下角 100x100 的区域设置为白色,你需要知道图像的宽度和高度,以便从正确的位置开始切片。
    以下是一个Python脚本示例:

    import cv2 
    import numpy as np 
    
    # 读取图像 
    img = cv2.imread('input.jpg') 
    
    # 获取图像的高度和宽度 
    height, width = img.shape[:2] 
    
    # 设置图像右下角的100x100区域为白色 
    img[height-100:height, width-100:width] = [255, 255, 255] 
    
    # 保存修改后的图像 
    cv2.imwrite('modified.jpg', img)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 我们首先读取了一张名为’input.jpg’的图像,然后获取了图像的高度和宽度。
    • 然后,我们使用numpy的切片操作选取了图像右下角的100x100像素区域,然后使用赋值操作将这一部分的颜色设置为白色(在OpenCV中,颜色的表示顺序为BGR,所以白色表示为[255, 255, 255])。
    • 最后,我们将修改后的图像保存为’modified.jpg’。

    请注意,如果图像的高度或宽度小于100 像素,你需要相应地调整切片操作的参数,否则会出现索引错误。

    动态输入高和宽

    import sys
    import cv2 # 导入包
    import numpy as np
    from PIL import Image
    
    num_args = len(sys.argv)
    print("The input length is:", num_args)
    if num_args != 3:
        print("Plese input the height and width")
        exit(1)
    
    print("type:", type(sys.argv))
    print("function name:", sys.argv[0])
    
    try:
        print("hight:", sys.argv[1])
        print("width:", sys.argv[2])
    except Exception as e:
        print("Input Error:", e)
    
    img = cv2.imread(r'test.jpg')#
    height, width = img.shape[:2]
    img[height - int(sys.argv[1]) : height, width - int(sys.argv[2]) : width] = [255, 255, 255]
    cv2.imwrite('modified.jpg', img)
    
    #read image
    img_grey = cv2.imread('modified.jpg', cv2.IMREAD_GRAYSCALE)
    
    
    # define a threshold, 128 is the middle of black and white in grey scale
    #thresh = 128
    thresh = 210
    
    # assign blue channel to zeros
    img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
    
    
    # 在窗口中显示图像
    cv2.imshow(r'Image', img_binary)
    
    # 最后还要写一句代码,这样就可以使窗口始终保持住
    cv2.waitKey(0)
    
    #最后别忘了释放窗口,养成良好习惯。
    cv2.destroyAllWindows()
    
    #save image
    cv2.imwrite('black-and-white.png',img_binary)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    Java-API简析_java.net.URL类(基于 Latest JDK)(浅析源码)
    Linxu系统(Centos 7)安装 DNS 服务
    【zabbix监控四】zabbix之监控tomcat服务报警
    ardupilot开发 --- 通信链路 篇
    【紧急整理】《信息资源管理》信息资源管理的标准与法规,第5章
    RabbitMQ 消息丢失 重复消费 集群部署
    Java 进阶多线程(一)
    华为云云耀云服务器L实例评测|企业项目最佳实践之docker部署及应用(七)
    学了labview怎么系统的编一个程序?
    ASP.NET实现的教研室管理系统(提供源码和报告)
  • 原文地址:https://blog.csdn.net/sinat_32960911/article/details/134399060