1、首先创建个简单的图片,大家都说会画圆形和矩形就可以画任意形状,因为我们可以对图片进行进行组合和按位操作
import cv2
import numpy as np
square = np.zeros((300,300),np.uint8)
cv2.rectangle(square,(50,50),(250,250),128,-2)
cv2.imshow("peng",square)
cv2.waitKey(0)
#画图
ellipse = np.zeros((300,300),np.uint8) cv2.ellipse(ellipse,(150,150),(150,150),30,0,180,255,-2)
cv2.imshow("ellipse",ellipse)
cv2.waitKey(0)
cv2.destroyAllWindows()
![](https://img.haomeiwen.com/i1327433/e96d171ed055d841.png)
![](https://img.haomeiwen.com/i1327433/955f3435a96cdfef.png)
2、现在开始对图片进行按位操作,首先看And操作
And = cv2.bitwise_and(square,ellipse)
cv2.imshow("And",And)
cv2.waitKey(0)
cv2.destroyAllWindows()
![](https://img.haomeiwen.com/i1327433/34815985db30374c.png)
3、or
bitwiseOR = cv2.bitwise_or(square,ellipse)
cv2.imshow("OR",bitwiseOR)
cv2.waitKey(0)
cv2.destroyAllWindows()
![](https://img.haomeiwen.com/i1327433/5477172c1ae42130.png)
4、NOT,取反
bitwiseNOT = cv2.bitwise_not(square)
cv2.imshow("not",bitwiseNOT)
cv2.waitKey(0)
cv2.destroyAllWindows()
![](https://img.haomeiwen.com/i1327433/0e075bd70ece8116.png)
5、XOR两个图形的非交互处
bitwiseXOR = cv2.bitwise_xor(square,ellipse)
cv2.imshow("XOR",bitwiseXOR)
cv2.waitKey(0)
cv2.destroyAllWindows()
![](https://img.haomeiwen.com/i1327433/24b4693bcd8f84db.png)
网友评论