mirror of
https://github.com/RustyRaptor/learnopencv.git
synced 2026-12-03 22:15:16 -07:00
Selective Search
This commit is contained in:
1 parent
67e8630b42
commit
ba182cbe58
4 files changed
+179
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 122 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 564 KiB |
@@ -0,0 +1,97 @@
|
||||
#include "opencv2/ximgproc/segmentation.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::ximgproc::segmentation;
|
||||
|
||||
static void help() {
|
||||
std::cout << std::endl <<
|
||||
"Usage:" << std::endl <<
|
||||
"./ssearch input_image (f|q)" << std::endl <<
|
||||
"f=fast, q=quality" << std::endl <<
|
||||
"Use l to display less rects, m to display more rects, q to quit" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// If image path and f/q is not passed as command
|
||||
// line arguments, quit and display help message
|
||||
if (argc < 3) {
|
||||
help();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// read image
|
||||
Mat im = imread(argv[1]);
|
||||
|
||||
// create Selective Search Segmentation Object using default parameters
|
||||
Ptr<SelectiveSearchSegmentation> ss = createSelectiveSearchSegmentation();
|
||||
// set input image on which we will run segmentation
|
||||
ss->setBaseImage(im);
|
||||
|
||||
// Switch to fast but low recall Selective Search method
|
||||
if (argv[2][0] == 'f') {
|
||||
ss->switchToSelectiveSearchFast();
|
||||
}
|
||||
// Switch to high recall but slow Selective Search method
|
||||
else if (argv[2][0] == 'q') {
|
||||
ss->switchToSelectiveSearchQuality();
|
||||
}
|
||||
// if argument is neither f nor q print help message
|
||||
else {
|
||||
help();
|
||||
return -2;
|
||||
}
|
||||
|
||||
// run selective search segmentation on input image
|
||||
std::vector<Rect> rects;
|
||||
ss->process(rects);
|
||||
std::cout << "Total Number of Region Proposals: " << rects.size() << std::endl;
|
||||
|
||||
// number of region proposals to show
|
||||
int numShowRects = 100;
|
||||
// increment to increase/decrease total number
|
||||
// of reason proposals to be shown
|
||||
int increment = 50;
|
||||
|
||||
while(1) {
|
||||
// create a copy of original image
|
||||
Mat imOut = im.clone();
|
||||
|
||||
// itereate over all the region proposals
|
||||
for(int i = 0; i < rects.size(); i++) {
|
||||
if (i < numShowRects) {
|
||||
rectangle(imOut, rects[i], Scalar(0, 255, 0));
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// show output
|
||||
imshow("Output", imOut);
|
||||
|
||||
// record key press
|
||||
int k = waitKey();
|
||||
|
||||
// m is pressed
|
||||
if (k == 109) {
|
||||
// increase total number of rectangles to show by increment
|
||||
numShowRects += increment;
|
||||
}
|
||||
// l is pressed
|
||||
else if (k == 108 && numShowRects > increment) {
|
||||
// decrease total number of rectangles to show by increment
|
||||
numShowRects -= increment;
|
||||
}
|
||||
// q is pressed
|
||||
else if (k == 113) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python
|
||||
'''
|
||||
Usage:
|
||||
./ssearch.py input_image (f|q)
|
||||
f=fast, q=quality
|
||||
Use "l" to display less rects, 'm' to display more rects, "q" to quit.
|
||||
'''
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, '/opt/lib/python3.5/dist-packages')
|
||||
import cv2
|
||||
|
||||
if __name__ == '__main__':
|
||||
# If image path and f/q is not passed as command
|
||||
# line arguments, quit and display help message
|
||||
if len(sys.argv) < 3:
|
||||
print(__doc__)
|
||||
sys.exit(1)
|
||||
|
||||
# read image
|
||||
im = cv2.imread(sys.argv[1])
|
||||
|
||||
# create Selective Search Segmentation Object using default parameters
|
||||
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
|
||||
|
||||
# set input image on which we will run segmentation
|
||||
ss.setBaseImage(im)
|
||||
|
||||
# Switch to fast but low recall Selective Search method
|
||||
if (sys.argv[2] == 'f'):
|
||||
ss.switchToSelectiveSearchFast()
|
||||
|
||||
# Switch to high recall but slow Selective Search method
|
||||
elif (sys.argv[2] == 'q'):
|
||||
ss.switchToSelectiveSearchQuality()
|
||||
# if argument is neither f nor q print help message
|
||||
else:
|
||||
print(__doc__)
|
||||
sys.exit(1)
|
||||
|
||||
# run selective search segmentation on input image
|
||||
rects = ss.process()
|
||||
print('Total Number of Region Proposals: {}'.format(len(rects)))
|
||||
|
||||
# number of region proposals to show
|
||||
numShowRects = 100
|
||||
# increment to increase/decrease total number
|
||||
# of reason proposals to be shown
|
||||
increment = 50
|
||||
|
||||
while True:
|
||||
# create a copy of original image
|
||||
imOut = im.copy()
|
||||
|
||||
# itereate over all the region proposals
|
||||
for i, rect in enumerate(rects):
|
||||
# draw rectangle for region proposal till numShowRects
|
||||
if (i < numShowRects):
|
||||
x, y, w, h = rect
|
||||
cv2.rectangle(imOut, (x, y), (x+w, y+h), (0, 255, 0), 1, cv2.LINE_AA)
|
||||
else:
|
||||
break
|
||||
|
||||
# show output
|
||||
cv2.imshow("Output", imOut);
|
||||
|
||||
# record key press
|
||||
k = cv2.waitKey(0) & 0xFF
|
||||
|
||||
# m is pressed
|
||||
if k == 109:
|
||||
# increase total number of rectangles to show by increment
|
||||
numShowRects += increment
|
||||
# l is pressed
|
||||
elif k == 108 and numShowRects > increment:
|
||||
# decrease total number of rectangles to show by increment
|
||||
numShowRects -= increment
|
||||
# q is pressed
|
||||
elif k == 113:
|
||||
break
|
||||
# close image show window
|
||||
cv2.destroyAllWindows()
|
||||
Reference in new issue
Block a user