# Python學習筆記 0004 (在輸出視窗畫線、圓、文字、調整顏色) ## 抓取(Cropped) ```javascript= import cv2 frameWidth = 400 frameHeight = 400 img = cv2.imread("img7.jpg") img = cv2.resize(img, (frameWidth, frameHeight)) imgCropped = img[0:270,100:380] cv2.imshow("img", img) cv2.imshow("Cro", imgCropped) cv2.waitKey(0) ``` ![Uploading file..._uvi9rj790]() - img[0:270,100:380] 0,0是在最左上角 0:270左上往下數270pixel - 100:380是 100是最左上往右數100pixel 380是最左上往右數380pixel ## 外框shape ```javascript= import cv2 frameWidth = 300 frameHeight = 400 img = cv2.imread("img7.jpg") img = cv2.resize(img, (300, 400)) imgCropped = img[0:270, 100:280] imgCroResize = cv2.resize(imgCropped, (img.shape[0],img.shape[1])) print(img.shape) print(imgCropped.shape) print(imgCroResize.shape) cv2.imshow("img", img) cv2.imshow("Cro", imgCropped) cv2.imshow("CroRe", imgCroResize) ``` ![Uploading file..._j89pv7vya]() - print out (400, 300, 3) (270, 180, 3) (300, 400, 3) imgCropped抓取img後、再被放入imgCroResize imgCroResize的外框的資料(frameWidth, frameHeight) 抓取(400, 300, 3) shape[0]=400 shape[1]=300 (imgCropped, (img.shape[0],img.shape[1])=(imgCropped, (400,300) - 程式碼 跟 印出的shape的 寬高順位不同 程式碼(300, 400) 寬300 高400 印出shape(400, 300) 高400 寬300 - (400, 300, 3)的3是? 3是代表RGB的意思 ### 例子1 ```javascript= import cv2 import numpy as np img1 = np.zeros((2, 2, 3)) print(img1) print(img1.shape) cv2.imshow("img", img1) cv2.waitKey(0) ``` - 輸出 [[[0. 0. 0.] [0. 0. 0.]] [[0. 0. 0.] [0. 0. 0.]]] (2, 2, 3) - [0. 0. 0.]那個點代表浮點數 由於我們不要小數點要整數(integer) 所以用uint8 - img1 = np.zeros((2, 2, 3)) 2,2表示長寬為2 ### 例子2 ```javascript= import cv2 import numpy as np img1 = np.zeros((2, 2, 3), np.uint8) print(img1) print(img1.shape) cv2.imshow("img", img1) cv2.waitKey(0) ``` - 輸出 [[[0 0 0] [0 0 0]] [[0 0 0] [0 0 0]]] (2, 2, 3) 小數點沒有了 ### 例子3 藍色uint ```javascript= import cv2 import numpy as np img1 = np.zeros((200, 200, 3), np.uint8) img1[:] = 255, 0, 0 img2 = np.zeros((200, 200, 3), np.uint8) img2[:] = 65535, 0, 0 img3 = np.zeros((200, 200, 3), np.uint16) img3[:] = 255, 0, 0 img4 = np.zeros((200, 200, 3), np.uint16) img4[:] = 65535, 0, 0 print(img4) print(img4.shape) cv2.imshow("img1", img1) cv2.imshow("img2", img2) cv2.imshow("img3", img3) cv2.imshow("img4", img4) cv2.waitKey(0) ``` ![](https://i.imgur.com/VsZlCSe.png) - 8位元與16位元 ## 填滿的矩形 ```javascript= import cv2 import numpy as np img1 = np.zeros((200, 200, 3), np.uint8) img1[20:50,60:100] = 0, 0, 255 cv2.imshow("img1", img1) cv2.waitKey(0) ``` ![](https://i.imgur.com/TdtogLx.png) - img1[] = 0, 0, 255順序表示 B G R ## 畫線 ```javascript= import cv2 import numpy as np img1 = np.zeros((200, 200, 3), np.uint8) img1[20:50,60:100] = 0, 0, 255 cv2.line (img1, (0, 0), (img1.shape[1], img1.shape[0]), (0, 255, 0), 3) cv2.imshow("img1", img1) cv2.waitKey(0) ``` ![](https://i.imgur.com/BReepYX.png) - cv2.line (img1, (0, 0), (img1.shape[1], img1.shape[0]), (0, 255, 0), 3) img1來源 (0, 0)線的開頭 (img1.shape[1], img1.shape[0])線的結尾 (0, 255, 0)顏色 3 粗細 ## 畫邊的矩形(cv2.rectangle) ```javascript= import cv2 import numpy as np img1 = np.zeros((200, 200, 3), np.uint8) img1[20:50,60:100] = 0, 0, 255 cv2.line (img1, (0, 0), (200, 200), (0, 255, 0), 3) cv2.rectangle (img1, (80, 170), (20, 100),( 50, 10, 100),8) cv2.imshow("img1", img1) cv2.waitKey(0) ``` ![](https://i.imgur.com/2KV7OoP.png) ## 填滿(cv2.FILLED) ```javascript= cv2.rectangle (img1, (150, 180), (100, 120),( 100, 200, 10),cv2.FILLED) ``` ![](https://i.imgur.com/hJPHHma.png) 會覆蓋前面所輸入的線 加一段 ## 圓形和文字 ```javascript= cv2.circle (img1, (99, 122), 90,(33, 66, 166),2) cv2.putText (img1, "HAHAHA DODODO", (0, 270),cv2.FONT_HERSHEY_COMPLEX, 1,(23, 60, 100)) ``` ![](https://i.imgur.com/6zP4VMp.png) - cv2.circle 定義 def circle(img, center, radius, color, thickness=None, lineType=None, shift=None) - cv2.putText定義 def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None) :::spoiler 一篇一個OpenCV函式(destroyWindow) cv2.destroyWindow 原型: destroyWindow(winname) 作用:關閉一個由imshow產生的影象視窗 引數:winname,關閉的視窗名字 cv2.destroyAllWindows 原型:destroyAllWindows() 作用:關閉所有由imshow產生的視窗 https://www.itread01.com/content/1550310670.html ::: :::spoiler 一篇一個numpy函式(partition) np.partition(array, kth) 以 kth 為標準,排序後返回陣列。 ```javascript= import numpy as np kth = 4 a = np.array([9, 1, 4, 0, 5, 9, 0, 2, 6, 0]) print("Hello kth = " + str(kth)) # Partition print("Hello before partition a = " + str(a)) print("Hello after- partition a = " + str(np.partition(a, kth))) ``` - kth = 4 輸出 Hello kth = 4 Hello before partition a = [9 1 4 0 5 9 0 2 6 0] Hello after partition a = [1 0 0 0 2 4 5 9 6 9] - kth = 7 輸出 Hello kth = 7 Hello before partition a = [9 1 4 0 5 9 0 2 6 0] Hello after- partition a = [0 0 4 0 1 2 5 6 9 9] - kth = 1 輸出 Hello kth = 1 Hello before partition a = [9 1 4 0 5 9 0 2 6 0] Hello after- partition a = [0 0 4 9 5 9 1 2 6 0] - kth = 1,在陣列 a0、a1的值要跟1做比較,找到比1還小的值就要交換 這裡的a0是=9 ,9>=1 然後找到了a3=0,a0和a3交換了位置 這裡的a1是=1 ,1>=1 然後找到了a6=0,a0和a3交換了位置 程式執行結束 https://hackmd.io/@yillkid/rJp5h90s8/https%3A%2F%2Fhackmd.io%2F%40yillkid%2FHy5a_5A-F#array ::: ###