show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次深入了图形绘制
- 图像
- 滑动条
- 文字
- 多边形
- 可以制作渐变的效果吗?
import cv2
import numpy
width = 400
height= 300
arr=numpy.ones((height,width),dtype=numpy.uint8)
for num in range(height):
arr[num, : ] = 0 + num / (height-1) * 255
cv2.imshow("ceshi",arr)
key=cv2.waitKey(0)
- 效果
import cv2
import numpy
width = 400
height= 300
arr=numpy.ones((height,width),dtype=numpy.uint8)
for num in range(height):
arr[num, : ] = 100 + num / (height-1) * 200
cv2.imshow("ceshi",arr)
key=cv2.waitKey(0)
- 从100到200
import cv2
import numpy as np
def callback(value):
print(value)
canvas = np.zeros((300,400), np.uint8)
cv2.namedWindow('canvas', cv2.WINDOW_NORMAL)
cv2.createTrackbar('T1', 'canvas', 0, 100, callback)
cv2.createTrackbar('T2', 'canvas', 100, 255, callback)
while True:
t1 = cv2.getTrackbarPos('T1', 'canvas')
t2 = cv2.getTrackbarPos('T2', 'canvas')
cv2.putText(canvas,str(t1),(30,80),cv2.FONT_HERSHEY_TRIPLEX,2,(0,255,0),5)
cv2.imshow("canvas",canvas)
width = 400
height= 300
for num in range(width):
value = t1 + num/ (width - 1) * t2
if value > 255:
value = 255
canvas[:,num] = value
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
- 效果
import cv2
import numpy
width = 400
height = 300
channels = 3
arr=numpy.zeros((height,width,channels),dtype=numpy.uint8)
for num in range(height):
arr[num, : ,2] = 255 - num / (height-1) * 255
cv2.imshow("ceshi",arr)
key=cv2.waitKey(0)
- 最终效果
import cv2
import numpy
width = 400
height = 300
channels = 3
arr=numpy.zeros((height,width,channels),dtype=numpy.uint8)
for num in range(height):
arr[num, : ,2] = 0 + num / (height-1) * 255
arr[num, : ,0] = 255 - num / (height-1) * 255
cv2.imshow("ceshi",arr)
key=cv2.waitKey(0)
- 效果
- 可以做出径向渐变的效果吗?
import cv2
import numpy as np
image = np.zeros((300,300),np.uint8)
for x in range(0,300):
for y in range(300):
color = int(((y-150)**2 + (x-150)**2)**0.5/(150*2**0.5)*255)
image[y,x] = color
cv2.imshow("image",image)
key = cv2.waitKey(0)
- 径向渐变
- 可以做出角度渐变吗?
import cv2
import numpy as np
import math
image = np.zeros((300,300),np.uint8)
for x in range(0,300):
for y in range(300):
if x == y == 150:
continue
angle = math.asin((y-150)/((x-150)**2+(y-150)**2)**0.5)
angle = angle * 180 / math.pi
image[y, x] = angle
print(x,y,angle)
cv2.imshow("image",image)
key = cv2.waitKey(0)
- 结果
- 怎么改呢?
import cv2
import numpy as np
import math
radius = 100
image = np.zeros((2*radius,2*radius),np.uint8)
for x in range(0,2 * radius):
for y in range(0, 2 * radius):
if x == y == radius:
continue
r = ((x-radius)**2+(y-radius)**2)**0.5
angle = math.asin((y-radius)/r)
if x <= radius and y <= radius:
angle = - angle * 180 / math.pi + 180
elif x <= radius and y >= radius:
angle = 180 - angle * 180 / math.pi
elif x >= radius and y <= radius:
angle = angle * 180 / math.pi
else:
angle = angle * 180 / math.pi
color = angle / 360 * 255
image[y, x] = color
print(y,x,angle,color)
cv2.imshow("image",image)
key = cv2.waitKey(0)
- 效果
import cv2
import numpy as np
# Create a blank image of desired size
width, height = 500, 500
image = np.zeros((height, width, 3), dtype=np.uint8)
# Define the center and maximum radius of the gradient
center_x, center_y = width // 2, height // 2
max_radius = min(width, height) // 2
# Generate the gradient
for angle in range(360):
# Convert angle to radians
angle_rad = np.deg2rad(angle)
# Calculate the radius based on angle
radius = angle * max_radius / 360
# Calculate the coordinates on the image
x = int(center_x + radius * np.cos(angle_rad))
y = int(center_y + radius * np.sin(angle_rad))
# Set the pixel color at that coordinate
image[y, x] = [angle, 255, 255] # Hue varies with angle
# Convert the image to BGR color space from HSV
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
# Display the resulting gradient image
cv2.imshow("Gradient", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
- 效果
- 这次设置了各种渐变的效果
- 可以控制灰度的渐变
- 也可以控制彩色的渐变
- 还可以配合滚动条制作渐变
- 还能玩点什么呢?🤔
- 下次再说👋