def _extract_spots(self) -> None:
# Dilate and Erode to 'clean' the spot (nb that this harms the number itself, so we only do it to extract spots)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
img = cv2.dilate(self._img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=2)
img = cv2.dilate(img, kernel, iterations=1)
# Perform a simple blob detect
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = 20 # The dot in 20pt font has area of about 30
params.filterByCircularity = True
params.minCircularity = 0.7
params.filterByConvexity = True
params.minConvexity = 0.8
params.filterByInertia = True
params.minInertiaRatio = 0.4
detector = cv2.SimpleBlobDetector_create(params)
self.spot_keypoints = detector.detect(img)
# Log intermediate image
img_with_keypoints = cv2.drawKeypoints(img, self.spot_keypoints, outImage=np.array([]), color=(0, 0, 255),
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
self.intermediate_images.append(NamedImage(img_with_keypoints, 'Spot Detection Image'))
python类DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS的实例源码
def add_blobs(crop_frame):
frame=cv2.GaussianBlur(crop_frame, (3, 3), 0)
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of green color in HSV
lower_green = np.array([70,50,50])
upper_green = np.array([85,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_green, upper_green)
mask = cv2.erode(mask, None, iterations=1)
mask = cv2.dilate(mask, None, iterations=1)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
reversemask=255-mask
keypoints = detector.detect(reversemask)
if keypoints:
print "found blobs"
if len(keypoints) > 4:
keypoints.sort(key=(lambda s: s.size))
keypoints=keypoints[0:3]
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(frame, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
else:
print "no blobs"
im_with_keypoints=crop_frame
return im_with_keypoints #, max_blob_dist, blob_center, keypoint_in_orders
def execute_BlobDetector(proxy,obj):
try: img=obj.sourceObject.Proxy.img.copy()
except: img=cv2.imread(__dir__+'/icons/freek.png')
im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
im=255-im
im2 = img
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = obj.Area
params.filterByConvexity = True
params.minConvexity = obj.Convexity/200
# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
if not obj.showBlobs:
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
obj.Proxy.img = im_with_keypoints
for k in keypoints:
(x,y)=k.pt
x=int(round(x))
y=int(round(y))
# cv2.circle(im,(x,y),4,0,5)
cv2.circle(im,(x,y),4,255,5)
cv2.circle(im,(x,y),4,0,5)
im[y,x]=255
im[y,x]=0
obj.Proxy.img = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)
else:
for k in keypoints:
(x,y)=k.pt
x=int(round(x))
y=int(round(y))
cv2.circle(im2,(x,y),4,(255,0,0),5)
cv2.circle(im2,(x,y),4,(0,0,0),5)
im2[y,x]=(255,0,0)
im2[y,x]=(0,0,0)
obj.Proxy.img = im2
def find_blobs(img):
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 100;
params.maxThreshold = 5000;
# Filter by Area.
params.filterByArea = True
params.minArea = 200
# Filter by Circularity
params.filterByCircularity = False
params.minCircularity = 0.785
# Filter by Convexity
params.filterByConvexity = False
params.minConvexity = 0.87
# Filter by Inertia
#params.filterByInertia = True
#params.minInertiaRatio = 0.01
# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector(params)
# Detect blobs.
keypoints = detector.detect(img)
print keypoints
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]),
(0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite("blobs.jpg", im_with_keypoints);
def draw_keypoints(self, im, keypoints, filename="keypoints.jpg"):
self._log("drawing keypoints into '%s'..." % filename)
rows, cols = im.shape
def to_cv2_kp(kp):
# assert kp = [<row>, <col>, <ori>, <octave_ind>, <layer_ind>]
ratio = get_size_ratio_by_octave(kp[3])
scale = get_scale_by_ind(kp[3], kp[4])
return cv2.KeyPoint(kp[1] / ratio, kp[0] / ratio, 10, kp[2] / PI * 180)
kp_for_draw = list(map(to_cv2_kp, keypoints))
im_kp = cv2.drawKeypoints(im, kp_for_draw, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite(filename, im_kp)
def cv2_match(im1, im2):
mysift = SIFT()
sift = cv2.SIFT()
bf = cv2.BFMatcher()
kp1, dp1 = sift.detectAndCompute(im1, None)
kp2, dp2 = sift.detectAndCompute(im2, None)
matches_ = bf.knnMatch(dp1, dp2, k=2)
print(len(matches_))
good = []
for m, n in matches_:
if m.distance < 0.90 * n.distance:
good.append(m)
print(len(good))
pos1 = [(int(kp.pt[1]), int(kp.pt[0])) for kp in kp1]
pos2 = [(int(kp.pt[1]), int(kp.pt[0])) for kp in kp2]
matches = [(m.queryIdx, m.trainIdx, 0.15) for m in good]
cv2.imwrite("cvkp1.jpg", cv2.drawKeypoints(im, kp1, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))
cv2.imwrite("cvkp2.jpg", cv2.drawKeypoints(imm, kp2, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS))
mysift.draw_matches(im, pos1, imm, pos2, matches, 'ckmatch.jpg')
def visualize_keypoints(image, keypoints):
kp_image = np.array([])
kp_image = cv2.drawKeypoints(image, keypoints, kp_image, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow(PROJ_WIN, kp_image)
wait()
def draw_image_with_keypoints(img: np.ndarray, keypoints, window_title: str ="Image with keypoints") -> None:
"""An apparently unused method which is actually quite useful when debugging!"""
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
img_with_keypoints = cv2.drawKeypoints(img, keypoints, outImage=np.array([]), color=(0, 0, 255),
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
draw_image(img_with_keypoints, window_title)
featuredetect.py 文件源码
项目:Compare-OpenCV-SIFT-SURF-FAST-ORB
作者: chengtaow
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def sift_thread():
sift = cv2.xfeatures2d.SIFT_create()
(kps, descs) = sift.detectAndCompute(gray, None)
cv2.drawKeypoints(gray, kps, img, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT Algorithm', img)
featuredetect.py 文件源码
项目:Compare-OpenCV-SIFT-SURF-FAST-ORB
作者: chengtaow
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def surf_thread():
surf = cv2.xfeatures2d.SURF_create()
(kps2, descs2) = surf.detectAndCompute(gray, None)
cv2.drawKeypoints(gray, kps2, img2, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SURF Algorithm', img2)
featuredetect.py 文件源码
项目:Compare-OpenCV-SIFT-SURF-FAST-ORB
作者: chengtaow
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def fast_thread():
fast = cv2.FastFeatureDetector_create()
kps3 = fast.detect(gray, None)
cv2.drawKeypoints(gray, kps3, img3, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('FAST Algorithm', img3)
featuredetect.py 文件源码
项目:Compare-OpenCV-SIFT-SURF-FAST-ORB
作者: chengtaow
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def orb_thread():
orb = cv2.ORB_create()
kps4 = orb.detect(gray, None)
(kps4, des4) = orb.compute(gray, kps4)
cv2.drawKeypoints(gray, kps4, img4, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('ORB Algorithm', img4)