这里只是展示一下OpenCV自带的显着性检测函数。效果都不是很好,各位可以测试一下。
需要OpenCV 3.4或更高版本
- cv2.saliency.ObjectnessBING_create()
- cv2.saliency.StaticSaliencySpectralResidual_create()
- cv2.saliency.StaticSaliencyFineGrained_create()
- cv2.saliency.MotionSaliencyBinWangApr2014_create()
静态显着性
OpenCV实现了两种用于静态显着性检测的算法。
第一种方法来自Montabone and Soto的2010年出版的《使用移动平台进行人体检测》和源自视觉显着性机制的新颖功能。
该算法最初用于检测图像和视频流中的人,但也可以推广到其他形式的显着性。
第二种方法是Hou和Zhang在其2007年CVPR论文中提出的“ 显着性检测:一种光谱残留方法”。
关于原理【请点击】
import cv2
imgpath = r'C:\Users\xxx\Desktop\the_angry_birds_movie.jpg'
image = cv2.imread(imgpath)
saliency = cv2.saliency.StaticSaliencySpectralResidual_create()
(success, saliencyMap) = saliency.computeSaliency(image)
saliencyMap = (saliencyMap * 255).astype("uint8")
cv2.imshow("Image", image)
cv2.imshow("Output", saliencyMap)
cv2.waitKey(0)
cv2.destroyAllWindows()
=======================================================================
imgpath = r'C:\Users\xxx\Desktop\the_angry_birds_movie.jpg'
image = cv2.imread(imgpath)
saliency = cv2.saliency.StaticSaliencyFineGrained_create()
(success, saliencyMap) = saliency.computeSaliency(image)
# 对显著映射设置阈值
threshMap = cv2.threshold(saliencyMap.astype("uint8"), 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.imshow("Image", image)
cv2.imshow("Output", saliencyMap)
cv2.imshow("Thresh", threshMap)
cv2.waitKey(0)
运动显着性
OpenCV显着性检测器来自Wang和Dudek的2014年出版的一种快速自调整背景扣除算法。
https://ieeexplore.ieee.org/document/6910012/
from imutils.video import VideoStream
import imutils
import time
import cv2
saliency = None
vs = VideoStream(src=0).start()
time.sleep(2.0)
while True:
frame = vs.read()
frame = imutils.resize(frame, width=500)
if saliency is None: # 若显著性对象是None,则需要实例化
saliency = cv2.saliency.MotionSaliencyBinWangApr2014_create()
saliency.setImagesize(frame.shape[1], frame.shape[0])
saliency.init()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(success, saliencyMap) = saliency.computeSaliency(gray)
saliencyMap = (saliencyMap * 255).astype("uint8")
cv2.imshow("Frame", frame)
cv2.imshow("Map", saliencyMap)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.stop()
客观显着性
OpenCV包含一个客观性显着性检测器-BING:由Cheng等人在300fps下进行客观性估计的二值化范数梯度。(CVPR 2014)。https://mmcheng.net/bing/
OpenCV中的其他显着性检测器在实现中完全独立的不同,BING显着性检测器需要九个单独的模型文件以用于各种窗口大小,色彩空间和数学运算。
测试命令:python objectness_saliency.py --model objectness_trained_model --image images/barcelona.jpg
import numpy as np
import cv2
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True,
help="path to BING objectness saliency model")
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-n", "--max-detections", type=int, default=10,
help="maximum # of detections to examine")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
saliency = cv2.saliency.ObjectnessBING_create()
saliency.setTrainingPath(args["model"])
(success, saliencyMap) = saliency.computeSaliency(image)
numDetections = saliencyMap.shape[0]
for i in range(0, min(numDetections, args["max_detections"])):
(startX, startY, endX, endY) = saliencyMap[i].flatten()
output = image.copy()
color = np.random.randint(0, 255, size=(3,))
color = [int(c) for c in color]
cv2.rectangle(output, (startX, startY), (endX, endY), color, 2)
cv2.imshow("Image", output)
cv2.waitKey(0)