2013年12月29日 星期日

[openCV]邊緣偵測

引用標頭檔morphoFeatures

  • #include "morphoFeatures.h"

假想:宣告邊緣影像處理工廠物件

  • MorphoFeatures morpho;

設定邊緣影像處理工廠處理程度參數

  • morpho.setThreshold(40);

將影像丟進工廠處理

  • cv::Mat edges;
  • edges= morpho.getEdges(image);
結果




















程式碼Webcam套上邊緣偵測

  • https://drive.google.com/file/d/0B3iLa8F71LQwV0pfRzlXSDRzN3c/edit?usp=sharing


Reference

  • 孫士韋老師課堂六

[openCV]讀取影片


宣告影像資料結構

  • CvCapture capture;

將影片讀進影像資料結構

  • 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


[openCV] 偵測像素變化值控制LED亮暗

在影像定義ROI並上貼上開關圖示

  1. cv::Mat lightImg = cv::imread("lightOn.jpg");
  2. cv::Mat lightImg_2 = cv::imread("lightOff.jpg");
  3. cv::Mat lightOnROI,
  4. cv::Mat lightOffROI;
  5. lightOnROI = image(cv::Rect(0,0,lightImg.size().width,lightImg.size().height));
  6. lightOffROI = image(cv::Rect(image.size().width-lightImg.size().width,0,lightImg.size().width,lightImg.size().height));
  7. cv::addWeighted(lightOnROI,1.0,lightImg,1.0,0.,lightOnROI);
  8. cv::addWeighted(lightOffROI,1.0,lightImg_2,1.0,0.,lightOffROI);



掃左上角4x4 pixels(可擴充),看看每個值是不是有變化到某個程度  lightThreshold

  1. for(int y=0;y<2;y++)
  2. {
  3. for(int x=0;x<2;x++)
  4. {
  5. /cout<<y<<","<<x<<","<<z<<endl;
  6. if(abs(nowValue[z]-tempValue[z])>lightThreshold)   //light 門檻
  7. {
  8. //cout<<"LightOn"<<endl;
  9. tempValue[z] = nowValue[z];
  10. lightState = 1;
  11. }
  12. int redValue = image.at<cv::Vec3b>(y,x)[0];
  13. int greenValue = image.at<cv::Vec3b>(y,x)[1];
  14. int blueValue = image.at<cv::Vec3b>(y,x)[2];
  15. nowValue[z++] = redValue + greenValue + blueValue;
  16. //cout<<"tempValue:"<<tempValue[z]<<","<<"nowValue:"<<nowValue[z]<<endl;
  17. if(z>3)
  18. {
  19. z=0;
  20. }
  21. }
  22. }



觸碰左右圖示即改變像素值,控制LED亮暗





























arduino端接收

  • if(Serial.available()>0)
  • lightState = Serial.read();



程式碼


  •  https://drive.google.com/file/d/0B3iLa8F71LQwemVPNm1LUE96SW8/edit?usp=sharing





[openCV] 連結Arduino using Visual C++

要用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

  1. try
  2. {
  3. ....
  4. }
  5. catch (IO::IOException^ e  ) 
  6. Console::WriteLine(e->GetType()->Name+": Port is not ready");
  7. }
  8. catch (ArgumentException^ e)
  9. {
  10. Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
  11. }

打開SerialPort

  • arduino->Open();

傳送字串

  • arduino->WriteLine(String);

關閉SerialPort

  • arduino->Close();
Reference












[openCV]Webcam

宣告視訊裝置

  • CvCapture *capture;

指定由哪個視訊裝置輸入

  • 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