CV基础:图像特效&;线段文字绘制(2)
程序员文章站
2023-12-25 18:22:15
...
####浮雕效果###
###newP=gray0-gray1+150
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height):
for j in range(0,width-1):
grayP0 = int(gray[i,j])
grayP1 = int(gray[i,j+1])
newP = grayP0-grayP1+150
if newP>255:
newP = 255
if newP<0:
newP = 0
dst[i,j] = newP
cv2.imshow('dst',dst)
cv2.waitKey(0)
####油画效果####
#1.灰度图处理2.把0-255进行分段,然后对每个像素点周围的方阵遍历
#统计那个段的数据最多,然后对(b,g,r)取平均值赋值给该点
img = cv2.imread('image00.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,3),np.uint8)
for i in range(4,height-4):
for j in range(4,width-4):
array1 = np.zeros(8,np.uint8)
for m in range(-4,4):
for n in range(-4,4):
p1 = int(gray[i+m,j+n]/32)
array1[p1] = array1[p1]+1
currentMax = array1[0]
l = 0
for k in range(0,8):
if currentMax<array1[k]:
currentMax = array1[k]
l = k
#简化的方法
for m in range(-4,4):
for n in range(-4,4):
if gray[i+m,j+n]>=(l*32) and gray[i+m,j+n]<=(l+1)*32:
(b,g,r) = img[i+m,j+n]
dst[i,j] = (b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
####形状绘制####
newImageInfo = (500,500,3)
dst = np.zeros(newImageInfo,np.uint8)
#line绘制线段1.目标图片数据2.起始位置3.终止位置4.color
cv2.line(dst,(100,100),(400,400),(0,0,255))
#para5.线条宽度
cv2.line(dst,(100,200),(400,200),(0,255,255),20)
#para6.线条类型
cv2.line(dst,(100,300),(400,300),(0,255,0),20,cv2.LINE_AA)
cv2.imshow('dst',dst)
cv2.waitKey(0)
####矩形圆形椭圆等绘制####
newImageInfo = (500,500,3)
dst = np.zeros(newImageInfo,np.uint8)
#矩形绘制1.目标图片2.左上角3.右下角4.颜色5.填充(-1)或者为线头宽度(大于0时)
cv2.rectangle(dst,(50,100),(200,300),(255,0,0),5)
#圆形绘制1.目标图片2.圆心3,半径4.颜色5.填充(-1)或者为线头宽度(大于0时)
cv2.circle(dst,(250,250),(50),(0,250,0),2)
#椭圆形绘制2.椭圆形圆心3,轴的长度(x,y)4.偏转角度5.原始角度6.终止角度7.颜色8.内容是否填充
cv2.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1)
#绘制任意多边形
points = np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.int32)
#points.shape=(5,2)
points = points.reshape((-1,1,2))#维度变换
#point.shape=(5,1,2)
cv2.polylines(dst,[points],True,(0,255,255))
cv2.imshow('dst',dst)
cv2.waitKey(0)
####文字绘制####
img = cv2.imread('image0.jpg',1)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.rectangle(img,(200,100),(500,400),(0,255,0),3)
#绘制文字1.目标图片2.文字内容3,写入的坐标4.字体5.字体大小6.颜色7.线条宽度8,线条类型
cv2.putText(img,'this is flower',(100,300),font,1,(200,100,255),2,cv2.LINE_AA)
cv2.imshow('src',img)
cv2.waitKey(0)
####图片插入另外一张图片####
img = cv2.imread('image0.jpg',1)
height = int(img.shape[0]*0.2)
width = int(img.shape[1]*0.2)
imgResize = cv2.resize(img,(width,height))
for i in range(0,height):
for j in range(0,width):
img[i+200,j+200]=imgResize[i,j]
cv2.imshow('src',img)
cv2.waitKey(0)