Har*_*kar 14 python opencv image-processing pixels
我想知道如何遍历图像的所有像素.我试过这个:
import cv2
import numpy as np
x = np.random.randint(0,5,(500,500))
img = cv2.imread('D:\Project\Capture1.jpg',0)
p = img.shape
print p
rows,cols = img.shape
for i in range(rows):
for j in range(cols):
k = x[i,j]
print k
Run Code Online (Sandbox Code Playgroud)
它打印一组垂直数字,不是数组的形式.我也得到一个超出范围的数组异常.请建议一种方法.
Rox*_*nne 13
我不明白你的x变量的目的是什么.你不需要它.
只需使用:
img = cv2.imread('D:\Project\Capture1.jpg',0)
rows,cols = img.shape
for i in range(rows):
for j in range(cols):
k = img[i,j]
print k
Run Code Online (Sandbox Code Playgroud)
这将打印一组垂直数字.如果要修改像素的值,请使用img.itemset()
.http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html
如果要打印整个阵列,请使用 print(img)
Roh*_*nke 12
访问Python中的特定像素
import cv2
image = cv2.imread("sample.jpg")
pixel= image[200, 550]
print pixel
Run Code Online (Sandbox Code Playgroud)
输出: [73 89 102]
使用numpy数组时,使用数组索引访问会很慢。
您可以使用该item()
方法进行访问和itemset
执行更改。
例如
for i in range(0,img.shape[0]):
for j in range(0,img.shape[1]):
pixel = img.item(i, j)
print pixel
Run Code Online (Sandbox Code Playgroud)
import cv2
import numpy as np
imagename = "capure.jpg"
img = cv2.imread(imagename, 0) # 0 params, for gray image
height, width = img.shape[:2] # image height and width
print(img) # all image pixels value in array
print(img[10, 10]) # one pixel value in 10,10 coordinate
for y in range(height):
for x in range(width):
print(img[y,x], end = "\t")
print("\t")
Run Code Online (Sandbox Code Playgroud)
垂直数组是图像的 RGB(Reg、Green、Blue)通道值。如果您想要像素的单个值,您可能需要首先将图像转换为灰度。这实际上取决于您的应用程序以及您想要对图像执行的操作,转换为灰度只是一种方法。
转换为灰度
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Run Code Online (Sandbox Code Playgroud)
一些基本操作详见文档
归档时间: |
|
查看次数: |
54035 次 |
最近记录: |