假设我有一个像这样的热图:
热图上的像素值范围是0-1。值越接近0,像素越暗。如图所示,上面有六个写簇。有什么方法可以让我知道热图中有多少个光簇?
最佳答案
源=>检测
我的方法是:灰色=>阈值=> FindContours(必要时按区域过滤)
#!/usr/bin/python3
# 2019/03/01
import cv2
img = cv2.imread("featmap.png")
# Gray => Threshold => FindContours (filter by area if necessary)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blured = cv2.medianBlur(gray, 3)
th, threshed = cv2.threshold(blured, 100, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY)
cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
print("nums: {}".format(len(cnts)))
# draw on the original
cv2.drawContours(img, cnts, -1, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imwrite("dst.png", img)
使用
findContours
时要小心:How to use `cv2.findContours` in different OpenCV versions?