引用標頭檔morphoFeatures
- #include "morphoFeatures.h"
假想:宣告邊緣影像處理工廠物件
設定邊緣影像處理工廠處理程度參數
將影像丟進工廠處理
- cv::Mat edges;
- edges= morpho.getEdges(image);
結果
程式碼Webcam套上邊緣偵測
- https://drive.google.com/file/d/0B3iLa8F71LQwV0pfRzlXSDRzN3c/edit?usp=sharing
Reference
宣告影像資料結構
將影片讀進影像資料結構
- char VideoFileName[]="test.mp4";
- capture = cvCaptureFromAVI("檔名.mp4"); (不一定只能mp4檔)
從影像資料結構中得到總共有多少frame
- int frame = cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
從影像資料結構中得到 fps: frame per second
- int fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
宣告視窗準備將frame丟到視窗上,參數0:可以自由調整視窗大小
- cv::namedWindow("Video Player",0);
判斷影像資料結構中是否有frame可以擷取
- cvGrabFrame(capture)-->ture/false
從影像資料結構中擷取一個一個的frame
- frame = cvRetrieveFrame(capture);
擷取到的frame丟到視窗上
- cv::imshow("Video Player",frame);
結束後釋放記憶體
- cvReleaseCapture(&capture);
- cvDestroyWindow("Video Player");
程式碼
- https://drive.google.com/folderview?id=0B3iLa8F71LQwRnBiM1RLQnQ4UWM&usp=sharing
Reference
在影像定義ROI並上貼上開關圖示
- cv::Mat lightImg = cv::imread("lightOn.jpg");
- cv::Mat lightImg_2 = cv::imread("lightOff.jpg");
- cv::Mat lightOnROI,
- cv::Mat lightOffROI;
- lightOnROI = image(cv::Rect(0,0,lightImg.size().width,lightImg.size().height));
- lightOffROI = image(cv::Rect(image.size().width-lightImg.size().width,0,lightImg.size().width,lightImg.size().height));
- cv::addWeighted(lightOnROI,1.0,lightImg,1.0,0.,lightOnROI);
- cv::addWeighted(lightOffROI,1.0,lightImg_2,1.0,0.,lightOffROI);
掃左上角4x4 pixels(可擴充),看看每個值是不是有變化到某個程度 lightThreshold
- for(int y=0;y<2;y++)
- {
- for(int x=0;x<2;x++)
- {
- /cout<<y<<","<<x<<","<<z<<endl;
- if(abs(nowValue[z]-tempValue[z])>lightThreshold) //light 門檻
- {
- //cout<<"LightOn"<<endl;
- tempValue[z] = nowValue[z];
- lightState = 1;
- }
- int redValue = image.at<cv::Vec3b>(y,x)[0];
- int greenValue = image.at<cv::Vec3b>(y,x)[1];
- int blueValue = image.at<cv::Vec3b>(y,x)[2];
- nowValue[z++] = redValue + greenValue + blueValue;
- //cout<<"tempValue:"<<tempValue[z]<<","<<"nowValue:"<<nowValue[z]<<endl;
- if(z>3)
- {
- z=0;
- }
-
- }
- }
觸碰左右圖示即改變像素值,控制LED亮暗
arduino端接收
- if(Serial.available()>0)
- lightState = Serial.read();
程式碼
- https://drive.google.com/file/d/0B3iLa8F71LQwemVPNm1LUE96SW8/edit?usp=sharing
要用CLR主控台應用程式,非Win32主控台應用程式,這樣才可以用System命名空間
- using namespace System;
- using namespace System::IO::Ports;
宣告comport,並指定portName與baudrate
- int baudRate = 9600;
- String^ portName = "COM5";
- SerialPort^ arduino;
- arduino = gcnew SerialPort(portName, baudRate);
用try catch處理Serialport
- try
- {
- ....
- }
- catch (IO::IOException^ e )
- {
- Console::WriteLine(e->GetType()->Name+": Port is not ready");
- }
- catch (ArgumentException^ e)
- {
- Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
- }
打開SerialPort
傳送字串
- arduino->WriteLine(String);
關閉SerialPort
Reference
宣告視訊裝置
指定由哪個視訊裝置輸入
- capture = cvCaptureFromCAM(0);
- 0:代表自動偵測視訊裝置,如果有兩台以上的Webcam就用兩次
將捕捉到的影像擷取丟到image上再show到window上
- cv::Mat image = cvQueryFrame(capture);
- cv::namedWindow("Webcam");
- cv::imshow("Webcam",image);
釋放記憶體
- cvReleaseCapture(capture);
- cvDestroyWindow("Webcam");
Reference