OPENCV อิอิ
วันจันทร์ที่ 27 กุมภาพันธ์ พ.ศ. 2560
OPENCV : 10. Save and Read ลง xml
#include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\imgcodecs\imgcodecs.hpp>
#include <opencv2\highgui\highgui.hpp>
int main()
{
cv::namedWindow("Image");
cv::FileStorage fileStoargeWrite("test.xml" , cv::FileStorage::WRITE);
int whatever = 30;
// Save
fileStoargeWrite << "FPS" << whatever;
cv::Mat img = cv::imread("BanPong.jpg");
// Save
fileStoargeWrite << "IMG" << img;
fileStoargeWrite.release();
cv::FileStorage fileStoargeRead("test.xml" , cv::FileStorage::READ);
cv::Mat result;
// Read
fileStoargeRead["IMG"] >> result;
int fpsResult;
// Read
fileStoargeRead["FPS"] >> fpsResult;
std::cout << fpsResult;
cv::imshow("Image", result*5);
cv::waitKey(0);
fileStoargeRead.release();
return 0;
}
วันศุกร์ที่ 23 ธันวาคม พ.ศ. 2559
OPENCV : 9. Write Video
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/videoio/videoio.hpp"
int main(int argc, char** argv)
{
cv::VideoCapture videoCapture("music.mp4");
cv::Mat frame;
int countFrame = 0;
int numberOfAllFrame = videoCapture.get(CV_CAP_PROP_FRAME_COUNT);
double frameRate = videoCapture.get(CV_CAP_PROP_FPS);
std::cout << "Framerate : " << frameRate << std::endl;
cv::Mat frame_bgra;
cv::VideoWriter outputVideo("out.wmv", CV_FOURCC('W', 'M', 'V', '2'), frameRate, cv::Size(800, 600), true);
while (countFrame < numberOfAllFrame)
{
videoCapture >> frame;
cv::cvtColor(frame, frame_bgra, CV_BGR2BGRA);
// find all white pixel and set alpha value to zero:
for (int y = 0; y < frame_bgra.rows; ++y)
for (int x = 0; x < frame_bgra.cols; ++x)
{
cv::Vec4b & pixel = frame_bgra.at<cv::Vec4b>(y, x);
// if pixel is black
if (pixel[0] < 10 && pixel[1] < 10 && pixel[2] < 10)
{
// set alpha to zero:
pixel[3] = 0;
}
}
outputVideo << frame_bgra;
std::cout << "----> " << ( (float)countFrame / numberOfAllFrame ) * 100 << " %" << std::endl;
countFrame++;
}
std::cout << "OKKKKK" << std::endl;
}
OPENCV : 8. Change Black background to alpha
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc, char** argv)
{
cv::VideoCapture videoCapture("music.mp4");
cv::Mat frame;
int countFrame = 0;
int numberOfAllFrame = videoCapture.get(CV_CAP_PROP_FRAME_COUNT);
double frameRate = videoCapture.get(CV_CAP_PROP_FPS);
std::cout << "Framerate : " << frameRate << std::endl;
cv::Mat frame_bgra;
while (countFrame < numberOfAllFrame)
{
videoCapture >> frame;
cv::cvtColor(frame, frame_bgra, CV_BGR2BGRA);
// find all white pixel and set alpha value to zero:
for (int y = 0; y < frame_bgra.rows; ++y)
for (int x = 0; x < frame_bgra.cols; ++x)
{
cv::Vec4b & pixel = frame_bgra.at<cv::Vec4b>(y, x);
// if pixel is black
if (pixel[0] < 10 && pixel[1] < 10 && pixel[2] < 10)
{
// set alpha to zero:
pixel[3] = 0;
}
}
cv::imwrite("Frame" + std::to_string(countFrame) + ".png" , frame_bgra);
std::cout << "----> " << ( (float)countFrame / numberOfAllFrame ) * 100 << " %" << std::endl;
countFrame++;
}
std::cout << "OKKKKK" << std::endl;
}
วันศุกร์ที่ 16 ธันวาคม พ.ศ. 2559
OPENCV : 7. Color Detection & Object Tracking
*******************************************************************
อธิบายโค้ด
- HSV ประกอบไปด้วย 3 matrics คือ Hue , SATURATION และ VALUE
- hue มีค่าตั้งแต่ 0 - 179 ใช้สำหรับเพื่อ แยกสี
- saturation มีค่าตั้งแต่ 0 - 255 ใช้สำหรับเพื่อ แยกแสง
- value มีค่าตั้งแต่ 0 - 255 ใช้สำหรับเพื่อ แยกแสง
- หลังจาก threshold image เราจะเจอ object สีขาวบางแห่ง ในที่ต่างๆ ซึงเราสามารถกำจัดสีขาวที่ไม่จำเป็น
ได้โดย morphological opening
- Morphological opening ทำได้โดยการ erosion แล้วตามด้วย dilation ด้วย structuring element ขนาดเดียวกัน
- หลังจาก threshold image เราจะเจอ object สีขาวบางแห่ง ใน object ที่เราสนใจ ซึงเราสามารถกำจัดสีขาวที่ไม่จำเป็น
ได้โดย morphological closing
- Morphological closing ทำได้โดยการ dilation แล้วตามด้วย erosion ด้วย structuring element ขนาดเดียวกัน
- void inRange( InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst);
ถ้า lowerb เป็น Scalar(x, y, z) พิกเซลที่มีค่าน้อยกว่า x , y และ z สำหรับ hsv แล้ว มันจะมี่ค่าเป็น 0
ซึ่งก็คือ สีดำ
ถ้า upperb เป็น Scalar(x, y, z) พิกเซลที่มีค่ามากกว่า x , y และ z สำหรับ hsv แล้ว มันจะมี่ค่าเป็น 255
ซึ่งก็คือ สีขาว
- Moments moments( InputArray array, bool binaryImage=false )
อินพุต array คือ image ที่ต้องเป็น single chanel
0th order central moments of the binary image is equal to the white area of the image in pixels.
- X coordinate of the position of the center of the object = 1st order spatial moment around x-axis / 0th order central moment
- Y coordinate of the position of the center of the object = 1st order spatial moment around y-axis / 0th order central moment
- static MatExpr zeros(Size size, int type)
จะให้เป็น ภาพสีดำ
OPENCV : 6. Mouse input and special key
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
/* Example 1
if (event == cv::EVENT_LBUTTONDOWN)
{
std::cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_RBUTTONDOWN)
{
std::cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_MBUTTONDOWN)
{
std::cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_MOUSEMOVE)
{
std::cout << "Mouse move over the window - position (" << x << ", " << y << ")" << std::endl;
}
*/
if (flags == (cv::EVENT_FLAG_CTRLKEY + cv::EVENT_FLAG_LBUTTON))
{
std::cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << std::endl;
}
else if (flags == (cv::EVENT_FLAG_RBUTTON + cv::EVENT_FLAG_SHIFTKEY))
{
std::cout << "Right mouse button is clicked while pressing SHIFT key - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_MOUSEMOVE && flags == cv::EVENT_FLAG_ALTKEY)
{
std::cout << "Mouse is moved over the window while pressing ALT key - position (" << x << ", " << y << ")" << std::endl;
}
}
int main(int argc, char** argv)
{
cv::namedWindow("Original Image");
// Set callback function bind with named window
cv::setMouseCallback("Original Image", CallBackFunc, NULL);
cv::VideoCapture videoCapture("video.mp4");
cv::Mat frame;
cv::Mat hsvImage;
cv::Mat threshold;
int countFrame = 0;
int numberOfAllFrame = videoCapture.get(CV_CAP_PROP_FRAME_COUNT);
std::cout << "All frame = " << numberOfAllFrame << std::endl;
while (1)
{
if (countFrame == numberOfAllFrame)
{
countFrame = 0;
videoCapture.set(CV_CAP_PROP_POS_FRAMES , 0);
}
videoCapture >> frame;
cv::imshow("Original Image", frame);
cv::waitKey(20);
countFrame++;
}
}
วันอังคารที่ 6 ธันวาคม พ.ศ. 2559
OPENCV : 5. Videocapture with Trackbar HSV
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
// Adjist parameter
int H_LOW = 0;
int H_HIGH = 256;
int S_LOW = 0;
int S_HIGH = 256;
int V_LOW = 0;
int V_HIGH = 256;
void CallbackFunction(int h_low , void* userData)
{
}
void CreateTrackbar()
{
cv::namedWindow("Controller", 0);
cv::createTrackbar("H MIN", "Controller", &H_LOW, 256, CallbackFunction);
cv::createTrackbar("H MAX", "Controller", &H_HIGH, 256, CallbackFunction);
cv::createTrackbar("S MIN", "Controller", &S_LOW, 256, CallbackFunction);
cv::createTrackbar("S MAX", "Controller", &S_HIGH, 256, CallbackFunction);
cv::createTrackbar("V MIN", "Controller", &V_LOW, 256, CallbackFunction);
cv::createTrackbar("V MAX", "Controller", &V_HIGH, 256, CallbackFunction);
}
int main(int argc, char** argv)
{
cv::namedWindow("Original Image");
cv::namedWindow("Threshold Image");
cv::VideoCapture videoCapture("video.mp4");
cv::Mat frame;
cv::Mat hsvImage;
cv::Mat threshold;
int countFrame = 0;
int numberOfAllFrame = videoCapture.get(CV_CAP_PROP_FRAME_COUNT);
std::cout << "All frame = " << numberOfAllFrame << std::endl;
CreateTrackbar();
while (1)
{
if (countFrame == numberOfAllFrame)
{
countFrame = 0;
videoCapture.set(CV_CAP_PROP_POS_FRAMES , 0);
}
videoCapture >> frame;
cv::cvtColor(frame , hsvImage, CV_BGR2HSV);
cv::inRange(hsvImage, cv::Scalar(H_LOW, S_LOW, V_LOW), cv::Scalar(H_HIGH, S_HIGH, V_HIGH), threshold);
cv::imshow("Original Image", frame);
cv::imshow("HSV Image", hsvImage);
cv::imshow("Threshold Image", threshold);
cv::waitKey(20);
countFrame++;
}
*********************************************************************
อธิบายโค้ด
cv::createTrackbar
- พารามิเตอร์ที่ 1 : ชื่อ trackbar
- พารามิเตอร์ที่ 2 : ชื่อ Window
- พารามิเตอร์ที่ 3 : ค่าที่เรอยากจะ adjust
- พารามิเตอร์ที่ 4 : ค่าสูงสุด
- พารามิเตอร์ที่ 5 : ฟังก์ชัน callback
cv::inRange
- พารามิเตอร์ที่ 1 : ภาพ Input ชนิด Mat
- พารามิเตอร์ที่ 2 : ค่าต่ำสุด ชนิดเป็น cv::Scalar
- พารามิเตอร์ที่ 3 : ค่าสูงสุด ชนิดเป็น cv::Scalar
- พารามิเตอร์ที่ 4 : ภาพ Output ชนิด Mat
Website ตัวอย่าง OpenCV
https://github.com/muilenta/opencv_files
Machine Learning
http://www.bytefish.de/pdf/machinelearning.pdf
A Neural Network Application in OpenCV
http://picoledelimao.github.io/blog/2016/01/31/is-it-a-cat-or-dog-a-neural-network-application-in-opencv/
สมัครสมาชิก:
บทความ (Atom)