# [20190602 - Raspberry Pi](https://hackmd.io/s/SkgLJCCnV) ###### tags: `tcfst` ## Author - Yuan < e61983@gmail.com > - Wei < demon0303loki@gmail.com > - Jia-Hao,Hsu < jhs.8901.3737@gmail.com > ## License ![cc by-nc-sa](https://licensebuttons.net/l/by-nc-sa/3.0/88x31.png) ## OpenCV ### Install ```shell= pip install opencv-python pip install opencv-contrib-python ``` ### Example ```python= import cv2 img = cv2.imread('messi5.jpg',0) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() ``` ### Pkg-Config ```shell= pkg-config --cflags pkg-config --libs pkg-config --static ``` #### Check Version ``` pkg-config --modversion opencv ``` #### Reference - [pkg-config 教學][pkg-conifg 教學] ## OpenCV - USB CAM ### Install - Python library ```shell= sudo apt install python-opencv ``` - C library ```shell= sudo apt install libopencv-dev ``` ### Example - Using C++ ```cpp= #include "opencv2/opencv.hpp" using namespace cv; int main(int, char**) { VideoCapture cap(0); // open the default camera if(!cap.isOpened()) // check if we succeeded return -1; Mat edges; namedWindow("edges",1); for(;;) { Mat frame; cap >> frame; // get a new frame from camera cvtColor(frame, edges, COLOR_BGR2GRAY); GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5); Canny(edges, edges, 0, 30, 3); imshow("edges", edges); if(waitKey(30) >= 0) break; } // the camera will be deinitialized automatically in VideoCapture destructor return 0; } ``` - Using Python ```python= import cv2 cap = cv2.VideoCapture(0) while(True): ret, frame = cap.read() cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ``` #### Reference - [Official][Official-opencv] ## Canny-Edge Detection - Introduce ``` 1. Noise Reduction 2. Finding Intensity Gradient of the Image 3. Non-maximum Suppression 4. Hysteresis Thresholding ``` - Using Python ```python= import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('messi5.jpg',0) edges = cv2.Canny(img,100,200) plt.subplot(121),plt.imshow(img,cmap = 'gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(edges,cmap = 'gray') plt.title('Edge Image'), plt.xticks([]), plt.yticks([]) plt.show() ``` - Using C++ ```cpp= #include <cstdio> #include <opencv2/opencv.hpp> using namespace cv; int main(){ Mat src = imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE); GaussianBlur(src, src, Size(3,3), 0, 0); Mat dst1, dst2; Canny(src, dst1, 50, 150, 3); threshold(dst1, dst2, 128, 255, THRESH_BINARY_INV); imshow("origin", src); imshow("Canny_1", dst1); imshow("Canny_2", dst2); waitKey(0); return 0; } ``` ## Image Filtering ### Filter2D() OpenCV 提供 filter2D() 函式方便濾波計算,我們依濾波值定義一個核矩陣,之後呼叫 filter2D 函式時輸入此核心矩陣當參數。 - C language ```c= void cvFilter2D( const CvArr* src, // 輸入 CvArr* dst, // 輸出 const CvMat* kernel, // 核心 CvPoint anchor=cvPoint(-1,-1) // 錨點 ) ``` - python ```python= cv.Filter2D(src, dst, kernel, anchor=(-1, -1)) ``` ### Kernel 本質上是固定大小的數值係數陣列以及該陣列中的錨點,其通常位於中心。 我們可以藉由此陣列來進行線性慮波。 ![](https://i.imgur.com/si2CQOK.png) #### Reference - [Making your own linear filters!][opencv-kernel] ### Blurring - Using Python ```python= import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('opencv_logo.png') kernel = np.ones((5,5),np.float32)/25 dst = cv2.filter2D(img,-1,kernel) plt.subplot(121),plt.imshow(img),plt.title('Original') plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(dst),plt.title('Averaging') plt.xticks([]), plt.yticks([]) plt.show() ``` ### Sharpen - Using C++ ```cpp= #include <cstdio> #include <opencv2/opencv.hpp> using namespace cv; void sharpen(const Mat &src, Mat &dst) { Mat kernel(3,3,CV_32F,Scalar(0)); kernel.at<float>(1,1) = 5.0; kernel.at<float>(0,1) = -1.0; kernel.at<float>(2,1) = -1.0; kernel.at<float>(1,0) = -1.0; kernel.at<float>(1,2) = -1.0; filter2D(src,dst,src.depth(),kernel); } int main() { Mat src = imread("lena.jpg",CV_LOAD_IMAGE_UNCHANGED); Mat dst; sharpen(src, dst); namedWindow("window1"); imshow("window1", src); imshow("window2", dst); waitKey(0); return 0; } ``` ### Reference - [Official Document - Image Filter][opencv-image-filter] ## Morphological (形態學) - Erode (浸蝕) ```python= import cv2 import numpy as np img = cv2.imread('j.png',0) kernel = np.ones((5,5),np.uint8) erosion = cv2.erode(img,kernel,iterations = 1) ``` - Dilate (膨脹) ```python= import cv2 import numpy as np img = cv2.imread('j.png',0) kernel = np.ones((5,5),np.uint8) dilation = cv2.dilate(img,kernel,iterations = 1) ``` - open 先 Erode (浸蝕) 再 Dilate (膨脹),用來消除噪點。 ```python= import cv2 import numpy as np img = cv2.imread('j.png',0) kernel = np.ones((5,5),np.uint8) opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) ``` - close 與 open 相反,先 Dilate (膨脹) 再 Erode (浸蝕),用來消除物件內的噪點。 ```python= import cv2 import numpy as np img = cv2.imread('j.png',0) kernel = np.ones((5,5),np.uint8) closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) ``` ### Reference - [opencv-morphological API] ## Color Detection - [opencv-color API] ## Find contours - [opencv-findContours API] ## 梗區 >高斯濾波是低通濾波 >[name= 李老師] >> 其實沒錯啦 >> Since the Fourier transform of a Gaussian is another Gaussian, applying a Gaussian blur has the effect of reducing the image's high-frequency components; a Gaussian blur is thus a low pass filter. >作業一: 使用openCV 設計一個追蹤三個顏色的程式~~ >[name=~~Linux Driver課程~~] ## 場外 LOL 這裡有筆記機器人 打完標題 內容就會自己產生!! 新的 人工智慧!! 太神啦! 聽他講影像辨識 讓我想起了阿烈跟世勳的好 他的教材我都還有留著XD +1 雖然阿烈的版本沒有教材XD > 大會廣播: 筆記機器人服務將於30分鐘後結束 > 我看成教育機器人 > 延長三小時要付費多少 其實可以先只放標題~~ 內容什麼的~~彈個手指~~就有了XD 原來是個姓薩的機器人還是姓道尼的 <img style="width:40%; height:auto;" src="https://i.imgur.com/YIJlRYc.jpg" > >~~少三顆寶石 抓到!!!~~ 少四顆才對 大拇指不知道有沒有XD >>所以才會暫停服務啊~XDD >是說~~只有Tony 才有機器人啊!! 所以 老賈 是你!! 剛好還有一顆在頭上 變成三顆寶石了XD [pkg-conifg 教學]: https://jyhshin.pixnet.net/blog/post/26588033 [Official-opencv]: https://docs.opencv.org/3.0-beta/modules/videoio/doc/reading_and_writing_video.html [opencv-color API]: https://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor [opencv-morphological API]: https://docs.opencv.org/3.0-beta/modules/imgproc/doc/filtering.html#dilate [opencv-findContours API]: https://docs.opencv.org/3.0-beta/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcon#cv2.findContours [opencv-image-filter]: https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html [OpenCV-Document]: https://docs.opencv.org/3.3.1/d4/d73/tutorial_py_contours_begin.html [opencv-kernel]: https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html