如何在图像的多个矩形边界框内应用阈值? [英] How to apply threshold within multiple rectangular bounding boxes in an image?

查看:108
本文介绍了如何在图像的多个矩形边界框内应用阈值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是: 对于图像中对象周围的边界框,我有ROI. ROI是由Faster R-CNN获得的.现在,我想要的是应用阈值处理,以将对象准确地包含在边界框中.该图像的投资回报率由Faster RCNN获得.

My question is that: I have ROI's for the bounding boxes around the objects in an image. The ROI's are obtained by the Faster R-CNN. Now what I want is to apply the thresholding to get the object accurately contained within the bounding box. The ROI of this image was got by the Faster RCNN.

因此,在获得ROI之后,我仅从图像中选择ROI并粘贴到相同大小和尺寸的黑色图像上,从而得到以下图像.

So, After getting the ROI's, I only selected ROI from the image and pasted on the black image of the same size and dimension which result in the following image.let say

如您所见,方框是矩形的,因此在某些地方它会覆盖一些背景区域以及尖峰.那么,如何应用阈值处理以仅使尖峰和其他像素变为黑色?

As you can see that boxes are rectangular so in some places it covers some background area along with spikes. So, how can I apply thresholding to get only the spikes and other pixels turn to black?

编辑: 我已将链接添加到问题中第一张图片的ROI文本文件中

EDIT: I've added the link to the ROI text file of the first image in the question

第一张图片的ROI文件

推荐答案

使用

Color thresholding using cv2.inRange() should work here. I'm assuming you want to isolate the green area

这是主要思想

  • 将图像转换为HSV格式,因为它比RBG表示颜色更容易
  • 使用较低/较高的阈值进行颜色分割

您还可以执行形态学操作在获得口罩后平滑或消除噪音

You could also perform morphological operations to smooth or remove noise after obtaining the mask

import numpy as np
import cv2

image = cv2.imread('1.jpg')
result = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([18, 0, 0])
upper = np.array([179, 255, 255])
mask = cv2.inRange(image, lower, upper)
result = cv2.bitwise_and(result,result, mask=mask)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)
cv2.waitKey()

您可以使用HSV颜色阈值脚本来隔离所需的颜色范围

You can use a HSV color thresholder script to isolate the desired color range

import cv2
import sys
import numpy as np

def nothing(x):
    pass

# Create a window
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)

# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

img = cv2.imread('1.jpg')
output = img
waitTime = 33

while(1):

    # get current positions of all trackbars
    hMin = cv2.getTrackbarPos('HMin','image')
    sMin = cv2.getTrackbarPos('SMin','image')
    vMin = cv2.getTrackbarPos('VMin','image')

    hMax = cv2.getTrackbarPos('HMax','image')
    sMax = cv2.getTrackbarPos('SMax','image')
    vMax = cv2.getTrackbarPos('VMax','image')

    # Set minimum and max HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Create HSV Image and threshold into a range.
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    output = cv2.bitwise_and(img,img, mask= mask)

    # Print if there is a change in HSV value
    if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display output image
    cv2.imshow('image',output)

    # Wait longer to prevent freeze for videos.
    if cv2.waitKey(waitTime) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

这是原始图像上的结果

这篇关于如何在图像的多个矩形边界框内应用阈值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆