python 简历中的值错误
value error in python cv
我正在尝试将单色 3 通道 QR 中的黑色图案更改为任何其他颜色,但我一直收到此错误
if k.any()==[0,0,0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我正在尝试的代码如下:
import cv2
import numpy as np
img = cv2.imread('C:/New folder (2)/new1.png')
rows,cols,bands = img.shape
print rows,cols,bands
for i in xrange (rows):
for j in xrange (cols):
k = img[i,j]
if k.any()==[0,0,0]:
img[i,j]==[255,255,255]
cv2.imshow('r',r)
cv2.waitKey(0)
我正在使用的图像附在下面。请帮助我摆脱困境。
虽然我已经更正了您 post 中的缩进错误,但您有两个小错误:
1.) np.any()
语法错误
2.) 分配新的像素值,你做了一个比较,对值没有影响。
附上带有注释的更正代码:
import cv2
import numpy as np
img = cv2.imread('image.png')
rows,cols,bands = img.shape
print rows,cols,bands
for i in xrange (rows):
for j in xrange (cols):
k = img[i,j]
#corrected syntax for comparison of multiple components
if np.all(k==[0,0,0]):
# Use = instead of == in next line, you do not want to compate
img[i,j]=[120,0,255]
cv2.imshow('r',img)
cv2.waitKey(0)
不过最后要说一句:我只是直接回答了你的问题,循环单个像素根本不是你应该选择的 OpenCV 解决方案。
虽然这看起来更复杂,但在 OpenCV 中执行此操作的更合适(更快)的方法如下:
import cv2
import numpy as np
img = cv2.imread('image.png')
rows,cols,bands = img.shape
print rows,cols,bands
# Create image with new colour for replacement
new_colour_image= np.zeros((rows,cols,3), np.uint8)
new_colour_image[:,:]= (255,0,0)
# Define range of color to be exchanged (in this case only one single color, but could be range of colours)
lower_limit = np.array([0,0,0])
upper_limit = np.array([0,0,0])
# Generate mask for the pixels to be exchanged
new_colour_mask = cv2.inRange(img, lower_limit, upper_limit)
# Generate mask for the pixels to be kept
old_image_mask=cv2.bitwise_not(new_colour_mask)
# Part of the image which is kept
img2= cv2.bitwise_and(img,img, old_image_mask)
# Part of the image which is replaced
new_colour_image=cv2.bitwise_and(new_colour_image,new_colour_image, new_colour_mask)
#Combination of the two parts
result=cv2.bitwise_or(img2, new_colour_image)
cv2.imshow('image',img)
cv2.imshow('mask',new_colour_mask)
cv2.imshow('r',result)
cv2.waitKey(0)
此处您想将数组与 [0,0,0] 进行比较,但是 k.any() 将 return 为布尔值而不是数组,因此无法与 [0, 0,0]
有一些方法可以解决这个问题。我想建议使用
if k.any() 或 k.all() 直接。都是return布尔值,any()检查是否有none-0,all()检查是否有0.
我正在尝试将单色 3 通道 QR 中的黑色图案更改为任何其他颜色,但我一直收到此错误
if k.any()==[0,0,0]: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我正在尝试的代码如下:
import cv2
import numpy as np
img = cv2.imread('C:/New folder (2)/new1.png')
rows,cols,bands = img.shape
print rows,cols,bands
for i in xrange (rows):
for j in xrange (cols):
k = img[i,j]
if k.any()==[0,0,0]:
img[i,j]==[255,255,255]
cv2.imshow('r',r)
cv2.waitKey(0)
我正在使用的图像附在下面。请帮助我摆脱困境。
虽然我已经更正了您 post 中的缩进错误,但您有两个小错误:
1.) np.any()
语法错误2.) 分配新的像素值,你做了一个比较,对值没有影响。
附上带有注释的更正代码:
import cv2
import numpy as np
img = cv2.imread('image.png')
rows,cols,bands = img.shape
print rows,cols,bands
for i in xrange (rows):
for j in xrange (cols):
k = img[i,j]
#corrected syntax for comparison of multiple components
if np.all(k==[0,0,0]):
# Use = instead of == in next line, you do not want to compate
img[i,j]=[120,0,255]
cv2.imshow('r',img)
cv2.waitKey(0)
不过最后要说一句:我只是直接回答了你的问题,循环单个像素根本不是你应该选择的 OpenCV 解决方案。
虽然这看起来更复杂,但在 OpenCV 中执行此操作的更合适(更快)的方法如下:
import cv2
import numpy as np
img = cv2.imread('image.png')
rows,cols,bands = img.shape
print rows,cols,bands
# Create image with new colour for replacement
new_colour_image= np.zeros((rows,cols,3), np.uint8)
new_colour_image[:,:]= (255,0,0)
# Define range of color to be exchanged (in this case only one single color, but could be range of colours)
lower_limit = np.array([0,0,0])
upper_limit = np.array([0,0,0])
# Generate mask for the pixels to be exchanged
new_colour_mask = cv2.inRange(img, lower_limit, upper_limit)
# Generate mask for the pixels to be kept
old_image_mask=cv2.bitwise_not(new_colour_mask)
# Part of the image which is kept
img2= cv2.bitwise_and(img,img, old_image_mask)
# Part of the image which is replaced
new_colour_image=cv2.bitwise_and(new_colour_image,new_colour_image, new_colour_mask)
#Combination of the two parts
result=cv2.bitwise_or(img2, new_colour_image)
cv2.imshow('image',img)
cv2.imshow('mask',new_colour_mask)
cv2.imshow('r',result)
cv2.waitKey(0)
此处您想将数组与 [0,0,0] 进行比较,但是 k.any() 将 return 为布尔值而不是数组,因此无法与 [0, 0,0]
有一些方法可以解决这个问题。我想建议使用
if k.any() 或 k.all() 直接。都是return布尔值,any()检查是否有none-0,all()检查是否有0.