对图像处理,最常见的也莫过于对图片打马赛克了。其实它的原理也很简单,对一整块图片采用一个像素点填充。我们还可以调用opencv的内置函数方法,对鼠标键盘的案件信号编程,实现可交互式打马赛克。
然后我们还将介绍,比较基础的图像运算处理。包括或运算、与运算、加法运算等,以及HSV参数获取,BGR参数分隔等方法。
import numpy as np
from cv2 import cv2 as cv
sizeof = 60
img = cv.imread(r'./CV-Pictures/008.jpg')
height = img.shape[0] #获取图片的高
width = img.shape[1] #获取图片的宽
num_Height = np.uint8(height/sizeof) #对图片的高分割
num_Width = np.uint8(width/sizeof) #对图片的宽分割
new_img = np.zeros((num_Height*sizeof,num_Width*sizeof,3),np.uint8)
# 新图片的初始化
# new_img = np.zeros(img.shape,np.uint8)
for i in range(sizeof):
y = i * num_Height # 对高遍历
for j in range(sizeof):
x = j * num_Width # 对宽遍历
b = img[y,x][0]
g = img[y,x][1]
r = img[y,x][2]
for n in range(num_Height):
for m in range(num_Width):
new_img[y + n, x + m][0] = b
new_img[y + n, x + m][1] = g
new_img[y + n, x + m][2] = r
cv.imshow('img',img)
cv.imshow('img_new',new_img)
cv.waitKey(0)
import numpy as np
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
enable = False
def call_back_draw(event,x,y,flag,param):
global enable
if event == cv.EVENT_LBUTTONDOWN:
enable = True
elif event == cv.EVENT_MOUSEMOVE and flag == cv.EVENT_FLAG_LBUTTON:
if enable:
dramMask(y,x)
elif event == cv.EVENT_LBUTTONUP:
enable = False
#图像局部采样函数
def dramMask(x,y,size=10):
m = np.uint8(x/size) * size
n = np.uint8(y/size) * size
img[m:m+size,n:n+size] = img[m,n] # 填充方法
#size 大小范围内
'''
for i in range(size):
for j in range(size):
img[m+i,n+j] = img[m,n]
#img[m+i][n+j] = img[m][n]
'''
cv.namedWindow("image")
cv.setMouseCallback("image",call_back_draw)
while True:
cv.imshow("image",img)
if cv.waitKey(1) == ord('q'):
break
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下
按住鼠标左键不松,给图片打马赛克
import numpy as np
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
new_img = np.zeros(img.shape,np.uint8)
cv.circle(new_img,(300,300),120,(20,20,255),-1)
or_img = cv.bitwise_or(img,new_img)
pic = np.hstack([img,or_img])
cv.imshow("img",pic)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:
import numpy as np
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
new_img = np.zeros(img.shape,np.uint8)
cv.circle(new_img,(300,300),120,(255,255,255),-1)
and_img = cv.bitwise_and(img,new_img)
pic = np.hstack([img,and_img])
cv.imshow("pic_and",pic)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下
HSV 是为了数字化图像提出来了,不能很好的表示人眼解释图像过程
H (Hue) 色相:[0,360]
S (Saturation) 饱和度,即色彩纯净度,0饱和度为白色
V (Value/Brightness) 明度 0明度为纯黑色
在OpenCV中,颜色范围:
H = [0,179]
S = [0,255]
V = [0,255]
具体细节请参考这篇博文,原文链接:https://blog.csdn.net/wsp_1138886114/article/details/80660014
import numpy as np
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
print(img)
hsv = cv.cvtColor(img,cv.COLOR_BGR2HSV)
b,g,r = cv.split(img)
pic = np.hstack([img,hsv])
cv.imshow("pic_HSV",pic)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下
首先我们对图像某一块区域填色。
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
img[100:400,300:400,:] = [255,255,0]
cv.imshow('picture0',img)
cv.waitKey(0)
cv.destroyAllWindows()
代码运行结果如下。可以看到img第一个参数代表图像的纵轴。
那么要实现两张图片的相加,首先要把两张图片的大小调整为相同的大小。
下面介绍成倍数增加的方法resize。它既可以通过传递目标大小的图片的性转改变图片大小,也可以认为控制成倍数的改变图片的大小。
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
print(img1.shape)
img1 = cv.resize(img1,dsize=None,fx=1.5,fy=2)
print(img1.shape)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下所示
(400, 640, 3)
(800, 960, 3)
说明了第一个参数时fy,第二个参数时fx
import numpy as np
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
img1 = cv.resize(img1,img.shape[:2])
# img2 = cv.resize(img2,dsize=None,fx=1.5,fy=2) 针对x,y对图片成倍数增大
img2 = img + img1
img3 = cv.add(img,img1)
pic_add = np.hstack([img2,img3])
cv.imshow("np_cv",pic_add)
#cv.imshow('img3',img2)# np 325/255 70
#cv.imshow('img4',img3)# cv 325 255
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下图所示,可以看到,右侧图发亮,而左侧图发暗,原因是np的图像相加时,超出255会对数值255取余 比如325%255 = 70,而opencv超过255直接赋值255,325直接赋值255显示。同理,在图像减法亦是如此,np把超出的值取余,opencv直接赋值0.
import numpy as np
from cv2 import cv2 as cv
img = cv.imread(r'./CV-Pictures/008.jpg')
img1 = cv.imread(r'./CV-Pictures/009.jpg')
b,g,r = cv.split(img)
cv.imshow('blue',b)
cv.imshow('green',g)
cv.imshow('red',r)
img_merge = cv.merge([b,r,b])
cv.imshow('new_merge',img_merge)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:
小程序员将代码文件和相关素材整理到了百度网盘里,因为文件大小基本不大,大家也不用担心限速问题。后期小程序员有能力的话,将在gitee或者github上上传相关素材。
链接:https://pan.baidu.com/s/1Ce14ZQYEYWJxhpNEP1ERhg?pwd=7mvf
提取码:7mvf