简体   繁体   English

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

[英]How to apply threshold within multiple rectangular bounding boxes in an image?

My question is that: I have ROI's for the bounding boxes around the objects in an image. 我的问题是:我对图像中对象周围的边界框有ROI。 The ROI's are obtained by the Faster R-CNN. ROI是由更快的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由更快的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 因此,在获得ROI之后,我只选择了图像中的ROI并粘贴在相同大小和尺寸的黑色图像上,从而得到以下图像。

图像仅包含边界框

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文本文件的链接

ROI file for first image 第一张图片的ROI文件

Color thresholding using cv2.inRange() should work here. 使用cv2.inRange()颜色阈值cv2.inRange()应该在这里工作。 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 将图像转换为HSV格式,因为它比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 您可以使用HSV颜色阈值脚本来隔离所需的颜色范围

在此输入图像描述

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 这是原始图像的结果

在此输入图像描述

In your TensorFlow detection, the output dictionary you get after you run the prediction has a field, "detection_scores". 在TensorFlow检测中,运行预测后得到的输出字典有一个字段“detection_scores”。

output_dict = sess.run(tensor_dict,feed_dict={image_tensor: image})

Set a threshold on that, 设置一个阈值,

 indexes=np.where(output_dict['detection_scores']>0.5)

Use the boxes, ie output_dict['detection_boxes'] only on those specific indexes which you filtered in the previous step. 使用这些框,即output_dict ['detection_boxes']仅对您在上一步中过滤的特定索引。

[EDIT] Adding more code after the discussion in comments [编辑]在评论中讨论后添加更多代码

#convert the image to hsv
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#tune the numbers below accordingly
lower_green = np.array([60, 100, 50])
upper_green = np.array([60 , 255, 255])

mask = cv2.inRange(hsv, lower_green, upper_green)
res = cv2.bitwise_and(frame,frame, mask= mask)
#res has the output masked image

[EDIT] editing with the actual image given in the question [编辑]使用问题中给出的实际图像进行编辑

img=cv2.imread("idJyc.jpg")
lower_green = np.array([0, 10, 0])
upper_green = np.array([255 , 100, 255])
mask = cv2.inRange(img, lower_green, upper_green)
mask = np.abs(255-mask)
res = cv2.bitwise_and(img,img, mask=mask)
cv2.imshow("a",res)
cv2.waitKey(0)

Adding the output image for your reference. 添加输出图像供您参考。

在此输入图像描述

If you re familiar with applying neural networks and you re having enough data. 如果你熟悉应用神经网络并且你有足够的数据。 This task is perfectly suited for segmentation. 此任务非常适合细分。

I recommend U-Net , since it works with a small amount of labelled data for training. 我推荐使用U-Net ,因为它可以处理少量标记数据用于培训。 It is also fast, with few operations for this task with comparably low complexity. 它也很快,这项任务的操作很少 ,复杂程度相对较低。 And has shown good performance on various tasks. 并且在各种任务上表现出良好的表现。

I also found a full code pipeline , in this case for heart segmentation in zebrafish, but in my opinion they did a good job explaining how to prepare the data (proposing labeling tools, etc.) and train the model. 我还找到了一个完整的代码管道 ,在这种情况下用于斑马鱼的心脏分割,但在我看来,他们很好地解释了如何准备数据(提出标记工具等)并训练模型。

Also taking a step back, you could also think of interpreting your task as a segmentation task from the beginning. 退后一步,您还可以考虑从一开始就将任务解释为分段任务。 Especially for the U-Net it shouldn't be a problem to segment multiple instances in the same image. 特别是对于U-Net,在同一图像中分割多个实例应该不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM