如何在图像的多个矩形边界框中应用阈值?

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

问题描述

我的问题是:我有图像中对象周围的边界框的 ROI.ROI 由 Faster R-CNN 获得.现在我想要的是应用阈值来使对象准确地包含在边界框中.该图像的 ROI 由 Faster RCNN 获得.

所以,在获得 ROI 后,我只从图像中选择 ROI 并粘贴到相同大小和尺寸的黑色图像上,从而产生以下图像.让我们说

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

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

将 numpy 导入为 np导入简历2图像 = cv2.imread('1.jpg')结果 = image.copy()图像 = cv2.cvtColor(图像,cv2.COLOR_BGR2HSV)下 = np.array([18, 0, 0])上 = np.array([179, 255, 255])掩码 = cv2.inRange(图像,下,上)结果= cv2.bitwise_and(结果,结果,掩码=掩码)cv2.imshow('结果', 结果)cv2.imwrite('result.png', 结果)cv2.waitKey()

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

导入 cv2导入系统将 numpy 导入为 np什么都没有(x):经过# 创建一个窗口cv2.namedWindow('图像')# 创建颜色变化的轨迹栏cv2.createTrackbar('HMin','image',0,179,nothing) # Opencv 的色调为 0-179cv2.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)# 设置 MAX HSV 轨迹栏的默认值.cv2.setTrackbarPos('HMax', '图像', 179)cv2.setTrackbarPos('SMax', '图像', 255)cv2.setTrackbarPos('VMax', 'image', 255)# 初始化检查 HSV 最小/最大值是否改变hMin = sMin = vMin = hMax = sMax = vMax = 0phMin = psMin = pvMin = phMax = psMax = pvMax = 0img = cv2.imread('1.jpg')输出 = img等待时间 = 33而(1):# 获取所有轨迹栏的当前位置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')# 设置要显示的最小和最大 HSV 值较低 = np.array([hMin, sMin, vMin])上 = np.array([hMax, sMax, vMax])# 创建 HSV 图像和阈值到一个范围内.hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)掩码 = cv2.inRange(hsv, 下, 上)输出 = cv2.bitwise_and(img,img, mask= mask)# 如果 HSV 值发生变化,打印如果((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 = hMinpsMin = sMinpvMin = vMinphMax = hMaxpsMax = sMaxpvMax = vMax# 显示输出图像cv2.imshow('图像',输出)# 等待更长的时间以防止视频冻结.如果 cv2.waitKey(waitTime) &0xFF == ord('q'):休息cv2.destroyAllWindows()

这是原始图像上的结果

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.

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?

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

ROI file for first image

解决方案

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

Here's the main idea

  • Convert image to HSV format since it is easier to represent color than RBG
  • Perform color segmentation with a lower/upper threshold

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()

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()

Here's the result on the original image

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

上一篇:如何下载 Coco Dataset 的特定部分? 下一篇:如何使用 OpenCV 检测和跟踪人员?

相关文章

最新文章