使用numpy.array访问图像数据


实现图片左上角设置为白像素:
import cv2 as cv
import numpy as np
img = cv.imread('D:\\Pycharm code\\venv\\src\\MyPic.png')
img[0, 0] = [255, 255, 255]
cv.imshow('image', img)
cv.waitKey(0)

用numpy.array的itemset方法操作单个像素点:

import cv2 as cv
import numpy as np
import os

img = cv.imread('D:\\Pycharm code\\venv\\src\\MyPic.png')
print(img.item(150, 120, 0))
img.itemset((150, 120, 0), 255)  #操作单个像素点
print(img.item(150, 120, 0))
 
  

操作通道(此处是将指定通道(B、G或R)的所有值置为0):

import cv2 as cv
import numpy as np
import os

img = cv.imread('D:\\Pycharm code\\venv\\src\\MyPic.png')
img[:, :, 1] = 0  #图像所有的G(绿色值)被设置为0,该图像将完全没有绿色
cv.imshow('image', img)
cv.waitKey(0)
 
  

实现将一块区域复制到另一块区域:

import cv2 as cv
import numpy as np
import os

img = cv.imread('D:\\Pycharm code\\venv\\src\\MyPic.png')
#感兴趣区域(Region Of Interest ROImy_roi = img[0:100, 0:100]
img[300:400, 300:400] = my_roi
cv.imshow('image', img)
cv.waitKey(0)
#实现将一块区域复制到另一块区域

获取图片信息:

import cv2 as cv
import numpy as np
import os

img = cv.imread('D:\\Pycharm code\\venv\\src\\MyPic.png')
print(img.shape)
print(img.size)
print(img.dtype)
 
  

shape返回包含宽度高度和通道数(如果图像是彩色的)的数组,若图像是单色或灰度的,将不包括通道值

size指图像像素的大小

datatype指图像的数据类型

你可能感兴趣的:(OpenCV)