A task to detect "object-like" parts in an image. As a rough flow, prepare multiple rectangles and slide them on the image to find the "object-like" part. Note that it doesn't just detect what an object is or a specific object. For example, it is not possible to find only a car in an image. It is a famous task that is also used in R-CNN, which is famous for its object detection method, but when I looked it up, there were surprisingly few articles, so I summarized it in this article.
Easy to install using pip
pip install selective-search
Install opencv with pip
Let's execute it for the following four images. The image is Pascal VOC.
car | dog | furniture |
---|---|---|
random There is a random parameter, but the reference says:
If random set to True, function will carry out pseudo random sorting. It only alters sequences of bounding boxes, instead of locations, which prevents heavily emphasis on large regions as combing proposals from up to 80 different strategies[1]. This only has a significant impact when selecting a subset of region proposals with high rankings, as in RCNN.
Apparently, in selective search, large rectangles are given priority and presented. When detecting an object using RCNN etc., it becomes impossible to recognize small objects, so by setting random, it is possible to prevent only large objects.
mode There are three modes in selective search
According to the reference, there are the following differences.
That is, as single, fast, quality, the number of rectangles, in other words, the number of trials increases. On the other hand, there is also the problem that the speed drops. The following is the trial result.
mode | car | furniture | dog |
---|---|---|---|
single | |||
fast | |||
quality |
Estimated objects increase as single, fast, quality, but there are almost no cases that require so many rectangles. Basically, it is enough to implement it with single.
Even if single is set, there are too many rectangles, so when implementing this time, we decided to suppress the output of rectangles that do not have a certain size.
Directory structure
├── pic #Original image
├── result #Result image
│ ├── fast
│ ├── quality
│ └── single
└── exe.py #Executable file
Executable file
import cv2
import glob
from selective_search import selective_search
###parameter settings
MODE="single"
#MODE="fast"
#MODE="quality"
MINH=100
MINW=100
###I / O directory settings
ORGDIR="./pic/"
RSTDIR="./result/"+MODE+"/"
def search():
###Read input file path
tgtpaths=glob.glob(ORGDIR+"*")
for tgtpath in tgtpaths:
###Output file path setting
rstpath=tgtpath.replace(ORGDIR,RSTDIR)
###Target image reading
tgtimg = cv2.imread(tgtpath, cv2.IMREAD_COLOR)
###Run Selective Search
boxes = selective_search(tgtimg, mode=MODE,random=False)
for box in boxes:
###Do not display rectangles without a certain length
if abs(box[2]-box[0]) < MINW or abs(box[3]-box[1])<MINH:
continue
###Rectangle drawing
cv2.rectangle(tgtimg, (box[0],box[1]), (box[2],box[3]), (0,255,0), thickness=1)
###output
print(rstpath)
cv2.imwrite(rstpath, tgtimg)
if __name__=="__main__":
search()
car | furniture | dog |
---|---|---|
Selectivesearch presented "object-like" parts. In RCNN etc., object detection is performed by applying these candidates to a classifier.
Recommended Posts