通过“声明”检查图像中某些感兴趣区域的像素值

问题描述 投票:0回答:2

我需要解决一个小问题。我有一幅图像,其中定义了该图像中的关注区域。现在,我想检查我ROI的像素值,以便如果ROI颜色范围在(105,105,105)和(255,255,255)之间,则打印'left',否则打印write。

问题是我无法制定if语句

示例代码:

import cv2
import matplotlib.pyplot as pltl
import numpy as np


frame=cv2.imread('frame 8 sec.jpg')


ROI=frame[450:500,380:391]



**if the color of ROI is in range of (105,105,105) and (255,255,255):!!!???** 

cv2.putText(frame,"Status {}".format('left'), (10,20), cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0,3))
else:
cv2.putText(frame,"Status {}".format('write'), (10,20), cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255,3)) 

cv2.imshow('img',frame)
counter=counter+1


cv2.waitKey(0)
cv2.destroyAllWindows()

非常感谢您能获得帮助在此先感谢

numpy opencv roi
2个回答
0
投票

尝试一下。

if (ROI>=105).all() and (ROI<=255).all():

0
投票

非常感谢您的回答。您发布的if语句对我有用,除了我现在面临另一个问题。我正在检查两幅图像中的ROI,在第一幅图像中ROI为白色方形,在第二幅图像中ROI变为黑色。现在,当我在两个图像中打印ROI的像素值时,它将返回几乎相同的像素值。参见下面的图片和示例代码

White ROIblack ROI

import cv2
import matplotlib.pyplot as pltl
import numpy as np


frame=cv2.imread('1m_white.bmp')



 ROI=frame[967:972,561:566]
 print(ROI)



 cv2.imshow('img',frame)
 cv2.waitKey(0)
 cv2.destroyAllWindows()

 Output for white ROI
 [[106 139 158]
 [ 98 143 177]
 [102 147 181]
 [105 154 180]
 [106 155 181]]

 [[104 143 158]
 [ 91 144 177]
 [ 95 148 181]
 [ 99 155 180]
 [100 156 181]]

 [[101 143 155]
 [ 92 143 175]
 [ 95 146 178]
 [ 99 153 178]
 [101 155 180]]

 [[108 145 153]
 [ 87 141 176]
 [ 91 145 180]
 [ 98 154 179]
 [ 99 155 180]]]

output for black ROI
[[105 142 156]
[ 82 148 173]
[ 86 152 177]
[102 154 177]
[104 156 179]]

[[102 141 150]
[ 84 143 175]
[ 87 146 178]
[105 150 177]
[106 151 178]]

[[107 143 151]
[ 90 140 176]
[ 93 143 179]
[102 149 177]
[104 151 179]]]

您看到的矩阵几乎相同,尽管一个图像中的ROI更接近白色,而另一图像中的ROI更接近黑色。你知道为什么会这样吗?

提前感谢

© www.soinside.com 2019 - 2024. All rights reserved.