赞
踩
实验大作业所需理论部分 :大作业实验openCV-数字图像处理(所需理论)
代码如下(示例):
- #关键API cv2.cvtColor
- import cv2
-
- def callback(value):
- pass
-
- cv2.namedWindow('color',cv2.WINDOW_NORMAL)
- cv2.resizeWindow('color',700,680)
-
- img = cv2.imread('./pic/cat.png')
-
- #定义颜色空间
- colorspaces = [
- cv2.COLOR_BGR2RGBA,cv2.COLOR_BGR2BGRA,
- cv2.COLOR_BGR2GRAY,cv2.COLOR_BGR2HSV,
- cv2.COLOR_BGR2YUV
- ]
-
- #设置trackbar
- cv2.createTrackbar('trackbar','color',0,4, callback)
-
- cv2.putText(img,'cute cat',(30,300), cv2.FONT_HERSHEY_COMPLEX_SMALL,3,[0,255,0])
- while True:
- index = cv2.getTrackbarPos('trackbar','color')
- cvt_img = cv2.cvtColor(img, colorspaces[index])
- cv2.imshow('color',cvt_img)
-
- key = cv2.waitKey(10)
- if key == ord('a'):
- break
- cv2.destroyAllWindows()
理论部分:
B复制A,A变,B变,浅拷贝。 (相当于共享出去的)
B复制A,A变,B不变,深拷贝。 (独立复制出去的)
代码如下(示例):
- #CV用的mat数据结构,
- #mat是C++用的,python封装了一层,numpy的darray
-
- import cv2
- import numpy as np
-
- img = cv2.imread('./pic/cat.png')
-
- #浅拷贝
- img2 = img.view()
-
- #深拷贝
- img3 = img.copy()
-
- img [10:100,10:100] = [0,0, 255]
-
- cv2.imshow('img',np.hstack((img,img2,img3)))
-
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
-
- img = cv2.imread('./pic/cat.png')
-
- b,g,r = cv2.split(img)
-
- b[10:100,10:100] = 255
- g[10:100,10:100] = 255
-
- img2 = cv2.merge((b, g, r))
-
- cv2.imshow('img',img)
- cv2.imshow('b',b)
- cv2.imshow('g',g)
- cv2.imshow('r',r)
- cv2.imshow('img2',img2)
-
- cv2.waitKey(0)
- cv2.destroyAllWindows()
我们能看出每个色域会有区别,明亮程度
- #直线,矩形cv2.line()
-
- import cv2
- import numpy as np
-
- img = np.zeros((480,640,3),np.uint8)
-
- # img,初始点,结束点,颜色,。。
- cv2.line(img,(10,20),(10,400),(0,0,255),5,4)
- cv2.line(img,(10,400),(300,400),(0,0,255),5,4)
- cv2.line(img,(300,400),(300,20),(0,0,255),5,4)
- cv2.line(img,(300,20),(10,20),(0,0,255),5,4)
-
- # img,初始点,对角点,颜色,。。
- cv2.rectangle(img,(30,40),(200,300),(0,255,0),5,4)
-
- cv2.imshow('draw',img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- #圆
- import cv2
- import numpy as np
-
- img = np.zeros((480,640,3),np.uint8)
- # img,圆心,半径,颜色,。。
- cv2.circle(img,(320,240),100,(0,0,255),5,4)
- # img,圆心,,椭圆的a,b,角度,起始角度,结束角度,颜色,。。
- cv2.ellipse(img,(320,240),(100,50), 0, 0, 360, [0,0,255],5,16)
-
- cv2.circle(img,(320,240),45,(0,255,0),5,4)
-
- cv2.imshow('draw',img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- #多边形
- import cv2
- import numpy as np
-
- img = np.zeros((480,640,3),np.uint8)
-
- #把每个点作为,数组一部分
- pts1 = np.array([[(100,100),(200,200),(100,300),(400,300)]],np.int32)
- cv2.polylines(img,[pts1],True,(0,0,255),5,4)
- #填充多边形
- pts = np.array([[(120,120),(220,220),(320,420),(450,360)]],np.int32)
- cv2.fillPoly(img,pts,(0,255,0))
-
- cv2.imshow('draw',img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
英文可以直接写,,但是中文需要额外的pillow包
- #文本
- import cv2
- import numpy as np
-
- img = np.zeros((480,640,3),np.uint8)
-
- cv2.putText(img,'hi na na',(30,300), cv2.FONT_HERSHEY_COMPLEX_SMALL,3,[0,255,0])
-
-
- cv2.imshow('draw',img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- #使用OpenCV绘制中文,pillow包
- #Windows,自带很多字体
- #pip install pillow -i http://pypi.douban.com/simple
- #导入: from PIL import ImageFont, ImageDraw,Image
-
- from PIL import ImageFont, ImageDraw,Image
-
- img = np.full((480,640,3),fill_value = 255,dtype = np.uint8)
-
- font = ImageFont.truetype('./Fonts/msyh.ttc',15)
-
- img_pil = Image.fromarray(img)
- draw = ImageDraw.Draw(IMG_PIL)
-
- #draw.text((10,140), '妈妈',font=font, fill=(0, 255, 0, 0))
- draw.text((10,150), 'mama', font = font, fill = (0,0,255,0))
- img = np.array(img_pil)
-
- cv2.imshow('img',img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
日积月累,第二章颜色变换,,期待第三章吧。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。