###### tags: `OpenCV`,`影像處理`,`素描照` # OpenCV 實作篇-利用CV2來畫個半邊素描照吧 ```python= #讀進照片 import cv2 img= cv2.imread('pregnant.jpg') #使用我懷孕時的照片 cv2.imshow("pregnant",img) cv2.waitKey(0) cv2.destroyAllWindows() #照片尺寸 img.shape #(627, 418, 3) ``` ![](https://i.imgur.com/8Pli17W.jpg) ```python= #取右半部 cv2.imshow("pregnant_half",img[:,209:,:]) cv2.waitKey(0) cv2.destroyAllWindows() ``` ![](https://i.imgur.com/ISsz8FI.jpg) ```python= #開始處理 img_half=img[:,209:,:] gray_image = cv2.cvtColor(img_half, cv2.COLOR_RGB2GRAY) #右邊一半圖形先轉變灰階 inv_gray = 255 - gray_image #再將色調改變 (黑變白,白變黑) blurred_image = cv2.GaussianBlur(inv_gray, (13, 13), 0, 0) #利用高斯模糊調整一下(搭配模糊降噪效果較佳) gray_sketch = cv2.divide(gray_image, 255 - blurred_image, scale=256) #針對右半部的gary_image去做divide gray_sketch =cv2.cvtColor(gray_sketch, cv2.COLOR_GRAY2BGR)#再將grayimage還原回BGR img[:,209:,:]=gray_sketch #將照片再結合回去 cv2.imshow("pregnant_finish",img) cv2.waitKey(0) cv2.destroyAllWindows() ``` ![](https://i.imgur.com/E5AOMij.jpg) conference: <https://docs.opencv.org/3.4/d2/de8/group__core__array.html#ga6db555d30115642fedae0cda05604874> **divide(src1, src2, dst=None, scale=None, dtype=None)** **src1:** first input array. **src2:** second input array of the same size and type as src1. **scale:** scalar factor. **dst:** output array of the same size and type as src2. dtype,optional depth of the output array; if -1, dst will have depth src2.depth(), but in case of an array-by-array division, you can only pass -1 when src1.depth()==src2.depth(). The function cv::divide divides one array by another: dst(I) = saturate(src1(I)*scale/src2(I)) or a scalar by an array when there is no src1 : dst(I) = saturate(scale/src2(I)) When src2(I) is zero, dst(I) will also be zero. Different channels of multi-channel arrays are processed independently. :star:補充說明 ```python= inv_gray = 255 - gray_image cv2.imshow("gray_image",gray_image) cv2.imshow("inv_gray",inv_gray) cv2.waitKey(0) cv2.destroyAllWindows() #暗的部分變亮 亮的部分變暗 #這就是要把我們圖裡面的邊框給選出來 ``` ![](https://i.imgur.com/4KabDjx.jpg)