show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次设置了各种渐变的效果
- 可以控制灰度的渐变
- 也可以控制彩色的渐变
- 还可以配合滚动条制作渐变
- 还能玩点什么呢?🤔
import numpy as np
import cv2
img = cv2.imread("kun1.png")
print(img.shape)
mask = np.zeros((305,483,1),np.uint8)
mask[75:118,214:255,:] = 255
black = np.zeros((305,483,3),np.uint8)
final = cv2.add(img,black,mask=mask)
cv2.imshow("final",final)
cv2.waitKey()
cv2.destroyAllWindows()
#coding:utf-8
import cv2
import numpy as np
#读取图片
img = cv2.imread("/usr/local/support/lena.png", cv2.IMREAD_GRAYSCALE)
#获取图像宽和高
rows, cols = img.shape[:2]
print(rows, cols)
#画圆形
circle = np.zeros((rows, cols), dtype="uint8")
cv2.circle(circle, (int(rows/2.0), int(cols/2)), 80, 255, -1)
print(circle.shape)
print(img.size, circle.size)
#OpenCV图像与运算
result = cv2.bitwise_and(img, circle)
#显示图像
cv2.imshow("original", img)
cv2.imshow("circle", circle)
cv2.imshow("result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 效果
import cv2
import numpy as np
import math
#获取图像宽和高
img = cv2.imread("lena.png", cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape[:2]
def draw_gradient(pos_x,pos_y,radius,image):
for x in range(0,cols):
for y in range(rows):
dx = abs(x - pos_x)
dy = abs(y - pos_y)
dis = (dy**2 + dx**2) ** 0.5
if dis < radius:
color = dis / radius * 255
color = 255 - int(color)
image[y,x] = color
#画圆形
mask = np.zeros((rows, cols), dtype="uint8")
draw_gradient(int(rows/2.0), int(cols/2), 80, mask)
#OpenCV图像与运算
result = np.zeros((rows, cols), dtype="uint8")
for x in range(cols):
for y in range(rows):
result[y, x] = int ( mask[y,x]/255* img[y,x])
#显示图像
cv2.imshow("original", img)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 结果
- 这个蒙版可以应用吗?
import cv2
import numpy as np
import math
#获取图像宽和高
img = cv2.imread("lena.png", cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape[:2]
def draw_gradient(pos_x,pos_y,radius,image):
for x in range(0,cols):
for y in range(rows):
dx = abs(x - pos_x)
dy = abs(y - pos_y)
dis = (dy**2 + dx**2) ** 0.5
if dis < radius:
color = dis / radius * 255
color = 255 - int(color)
image[y,x] = color
#画圆形
img2 = cv2.imread("gear.jpg", cv2.IMREAD_GRAYSCALE)
mask = np.zeros((rows, cols), dtype="uint8")
draw_gradient(int(rows/2.0), int(cols/2), 80, mask)
#OpenCV图像与运算
result = img2
rows, cols = img2.shape[:2]
dy = 100
dx = 100
for x in range(cols):
for y in range(rows):
percent = mask[y+dy,x+dx] / 255
result2[y, x] = int(percent*img[y+dy,x+dx] + (1-percent)*img2[y,x])
#显示图像
cv2.imshow("original", img)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 效果
- 可以对比 软硬蒙版的效果的不同吗?
import cv2
import numpy as np
import math
#获取图像宽和高
img = cv2.imread("lena.png", cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape[:2]
def draw_gradient(pos_x,pos_y,radius,image):
for x in range(0,cols):
for y in range(rows):
dx = abs(x - pos_x)
dy = abs(y - pos_y)
dis = (dy**2 + dx**2) ** 0.5
if dis < radius:
color = dis / radius * 255
color = 255 - int(color)
image[y,x] = color
#画圆形
img2 = cv2.imread("gear.jpg", cv2.IMREAD_GRAYSCALE)
mask = np.zeros((rows, cols), dtype="uint8")
draw_gradient(int(rows/2.0), int(cols/2), 80, mask)
#OpenCV图像与运算
result = img2
dest_rows, dest_cols = img2.shape[:2]
dy = 100
dx = 100
for x in range(dest_cols):
for y in range(dest_rows):
percent = mask[y+dy,x+dx] / 255
result2[y, x] = int(percent*img[y+dy,x+dx] + (1-percent)*img2[y,x])
#显示图像
cv2.imshow("result", result)
result2 = img2
mask = np.zeros((rows,cols),dtype="uint8")
mask = cv2.circle(mask,(int(rows/2),int(cols/2)),80,255,-1)
cv2.imshow("mask", mask)
for x in range(dest_cols):
for y in range(dest_rows):
percent = mask[y+dy,x+dx] / 255
result2[y, x] = int(percent*img[y+dy,x+dx] + (1-percent)*img2[y,x])
#显示图像
cv2.imshow("result2", result2)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 可以做一个滑块看效果吗?
import cv2
import numpy as np
import math
#获取图像宽和高
img = cv2.imread("lena.png", cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape[:2]
def draw_gradient(pos_x,pos_y,radius,image):
for x in range(0,cols):
for y in range(rows):
dx = abs(x - pos_x)
dy = abs(y - pos_y)
dis = (dy**2 + dx**2) ** 0.5
if dis < radius:
color = dis / radius * 255
color = 255 - int(color)
image[y,x] = color
#画圆形
img2 = cv2.imread("gear.jpg", cv2.IMREAD_GRAYSCALE)
mask = np.zeros((rows, cols), dtype="uint8")
draw_gradient(int(rows/2.0), int(cols/2), 80, mask)
#OpenCV图像与运算
result = img2
dest_rows, dest_cols = img2.shape[:2]
dy = 100
dx = 100
for x in range(dest_cols):
for y in range(dest_rows):
result[y, x] = int ( mask[y+dy,x+dx]/255* img[y+dy,x+dx]) + img2[y,x]
#显示图像
def callback(value):
dx = value
img2 = cv2.imread("gear.jpg", cv2.IMREAD_GRAYSCALE)
mask = np.zeros((rows, cols), dtype="uint8")
draw_gradient(int(rows/2.0), int(cols/2), 80, mask)
for x in range(dest_cols):
for y in range(dest_rows):
percent = mask[y+dy,x+dx] / 255
result[y, x] = int(percent*img[y+dy,x+dx] + (1-percent)*img2[y,x])
cv2.imshow("result", result)
def callback2(value):
dx = value
img2 = cv2.imread("gear.jpg", cv2.IMREAD_GRAYSCALE)
mask = np.zeros((rows, cols), dtype="uint8")
mask = cv2.circle(mask,(int(rows/2),int(cols/2)),80,255,-1)
for x in range(dest_cols):
for y in range(dest_rows):
percent = mask[y+dy,x+dx] / 255
result2[y, x] = int(percent*img[y+dy,x+dx] + (1-percent)*img2[y,x])
cv2.imshow("result2", result2)
result2 = img2.copy()
cv2.imshow("result", result)
cv2.createTrackbar('x', 'result', 0, 100, callback)
mask = np.zeros((rows,cols),dtype="uint8")
mask = cv2.circle(mask,(int(rows/2),int(cols/2)),80,255,-1)
for x in range(dest_cols):
for y in range(dest_rows):
percent = mask[y+dy,x+dx] / 255
result2[y, x] = int(percent*img[y+dy,x+dx] + (1-percent)*img2[y,x])
#显示图像
cv2.imshow("result2", result2)
cv2.createTrackbar('x', 'result2', 0, 100, callback2)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
- 结果
- 可以控制x
- 可以控制y,radius或者蒙版的硬度吗?
- 这次了解了 蒙版的用法
- 可以控制蒙版的
- 位置
- 软硬
- 半径
- 形状
- 可以控制蒙版的
- 还有什么好玩的吗?
- 下次再说👋