Python OpenCV像素操作
點(diǎn)擊上方“小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂”
重磅干貨,第一時(shí)間送達(dá)
本文轉(zhuǎn)自:opencv學(xué)堂
環(huán)境聲明 : Python3.6 + OpenCV3.3 + PyCharm IDE
import cv2 as cv;
import numpy as np;
對RGB圖像來說,在Python中第一個(gè)維度表示高度、第二個(gè)維度表示寬度、第三個(gè)維度是通道數(shù)目,可以通過下面的代碼獲取圖像三個(gè)維度的大小
print(image.shape)
print(image.size)
print(image.dtype)
直接從圖像中讀取,缺點(diǎn)是每次都需要訪問imread之后的Mat對象,進(jìn)行native操作,速度是個(gè)問題, 代碼實(shí)現(xiàn)如下:
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print(image.shape)
for row in range(height):
for col in range(width):
for c in range(channels):
level = image[row, col, c]
pv = level + 30
image[row, col, c] = (255 if pv>255 else pv)
首先通過Numpy把像素?cái)?shù)據(jù)讀到內(nèi)存中,在內(nèi)存中進(jìn)行高效循環(huán)訪問每個(gè)像素,修改之后,在賦值回去即可,代碼如下:
# read once
pixel_data = np.array(image, dtype = np.uint8);
# loop pixel by pixel
for row in range(height):
for col in range(width):
for c in range(channels):
level = pixel_data[row, col, c]
pixel_data[row, col, c] = 255 - level
# write once
image[ : : ] = pixel_data
案例演示 在Python語言中完成圖像的屬性讀取、像素讀取與操作、實(shí)現(xiàn)了圖像的顏色取反、亮度提升、灰度化、梯度化、操作。首先看一下效果:




完整的Python代碼實(shí)現(xiàn)如下:
import cv2 as cv;
import numpy as np;
def inverse(image):
print("read and write pixel by pixel")
print(image.shape)
print(image.size)
print(image.dtype)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
# read once
pixel_data = np.array(image, dtype = np.uint8);
# loop pixel by pixel
for row in range(height):
for col in range(width):
for c in range(channels):
level = pixel_data[row, col, c]
pixel_data[row, col, c] = 255 - level
# write once
image[ : : ] = pixel_data
cv.imshow("inverse image", image)
def brightness(image):
print("read and write pixel by pixel")
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print(image.shape)
for row in range(height):
for col in range(width):
for c in range(channels):
level = image[row, col, c]
pv = level + 30
image[row, col, c] = (255 if pv>255 else pv)
cv.imshow("inverse image", image);
def to_gray(image):
print("RGB to Gray Image")
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("channels : ", channels);
print(image.shape)
for row in range(height):
for col in range(width):
blue = image[row, col, 0]
green = image[row, col, 1];
red = image[row, col, 2]
gray = (0.2989*red + 0.5870*green + 0.1140*blue);
image[row, col, 0] = gray;
image[row, col, 1] = gray;
image[row, col, 2] = gray;
cv.imshow("gray image", image)
def gradient_image(image):
gx = cv.Sobel(image, cv.CV_32F, 1, 0)
gy = cv.Sobel(image, cv.CV_32F, 0, 1)
dst = cv.addWeighted(gx, 0.5, gy, 0.5, 50)
sobel_abs = np.absolute(dst)
sobel_8u = np.uint8(sobel_abs)
cv.imshow("gradient image", sobel_8u)
def clam(pv):
if pv > 255:
return 255
if pv < 0:
return 0;
return pv;
print("Image Pixel Operation Demo")
src = cv.imread("D:/vcprojects/images/demo.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
gradient_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
交流群
歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動(dòng)駕駛、計(jì)算攝影、檢測、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競賽等微信群(以后會(huì)逐漸細(xì)分),請掃描下面微信號(hào)加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會(huì)根據(jù)研究方向邀請進(jìn)入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會(huì)請出群,謝謝理解~

