春休みの自由研究として固定抵抗の物体検出をOpenCVでやろうとしたときに書いたスクリプトの中から、将来的に再び使用することになるであろうものをとりあえず残しておいたもの。
Created
April 29, 2014 04:06
-
-
Save arosh/11390420 to your computer and use it in GitHub Desktop.
object-detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
def main(): | |
ok_file = 'OK.txt' | |
with open(ok_file) as f: | |
k = 0 | |
for line in f: | |
seq = line.split() | |
imgname = seq[0] | |
im = Image.open(imgname) | |
numobj = int(seq[1]) | |
for i in xrange(numobj): | |
x, y, w, h = map(int, seq[2+i*4:2+i*4+4]) | |
box = (x, y, x+w, y+h) | |
im.crop(box).save('OK_samples/{0}.jpg'.format(k)) | |
k += 1 | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <vector> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/objdetect/objdetect.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
int main(int argc, const char *argv[]) { | |
const char *exename = *argv; | |
--argc; ++argv; | |
if(argc == 0) { | |
fprintf(stderr, "usage: %s <img>", exename); | |
exit(EXIT_FAILURE); | |
} | |
const char *fname = *argv; | |
--argc; ++argv; | |
cv::Mat img = cv::imread(fname); | |
if(img.empty()) { | |
perror(fname); | |
exit(EXIT_FAILURE); | |
} | |
const char *cascadeName = "./resistor_cascade.xml"; | |
cv::CascadeClassifier cascade; | |
if(!cascade.load(cascadeName)) { | |
perror(cascadeName); | |
exit(EXIT_FAILURE); | |
} | |
cv::Mat gray; | |
cv::cvtColor(img, gray, CV_RGB2GRAY); | |
std::vector<cv::Rect> rects; | |
cascade.detectMultiScale(gray, rects); | |
for (const cv::Rect &rect : rects) { | |
cv::rectangle(img, rect, cv::Scalar(255, 0, 0)); | |
} | |
const char *windowName = "window"; | |
cv::namedWindow(windowName, CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO); | |
cv::imshow(windowName, img); | |
cv::waitKey(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
int main(int argc, const char *argv[]) { | |
--argc; ++argv; | |
const char *fname = "image.jpg"; | |
if (argc > 0) { | |
fname = *argv; | |
--argc; ++argv; | |
} | |
cv::Mat src = cv::imread(fname); | |
if(src.empty()) { | |
perror(fname); | |
exit(EXIT_FAILURE); | |
} | |
cv::Mat gray; | |
cv::cvtColor(src, gray, CV_RGB2GRAY); | |
cv::Mat dst; | |
cv::equalizeHist(gray, dst); | |
const char *src_window = "src"; | |
const char *dst_window = "dst"; | |
cv::namedWindow(src_window); | |
cv::imshow(src_window, gray); | |
cv::namedWindow(dst_window); | |
cv::imshow(dst_window, dst); | |
cv::waitKey(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import os | |
import shutil | |
files = glob.glob('**/*.jpg') | |
for f in files: | |
# print f, os.path.join('~/resistor/NG', os.path.dirname(f) + '-' + os.path.basename(f)) | |
shutil.move(f, os.path.join('/home/arosh/resistor/NG', os.path.dirname(f) + '-' + os.path.basename(f))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
import numpy as np | |
from PIL import Image | |
def main(): | |
images = glob.glob('OK_samples/*.jpg') | |
k = 0 | |
for fname in images: | |
im = np.array(Image.open(fname).convert('L')) | |
edge = [] | |
for y in xrange(im.shape[0]): | |
edge.append(im[y,0]) | |
edge.append(im[y,-1]) | |
for x in xrange(1,im.shape[1]-1): | |
edge.append(im[0,x]) | |
edge.append(im[-1,x]) | |
edge = np.array(edge) | |
median = int(np.median(edge)) | |
thresh = min(int(np.std(edge)), 20) | |
command = 'opencv_createsamples -img {} -vec OK_vecs/{}.vec -bg NG.txt -num 40 -bgcolor {} -bgthresh {}'.format(fname, k, median, thresh) | |
print command | |
k += 1 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment