如果使用 python 在捕获区域中存在特定颜色,如何 return 布尔值
how to return boolean if certain color exist in captured area using python
我是 python 的新手,刚开始自己学习它,但找不到解决问题的确切方法。
这就是我想要做的。
使用
捕获图像
判断截取的图像中是否存在某种颜色(如红、蓝、绿)
return 布尔值
我花了几天时间寻找答案,但我找不到任何答案。
我唯一知道的就是使用 openCV...
我想这个解决方案太简单了。我对计算机语言相当陌生,
所以我真的很感激尽可能详细。谢谢!
在 Python/OpenCV 中有两种方法可以做到这一点。
通过 cv2.inRange() 使用颜色阈值。然后使用 np.nonzero() 计算白色像素的数量,或者使用 cv2.mean().
测试阈值图像的平均值
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('red_brushed.png')
# set red range
lowcolor = (0,0,255)
highcolor =(128,128,255)
# threshold
thresh = cv2.inRange(img, lowcolor, highcolor)
# Method 1: count number of white pixels and test if zero
count = np.sum(np.nonzero(thresh))
print("count =",count)
if count == 0:
print("Not Red")
else:
print("Red")
print("")
# Method 2: get the average of the image and test if zero
average = cv2.mean(thresh)[0]
print("average =",average)
if average == 0:
print("Not Red")
else:
print("Red")
# write thresholded image to disk
cv2.imwrite("red_brushed_thresh.png", thresh)
# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESH", thresh)
cv2.waitKey(0)
阈值图像:
文本结果:
count = 3534485
Red
average = 6.933746337890625
Red
我在寻找一种方法来检查图像中是否出现某种颜色时遇到了这个 post。我想测试处理这两种方法所花费的时间。
继续 @fmw42 的 post:
import cv2
import numpy as np
import time
# read image
img = cv2.imread('red_brushed.png')
# set red range
lowcolor = (0,0,255)
highcolor =(128,128,255)
# threshold
thresh = cv2.inRange(img, lowcolor, highcolor)
t0 = time.process_time()
# Method 1: count number of white pixels and test if zero
count = np.sum(np.nonzero(thresh))
print(f"count = {count}")
if count == 0:
print("Not Red")
else:
print("Red")
print("")
t1 = time.process_time() - t0
print(f"Sum Method time elapsed {t1}")
t2 = time.process_time()
# Method 2: get the average of the image and test if zero
average = cv2.mean(thresh)[0]
print(f"average ={average}")
if average == 0:
print("Not Red")
else:
print("Red")
# write thresholded image to disk
cv2.imwrite("red_brushed_thresh.png", thresh)
t3 = time.process_time() - t2
print(f"Average Method time elapsed {t2}")
这产生了一般输出:
Sum Method time elapsed 0.0013151379999999935
Average Method time elapsed 0.186069437
事实证明,使用 sum()
和 np.nonzero()
总是更快。
我是 python 的新手,刚开始自己学习它,但找不到解决问题的确切方法。
这就是我想要做的。
使用
捕获图像
判断截取的图像中是否存在某种颜色(如红、蓝、绿)
return 布尔值
我花了几天时间寻找答案,但我找不到任何答案。
我唯一知道的就是使用 openCV...
我想这个解决方案太简单了。我对计算机语言相当陌生, 所以我真的很感激尽可能详细。谢谢!
在 Python/OpenCV 中有两种方法可以做到这一点。
通过 cv2.inRange() 使用颜色阈值。然后使用 np.nonzero() 计算白色像素的数量,或者使用 cv2.mean().
测试阈值图像的平均值输入:
import cv2
import numpy as np
# read image
img = cv2.imread('red_brushed.png')
# set red range
lowcolor = (0,0,255)
highcolor =(128,128,255)
# threshold
thresh = cv2.inRange(img, lowcolor, highcolor)
# Method 1: count number of white pixels and test if zero
count = np.sum(np.nonzero(thresh))
print("count =",count)
if count == 0:
print("Not Red")
else:
print("Red")
print("")
# Method 2: get the average of the image and test if zero
average = cv2.mean(thresh)[0]
print("average =",average)
if average == 0:
print("Not Red")
else:
print("Red")
# write thresholded image to disk
cv2.imwrite("red_brushed_thresh.png", thresh)
# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESH", thresh)
cv2.waitKey(0)
阈值图像:
文本结果:
count = 3534485
Red
average = 6.933746337890625
Red
我在寻找一种方法来检查图像中是否出现某种颜色时遇到了这个 post。我想测试处理这两种方法所花费的时间。
继续 @fmw42 的 post:
import cv2
import numpy as np
import time
# read image
img = cv2.imread('red_brushed.png')
# set red range
lowcolor = (0,0,255)
highcolor =(128,128,255)
# threshold
thresh = cv2.inRange(img, lowcolor, highcolor)
t0 = time.process_time()
# Method 1: count number of white pixels and test if zero
count = np.sum(np.nonzero(thresh))
print(f"count = {count}")
if count == 0:
print("Not Red")
else:
print("Red")
print("")
t1 = time.process_time() - t0
print(f"Sum Method time elapsed {t1}")
t2 = time.process_time()
# Method 2: get the average of the image and test if zero
average = cv2.mean(thresh)[0]
print(f"average ={average}")
if average == 0:
print("Not Red")
else:
print("Red")
# write thresholded image to disk
cv2.imwrite("red_brushed_thresh.png", thresh)
t3 = time.process_time() - t2
print(f"Average Method time elapsed {t2}")
这产生了一般输出:
Sum Method time elapsed 0.0013151379999999935
Average Method time elapsed 0.186069437
事实证明,使用 sum()
和 np.nonzero()
总是更快。