import cv
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import Image
cam = cv2.VideoCapture(0)
s,img1 = cam.read()
height, width, depth = img1.shape
print height, width
for i in range(0,height):
for j in range(0,width):
if (img1[i, j] <= [25,25,25]).all():
img1[i, j] = [255, 0, 128]
elif ((img1[i, j] > [25,25,25]).all() and (img1[i, j] <= [50,50,50]).all()):
img1[i,j] = [255, 255, 128]
elif ((img1[i, j] > [50,50,50]).all() and (img1[i, j] <= [75,75,75]).all()):
img1[i,j] = [255, 128, 0]
elif ((img1[i, j] > [75,75,75]).all() and (img1[i, j] <= [100,100,100]).all()):
img1[i,j] = [0, 255, 0]
elif ((img1[i, j] > [100,100,100]).all() and (img1[i, j] <= [125,125,125]).all()):
img1[i,j] = [68, 128, 251]
elif ((img1[i, j] > [125,125,125]).all() and (img1[i, j] <= [150,150,150]).all()):
img1[i,j] = [0, 255, 255]
elif ((img1[i, j] > [150,150,150]).all() and (img1[i, j] <= [175,175,175]).all()):
img1[i,j] = [0, 0, 255]
elif ((img1[i, j] > [175,175,175]).all() and (img1[i, j] <= [200,200,200]).all()):
img1[i,j] = [128, 128, 128]
elif ((img1[i, j] > [200,200,200]).all() and (img1[i, j] <= [225,225,225]).all()):
img1[i,j] = [0, 0, 0]
elif ((img1[i, j] > [225,225,225]).all() and (img1[i, j] <= [255,255,255]).all()):
img1[i,j] = [255, 255, 255]
else:
img1[i,j] = [0, 60, 0]
j=j+1
i=i-1
m=1
while m<2:
cv2.imshow('pseudocolor',img1)
cv2.waitKey(10)
运行此else语句时效果更好 即我该怎么做才能给像素值赋予不同的颜色,如[10,50,2],[23,3,167]等 关于像素值的分配存在一些问题..... 无法获取某些像素,这就是else语句工作次数更多的原因
答案 0 :(得分:-1)
import cv
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import Image
cam = cv2.VideoCapture(0)
s,img1 = cam.read()
imgg1=img1
height, width, depth = img1.shape
print height, width
for i in range(0,height):
for j in range(0,width):
if (img1[i, j] <= [25,25,25]).all():
img1[i,j] = [255, 255, 255]
imgg1[i, j] = [255, 0, 128]
elif (img1[i, j] <= [50,50,50]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [255, 255, 128]
elif (img1[i, j] <= [75,75,75]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [255, 128, 0]
elif (img1[i, j] <= [100,100,100]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [0, 255, 0]
elif (img1[i, j] <= [125,125,125]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [68, 128, 251]
elif (img1[i, j] <= [150,150,150]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [0, 255, 255]
elif (img1[i, j] <= [175,175,175]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [0, 0, 255]
elif (img1[i, j] <= [200,200,200]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [128, 128, 128]
elif (img1[i, j] <= [225,225,225]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [0, 0, 0]
elif (img1[i, j] < [255,255,255]).all():
img1[i,j] = [255, 255, 255]
imgg1[i,j] = [255, 255, 255]
else:
imgg1[i,j] = [0, 60, 0]
j=j+1
i=i-1
m=1
while m<2:
cv2.imshow('pseudocolor',imgg1)
cv2.waitKey(10)
以上程序执行似乎需要大约7-8分钟。 有没有提高执行速度的方法。