问题描述
我编写了一个代码来查找边界框内白色像素的百分比。所以基本上代码给出了 % 的白色像素,我用它来进一步将 YOLO 对象检测中的“人”类对象分类为穿白衬衫的人而不是穿白衬衫的人。现在我正在抓取边界框坐标并计算白色像素。如果此人穿着白色裤子/边界框的下半部分,则会出现问题。有没有办法将边界框分成两半,只抓取边界框的上半部分。这是我的代码:
region = frame[x:x+w,y:y+h] #region is my bounding Box of class Person grabbed from video frame
#print(region)
# Here,you define your target color as
# a tuple of three values: RGB
if region.size:
white = [255,255,255]
#print("hfc")
# You define an interval that covers the values
# in the tuple and are below and above them by 15
diff = 15
# Be aware that opencv loads image in BGR format,# that's why the color values have been adjusted here:
boundaries = [([white[2],white[1]-diff,white[0]-diff],[white[2]+diff,white[1]+diff,white[0]+diff])]
# Scale your BIG image into a small one:
scalePercent = 0.3
# Calculate the new dimensions
width = int(region.shape[1] * scalePercent)
height = int(region.shape[0] * scalePercent)
newSize = (width,height)
# for each range in your boundary list:
for (lower,upper) in boundaries:
# # You get the lower and upper part of the interval:
lower = np.array([0,255],dtype=np.uint8)
upper = np.array([255,dtype=np.uint8)
# cv2.inRange is used to binarize (i.e.,render in white/black) an image
# All the pixels that fall inside your interval [lower,uipper] will be white
# All the pixels that do not fall inside this interval will
# be rendered in black,for all three channels:
mask = cv2.inRange(region,lower,upper)
# Now,you AND the mask and the input image
# All the pixels that are white in the mask will
# survive the AND operation,all the black pixels
# will remain black
output = cv2.bitwise_and(region,region,mask=mask)
# You can use the mask to count the number of white pixels.
# Divide by the image size and you got the
# percentage of white pixels in the original image:
ratio_white = cv2.countNonZero(mask)/(region.size/3)
# This is the color percent calculation,considering the resize I did earlier.
colorPercent = (ratio_white * 100) / scalePercent
# Print the color percent,use 2 figures past the decimal point
print('white pixel percentage:',np.round(colorPercent,2))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)