OpenCV Temelleri
人工智能等各类编程培训资料整理,所有资源无秘无压缩-购买会员
我们可以使用 OpenCV Numpy 库来处理图像。在这篇文章中,我将主要讨论 OpenCV 的功能以及如何玩转图像。
1. 调整图像大小
2. 裁剪图像
3. WarpPrespective——透视
4. 合并图像
5. 颜涩检测
6. 图像遮蔽
调整图像大小
调整图像大小的 OpenCV 代码是 Resize()。
import cv2
import numpy as np
img=cv2.imread("ressim.jpg")
#Resmi yeniden boyutlandırmaya yarar.
imga= cv2.resize(img,(1000,1000))
cv2.imshow("img",imga)
cv2.Waitkey(0)
cv2.destroyAllWindows()
裁剪图像
要裁剪图片,只需输入图片的矩阵值并将其分配给新值即可。
import cv2
import numpy as np
#Resmin belli yerini kırpıyoruz.
#img[yükseklik değerleri,boyuna değerleri]
kirpilmisimg= img[0:200,200:500]
cv2.imshow("Kırpılmış",kirpilmisimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
WarpPrespective——透视
透视是一种投影技术,用于在平面上以 3D 形式显示对象的外观,即将对象缩小为二维。
也就是说,它是一张技术图纸。在透视图中,绘图是根据对象相对于观察者的位置和距离的影响进行绘制的。
考虑车辆道路摄像头上的图像。图像中的路径必须是直的才能操作。这是 OpenCV 中的这个过程;
· cv2.getPerspectiveTransform()
· cv2.warpPerspective()
import cv2
import numpy as np
width,height=550,350 #<-- Yeni şeklin boyut değerleri
#-- resimden çekeceğimiz görselin matris koordinatları.
p1 = np.float32([[65,67],[242,13],[133,212],[301,130]])
#-- Çektiğimiz resmi kaydedeceğimiz yeni resim koordinatları.
p2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix = cv2.getPerspectiveTransform(p1,p2)
imgoutput = cv2.warpPerspective(img,matrix,(width,height))
cv2.imshow("a",img)
cv2.imshow("c",imgoutput)
cv2.waitKey(0)
cv2.destroyAllWindows()
合并图像
将图像并排组合或合并在单个图像中;
· stackImages()
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
我们的代码;
import cv2
import numpy as np
img=cv2.imread("ressim.jpg")
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
imgyapismis= stackImages(0.5,([img,img,img],[img,img,img])) #<-- Birinci satırda 3 adet colon(görsel) varsa, ikincide de 3 adet olmak zorunda.
cv2.imshow("imgyapismis.jpg",imgyapismis)
cv2.imshow("a",imgdikey)
cv2.waitKey(0)
cv2.destroyAllWindows()
颜涩检测
得益于OpenCV的cvtColor()函数,我们让我们拥有的图像通过一些过滤器,这样我们就可以从我们拥有的图像中获取我们想要的颜涩值。
这里我们使用的是 HVS 颜涩模型,而不是 BGR。
我们的代码:
import cv2
img = cv2.imread("ressim.jpg")
cv2.imshow("a",img)
#-- cvtColor() Görüntüyü bir renk uzayından diğerine dönüştürür.
imgHVS=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("b",imgHVS)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像遮蔽
这里我们使用了 HVS 颜涩模型,而不是 BGR。当我们给现实世界中只有一种颜涩的物体拍照时,照片实际上并没有单一的颜涩范围。环境的光线,打在物体上的光线亮度等。影响他们。这就是为什么我们在确定颜涩时需要看那些值的范围,而不是单个值。
例如,在下面的照片中,我们看到有两个 Hue max 和 Hue min 涩调。这是因为我们指定了涩调范围。
import cv2
import numpy as np
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows): for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale) if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
path = 'ressim.jpg'
def empty(a):
pass
cv2.namedWindow("Cubuk")
cv2.resizeWindow("Cubuk",640,240)
# Hue: Renk tonu.
# Sat: Doygunluk.
# Value: Değer.
cv2.createTrackbar("Hue Min", "Cubuk", 0, 179, empty)
cv2.createTrackbar("Hue Max", "Cubuk", 179, 179, empty)
cv2.createTrackbar("Sat Min", "Cubuk", 0, 255, empty)
cv2.createTrackbar("Sat Max", "Cubuk", 255, 255, empty)
cv2.createTrackbar("Val Min", "Cubuk", 0, 255, empty)
cv2.createTrackbar("Val Max", "Cubuk", 255, 255, empty)
while True:
img = cv2.imread(path)
imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("Hue Min", "Cubuk")
h_max = cv2.getTrackbarPos("Hue Max", "Cubuk")
s_min = cv2.getTrackbarPos("Sat Min", "Cubuk")
s_max = cv2.getTrackbarPos("Sat Max", "Cubuk")
v_min = cv2.getTrackbarPos("Val Min", "Cubuk")
v_max = cv2.getTrackbarPos("Val Max", "Cubuk")
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHSV, lower, upper)
imgResult = cv2.bitwise_and(img,img,mask=mask)
StackedImages = stackImages(0.6,([img,imgHSV], [mask,imgResult]))
cv2.imshow("Birleştirilmiş Resim", StackedImages)
cv2.waitKey(1)
来源
600学习网 » OpenCV Temelleri