Python 中的 OpenCV - 操纵像素
OpenCV in Python - Manipulating pixels
我正在使用 python 2.7 和 OpenCV 将图像设置为全白像素,但它不起作用。
这是我的代码:
import cv2
import numpy as np
image = cv2.imread("strawberry.jpg") #Load image
imageWidth = image.shape[1] #Get image width
imageHeight = image.shape[0] #Get image height
xPos = 0
yPos = 0
while xPos < imageWidth: #Loop through rows
while yPos < imageHeight: #Loop through collumns
image.itemset((xPos, yPos, 0), 255) #Set B to 255
image.itemset((xPos, yPos, 1), 255) #Set G to 255
image.itemset((xPos, yPos, 2), 255) #Set R to 255
yPos = yPos + 1 #Increment Y position by 1
xPos = xPos + 1 #Increment X position by 1
cv2.imwrite("result.bmp", image) #Write image to file
print "Done"
我使用 numpy 设置图像的像素 - 但 result.bmp 是原始图像的精确副本。
我做错了什么?
编辑:
我知道遍历像素是个坏主意,但我的代码中没有发挥作用的部分是什么?
规则一 opencv/python:从不 遍历像素,如果可以避免的话!
如果你想将所有像素设置为 (1,2,3),很简单:
image[::] = (1,2,3)
对于 'all white':
image[::] = (255,255,255)
除了@berak 提出的有效建议之外,如果这是您为学习要使用的库而编写的代码,那么您犯了 2 个错误:
- 您忘记在内部 while 循环后重置
yPos
行索引计数器
- 您调换了
itemset
中 xPos, yPos
的顺序。
我猜你的图片确实改变了,但它只在第一行,如果你不放大你可能看不到。如果你这样改变你的代码,它有效:
import cv2
import numpy as np
image = cv2.imread("testimage.jpg") #Load image
imageWidth = image.shape[1] #Get image width
imageHeight = image.shape[0] #Get image height
xPos, yPos = 0, 0
while xPos < imageWidth: #Loop through rows
while yPos < imageHeight: #Loop through collumns
image.itemset((yPos, xPos, 0), 255) #Set B to 255
image.itemset((yPos, xPos, 1), 255) #Set G to 255
image.itemset((yPos, xPos, 2), 255) #Set R to 255
yPos = yPos + 1 #Increment Y position by 1
yPos = 0
xPos = xPos + 1 #Increment X position by 1
cv2.imwrite("result.bmp", image) #Write image to file
请注意,如前所述,我也不建议逐个像素地迭代图像。
我正在使用 python 2.7 和 OpenCV 将图像设置为全白像素,但它不起作用。
这是我的代码:
import cv2
import numpy as np
image = cv2.imread("strawberry.jpg") #Load image
imageWidth = image.shape[1] #Get image width
imageHeight = image.shape[0] #Get image height
xPos = 0
yPos = 0
while xPos < imageWidth: #Loop through rows
while yPos < imageHeight: #Loop through collumns
image.itemset((xPos, yPos, 0), 255) #Set B to 255
image.itemset((xPos, yPos, 1), 255) #Set G to 255
image.itemset((xPos, yPos, 2), 255) #Set R to 255
yPos = yPos + 1 #Increment Y position by 1
xPos = xPos + 1 #Increment X position by 1
cv2.imwrite("result.bmp", image) #Write image to file
print "Done"
我使用 numpy 设置图像的像素 - 但 result.bmp 是原始图像的精确副本。
我做错了什么?
编辑:
我知道遍历像素是个坏主意,但我的代码中没有发挥作用的部分是什么?
规则一 opencv/python:从不 遍历像素,如果可以避免的话!
如果你想将所有像素设置为 (1,2,3),很简单:
image[::] = (1,2,3)
对于 'all white':
image[::] = (255,255,255)
除了@berak 提出的有效建议之外,如果这是您为学习要使用的库而编写的代码,那么您犯了 2 个错误:
- 您忘记在内部 while 循环后重置
yPos
行索引计数器 - 您调换了
itemset
中xPos, yPos
的顺序。
我猜你的图片确实改变了,但它只在第一行,如果你不放大你可能看不到。如果你这样改变你的代码,它有效:
import cv2
import numpy as np
image = cv2.imread("testimage.jpg") #Load image
imageWidth = image.shape[1] #Get image width
imageHeight = image.shape[0] #Get image height
xPos, yPos = 0, 0
while xPos < imageWidth: #Loop through rows
while yPos < imageHeight: #Loop through collumns
image.itemset((yPos, xPos, 0), 255) #Set B to 255
image.itemset((yPos, xPos, 1), 255) #Set G to 255
image.itemset((yPos, xPos, 2), 255) #Set R to 255
yPos = yPos + 1 #Increment Y position by 1
yPos = 0
xPos = xPos + 1 #Increment X position by 1
cv2.imwrite("result.bmp", image) #Write image to file
请注意,如前所述,我也不建议逐个像素地迭代图像。