Skip to content

Instantly share code, notes, and snippets.

@reiji1020
Last active January 20, 2016 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reiji1020/eb9cf1bced677a8d94a7 to your computer and use it in GitHub Desktop.
Save reiji1020/eb9cf1bced677a8d94a7 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/opencv_lib.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(void) {
uchar hue, sat, val;
Mat src_video(Size(640, 480), CV_8UC1, Scalar::all(255));
Mat smooth_video(Size(640, 480), CV_8UC1, Scalar::all(255));
Mat dst_img(Size(640, 480), CV_8UC1, Scalar::all(0));
Mat hsv_video(Size(640, 480), CV_8UC1, Scalar::all(255));
Mat frame(Size(640, 480), CV_8UC1, Scalar::all(255));
VideoCapture capture(0);
// カメラが使えない場合はプログラムを止める
if (!capture.isOpened())
return -1;
// ウィンドウを作成する
char windowName[] = "カメラでさるくマップを撮影してね!";
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
char hsvwindow[] = "HSV変換結果";
namedWindow(hsvwindow, CV_WINDOW_AUTOSIZE);
char dstwindow[] = "認識結果";
namedWindow(dstwindow, CV_WINDOW_AUTOSIZE);
// 何かキーが押下されるまで、ループをくり返す
while (cvWaitKey(1) == -1)
{
dst_img = Scalar::all(0);
// カメラから1フレーム取得する
do{
capture >> frame;
} while (frame.empty());
src_video = frame;
imshow(windowName, src_video);
// HSV表色系へ色情報を変換
// 先にノイズを消しておく
medianBlur(src_video, smooth_video, 5);
cvtColor(smooth_video, hsv_video, CV_BGR2HSV);
imshow(hsvwindow, hsv_video);
// H,S,Vの要素に分割する
for (int y = 0; y < hsv_video.rows; y++) {
for (int x = 0; x < hsv_video.cols; x++) {
hue = hsv_video.at<Vec3b>(y, x)[0];
sat = hsv_video.at<Vec3b>(y, x)[1];
val = hsv_video.at<Vec3b>(y, x)[2];
// 居留地マップの検出
if ((hue < 35 && hue > 20) && sat > 127) {
dst_img.at<uchar>(y, x) = 255;
}
else {
dst_img.at<uchar>(y, x) = 0;
}
}
}
imshow(dstwindow, dst_img);
}
destroyAllWindows();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment