問題描述
我的問題是:我有圖像中對象周圍的邊界框的 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
這篇關于如何在圖像的多個矩形邊界框中應用閾值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!