Python+OpenCV 图像处理系列(4)—— 图像像素的读写、算术运算、逻辑运算及像素的统计
生活随笔
收集整理的這篇文章主要介紹了
Python+OpenCV 图像处理系列(4)—— 图像像素的读写、算术运算、逻辑运算及像素的统计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 像素的讀寫
可以根據像素的行和列的坐標獲取它的像素值。對 BGR 圖像而言,返回值為 B,G,R 的值。
- img.shape 可以獲取圖像的形狀。它的返回值是一個包含行數 h,列數 w,通道數 ch 的元組。注意:如果圖像是灰度圖,返回值僅有行數和列數。所以通過檢查這個返回值就可以知道加載的是灰度圖還是彩色圖。
- img.size 可以返回圖像的像素數目 (h * w * ch)
- img.dtype 返回的是圖像的數據類型:
import cv2image_name = "img/003.jpg"
img = cv2.imread(image_name)
cv2.imshow("origin", img)high, wide, channel = img.shape
print "high is {0}, wide is {1}, channel is {2}".format(high, wide, channel)
# high is 198, wide is 198, channel is 3for row in range(high):for col in range(wide):b, g, r = img[row, col]b = 255 - bg = 255 - gr = 255 - rimg[row, col] = b, g, r # 修改像素值cv2.imshow("output", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
顯示結果:
如果上面的難以理解,可以看下面的這些操作,希望能幫助讀者更好的理解。
>>> a = np.array([[[1, 2], [3,4], [5, 6]], [[11, 22], [33,44], [55, 66]]])
>>> a.shape
(2, 3, 2)
>>> a
array([[[ 1, 2],[ 3, 4],[ 5, 6]],[[11, 22],[33, 44],[55, 66]]])>>> a.ndim
3
>>> a.size
12
>>> a[0, 1]
array([3, 4])
>>> a[1, 2]
array([55, 66])
>>> h, w = a[1, 2]
>>> h,w
(55, 66)
2. 像素的算術運算
圖像像素也可以進行加、減、乘、除操作,但是必須要注意,進行算術運算操作的時,必須要保證兩幅圖像的大小、數據類型和通道數目必須相同。
- OpenCV 中的加法與 Numpy 的加法是有所不同的。OpenCV 的加法是一種飽和操作,而 Numpy 的加法是一種模操作。這種差別在你對兩幅圖像進行加法時會更加明顯。OpenCV 的結果會更好一點。所以我們盡量使用 OpenCV 中的函數。
使用示例如下:
import cv2
import numpy as npimg1 = cv2.imread("img/003.jpg")
img2 = cv2.imread("img/003_gray.jpg")cv2.imshow("img1", img1)
cv2.imshow("img2", img2)h, w, ch = img1.shapeadd_result = np.zeros(img1.shape, img1.dtype)
cv2.add(img1, img2, add_result)
cv2.imshow("add_result", add_result)sub_result = np.zeros(img1.shape, img1.dtype)
cv2.subtract(img1, img2, sub_result)
cv2.imshow("sub_result", sub_result)mul_result = np.zeros(img1.shape, img1.dtype)
cv2.multiply(img1, img2, mul_result)
cv2.imshow("mul_result", mul_result)div_result = np.zeros(img1.shape, img1.dtype)
cv2.divide(img1, img2, div_result)
cv2.imshow("div_result", div_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 像素的邏輯運算
這里包括的按位操作有:AND,OR,NOT,XOR 等。當我們提取圖像的一部分,選擇非矩形 ROI 時這些操作會很有用。下面的例子就是教給我們如何改變一幅圖的特定區域。
import cv2
import numpy as np# create img one
img1 = np.zeros(shape=(200, 200, 3), dtype=np.uint8)
img1[50:100, 50:100, 1] = 255
img1[50:100, 50:100, 2] = 255
cv2.imshow("img1", img1)# create img two
img2 = np.zeros(shape=(200, 200, 3), dtype=np.uint8)
img2[75:150, 75:150, 2] = 255
cv2.imshow("img2", img2)dst1 = cv2.bitwise_and(img1, img2)
dst2 = cv2.bitwise_or(img1, img2)
dst3 = cv2.bitwise_xor(img1, img2)cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.imshow("dst3", dst3)img4 = cv2.imread("img/003.jpg")
dst4 = cv2.bitwise_not(img4)cv2.imshow("dst4", dst4)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. 像素的統計
像素值統計涉及到的知識點有以下幾個
- 最小(min)
- 最大(max)
- 均值(mean)
- 標準方差(standard deviation)
OpenCV 相關的 API 知識點
- 最大最小值 minMaxLoc
- 計算均值與標準方差 meanStdDev
注意:像素值統計必須是單通道的圖片,所以 cv2.imread 時必須加上圖片的載入類型。
源碼中有如下提示:
'''
The function do not work with multi-channel arrays. If you need to find minimum or maximum
elements across all the channels, use Mat::reshape first to reinterpret the array as
single-channel.
'''
import cv2
import numpy as npimg = cv2.imread("img/003.jpg", cv2.IMREAD_GRAYSCALE)minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(img)
print minVal, maxVal, minLoc, maxLoc # 0.0 255.0 (128, 24) (120, 12)
means, stddev = cv2.meanStdDev(img)
print means, stddev # [[97.23278237]] [[45.80720813]]img[np.where(img < means)] = 0
img[np.where(img > means)] = 255cv2.imshow("binary", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
總結
以上是生活随笔為你收集整理的Python+OpenCV 图像处理系列(4)—— 图像像素的读写、算术运算、逻辑运算及像素的统计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python+OpenCV 图像处理系列
- 下一篇: Python+OpenCV 图像处理系列