OpenCV 获取并修改图中的像素点
生活随笔
收集整理的這篇文章主要介紹了
OpenCV 获取并修改图中的像素点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用for循環遍歷圖中的所有像素點
import cv2 as cv import numpy as np import matplotlib.pyplot as pltimage = np.zeros((256, 256, 3), np.uint8) # 創建一個 長256 寬 256的圖 plt.imshow(image[:, :, ::-1]) h, w, c = image.shapefor row in range(h): # 對圖中所有的像素點進行遍歷for col in range(w):b, g, r = image[row, col]print(b, g, r)plt.show()通過行和列的坐標值獲取該像素點的像素值,對于BGR圖像,它返回一個藍,綠,紅值的數組。對于灰度圖像,僅返回相應的強度值。
import numpy as np import matplotlib.pyplot as pltimg = np.zeros((256, 256, 3), np.uint8) # 創建一個 長256 寬 256的圖plt.imshow(img[:, :, ::-1]) # 獲取某個點的值 print(img[100, 100])# 僅獲取藍色通道的強度值 print(img[100, 100, 0])# 修改某個位置的像素值 img[100, 200] = (0, 0, 255) # 在y軸100,x軸200的位置 將像素點變為紅色 img[100, 100] = [255, 255, 255] # 在y軸100,x軸100的位置 將像素點變為白色 plt.imshow(img[:, :, ::-1]) plt.show() print(img[100, 200]) # 獲取某個點的值的內容 print(img[100, 200].dtype) # 8位無符號整型print(img.shape) # 獲取到圖像的行數,列數 print(img.dtype) # 查看圖像類型 print(img.size) # 查看像素數獲取圖像的屬性
圖像屬性包括行數,列數和通道數,圖形數據類型,像素數等。
| 形狀 | img.shape |
| 圖像大小 | img.dtype |
| 數據類型 | img.size |
總結
以上是生活随笔為你收集整理的OpenCV 获取并修改图中的像素点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV 绘制几何图形
- 下一篇: OpenCV 图像通道的拆分与合并