隔离图像中的多个暗区
Isolating for multiple dark regions in an image
我有多个带有 darks bands and I want to draw a rectangle over all of the darker bands. I have tried using HSV color detection, it works reasonably well but it picks up darker regions Current output of the code 的图像,代码如下。
import cv2
import numpy as np
import matplotlib.pyplot as plt
stretch = frames.copy() #Frame is the imported image
#Increasing the contrast of the image
# CLAHE (Contrast Limited Adaptive Histogram Equalization)
clahe = cv2.createCLAHE(clipLimit=2., tileGridSize=(8,8))
lab = cv2.cvtColor(stretch, cv2.COLOR_BGR2LAB) # convert from BGR to LAB color space
l, a, b = cv2.split(lab) # split on 3 different channels
l2 = clahe.apply(l) # apply CLAHE to the L-channel
lab = cv2.merge((l2,a,b)) # merge channels
img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) # convert from LAB to BG
# convert to gray
gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# blur
smooth = cv2.GaussianBlur(gray, (11,11), 0)
img2 = cv2.cvtColor(smooth,cv2.COLOR_GRAY2BGR)
frame_HSV = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
frame_threshold = cv2.inRange(frame_HSV, (0, 0, 10), (180, 255, 80))
contours,hierarchy, = cv2.findContours(frame_threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
sorted_areas = np.sort(areas)
#cnt=contours[areas.index(sorted_areas[-1])] #the biggest contour
largestContourArea = sorted_areas[-1]*0.1
largestContour = 0
for cnt in contours:
contourArea = cv2.contourArea(cnt)
if( contourArea > largestContourArea):
#cv2.drawContours(frames, cnt, -1, (255,0,132), thickness=4)
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(frames, (x, y), (x+w, y+h), (255,0,0), 4)
ROI = frames[y:y+h,x:x+w]
plt.imshow(ROI)
plt.show()
cv2.drawContours(frames, contours, -1, (0,150,132), thickness=4)
plt.imshow(frames)
plt.show()
Output I want
我已经尝试过 Canny 检测,但无法正常工作,我对它的经验有限。还有另一种方法可以在暗带周围画一个方框吗?
Otsu 阈值处理对该图像的处理非常好。恕我直言,将近灰色转换为 HSL 没有什么意义,因为没有颜色,所以我将其加载为灰度:
import cv2
# Load as greyscale - no colour anyway
im = cv2.imread('9uu2H.jpg', cv2.IMREAD_GRAYSCALE)
# Otsu threshold
_, thr = cv2.threshold(im,127,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
注意:我剪掉了你的黑色边框,因为我不想将 Otsu 阈值与额外的第三个 class 灰色(即黑色)混淆。
注意:我人为添加了红色边框,以便您可以在 Stack Overflow 的背景上看到范围。
注意:您可以添加模糊、过滤或形态来填充孔洞。
我有多个带有 darks bands and I want to draw a rectangle over all of the darker bands. I have tried using HSV color detection, it works reasonably well but it picks up darker regions Current output of the code 的图像,代码如下。
import cv2
import numpy as np
import matplotlib.pyplot as plt
stretch = frames.copy() #Frame is the imported image
#Increasing the contrast of the image
# CLAHE (Contrast Limited Adaptive Histogram Equalization)
clahe = cv2.createCLAHE(clipLimit=2., tileGridSize=(8,8))
lab = cv2.cvtColor(stretch, cv2.COLOR_BGR2LAB) # convert from BGR to LAB color space
l, a, b = cv2.split(lab) # split on 3 different channels
l2 = clahe.apply(l) # apply CLAHE to the L-channel
lab = cv2.merge((l2,a,b)) # merge channels
img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) # convert from LAB to BG
# convert to gray
gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# blur
smooth = cv2.GaussianBlur(gray, (11,11), 0)
img2 = cv2.cvtColor(smooth,cv2.COLOR_GRAY2BGR)
frame_HSV = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
frame_threshold = cv2.inRange(frame_HSV, (0, 0, 10), (180, 255, 80))
contours,hierarchy, = cv2.findContours(frame_threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
sorted_areas = np.sort(areas)
#cnt=contours[areas.index(sorted_areas[-1])] #the biggest contour
largestContourArea = sorted_areas[-1]*0.1
largestContour = 0
for cnt in contours:
contourArea = cv2.contourArea(cnt)
if( contourArea > largestContourArea):
#cv2.drawContours(frames, cnt, -1, (255,0,132), thickness=4)
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(frames, (x, y), (x+w, y+h), (255,0,0), 4)
ROI = frames[y:y+h,x:x+w]
plt.imshow(ROI)
plt.show()
cv2.drawContours(frames, contours, -1, (0,150,132), thickness=4)
plt.imshow(frames)
plt.show()
Output I want
我已经尝试过 Canny 检测,但无法正常工作,我对它的经验有限。还有另一种方法可以在暗带周围画一个方框吗?
Otsu 阈值处理对该图像的处理非常好。恕我直言,将近灰色转换为 HSL 没有什么意义,因为没有颜色,所以我将其加载为灰度:
import cv2
# Load as greyscale - no colour anyway
im = cv2.imread('9uu2H.jpg', cv2.IMREAD_GRAYSCALE)
# Otsu threshold
_, thr = cv2.threshold(im,127,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
注意:我剪掉了你的黑色边框,因为我不想将 Otsu 阈值与额外的第三个 class 灰色(即黑色)混淆。
注意:我人为添加了红色边框,以便您可以在 Stack Overflow 的背景上看到范围。
注意:您可以添加模糊、过滤或形态来填充孔洞。