Coding/Image

[2] Selective Search

linguana 2021. 4. 12. 19:55

 

www.pyimagesearch.com/2020/06/29/opencv-selective-search-for-object-detection/

 

OpenCV Selective Search for Object Detection - PyImageSearch

In this tutorial you will learn how to use OpenCV Selective Search for object detection with Python.

www.pyimagesearch.com

 


Pyramid of Image와 Sliding Window를 이용한 전통적인 방식을 대체할 수 있는 Selective Search 방법에 대해서 알아보자.

 

파일구조:

간단하다. 테스트 할 이미지 1장과 파이썬 파일 1개다.

 


필요한 라이브러리 임포트, 아규먼트 파싱

 


이미지 로딩, selective search 초기화, 모드 프린트해주기


selective search 실행

 


결과물 출력


코드 정리:

더보기
# import the necessary packages
import argparse
import random
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
	help="path to the input image")
ap.add_argument("-m", "--method", type=str, default="fast",
	choices=["fast", "quality"],
	help="selective search method")
args = vars(ap.parse_args())

# load the input image
image = cv2.imread(args["image"])

# initialize OpenCV's selective search implementation and set the
# input image
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(image)

# check to see if we are using the *fast* but *less accurate* version
# of selective search
if args["method"] == "fast":
	print("[INFO] using *fast* selective search")
	ss.switchToSelectiveSearchFast()
    
# otherwise we are using the *slower* but *more accurate* version


else:
	print("[INFO] using *quality* selective search")
	ss.switchToSelectiveSearchQuality()
    
# run selective search on the input image
start = time.time()
rects = ss.process()
end = time.time()

# show how along selective search took to run along with the total
# number of returned region proposals
print("[INFO] selective search took {:.4f} seconds".format(end - start))
print("[INFO] {} total region proposals".format(len(rects)))

# loop over the region proposals in chunks (so we can better
# visualize them)
for i in range(0, len(rects), 100):
	# clone the original image so we can draw on it
	output = image.copy()
    
	# loop over the current subset of region proposals
	for (x, y, w, h) in rects[i:i + 100]:
		# draw the region proposal bounding box on the image
		color = [random.randint(0, 255) for j in range(0, 3)]
		cv2.rectangle(output, (x, y), (x + w, y + h), color, 2)
        
	# show the output image
	cv2.imshow("Output", output)
	key = cv2.waitKey(0) & 0xFF
    
	# if the `q` key was pressed, break from the loop
	if key == ord("q"):
		break

 

 

실행하는 방법 ($ 표시는 cmd)