Python-OpenCV 处理图像(六)(七)(八):对象识别 图像灰度化处理 图像二值化处理
为了加快处理速度,在图像处理算法中,往往需要把彩色图像转换为灰度图像。
0x00. 灰度图
灰度数字图像是每个像素只有一个采样颜色的图像,这类图像通常显示为从最暗黑色到最亮的白色的灰度。
灰度图像与黑白图像不同,在计算机图像领域中黑白图像只有黑白两种颜色,灰度图像在黑色与白色之间还有许多级的颜色深度。
在RGB模型中,如果R=G=B
时,则彩色表示一种灰度颜色,其中R=G=B
的值叫灰度值。
0x01. 灰度化的方法
1. 分量法
将彩色图像中的三分量的亮度作为三个灰度图像的灰度值,可根据应用需要选取一种灰度图像。
F1(i,j) = R(i,j)
F2(i,j) = G(i,j)
F3(i,j) = B(i,j)
代码示例:
-
import cv2.cv as cv
-
image = cv.LoadImage('mao.jpg')
-
b = cv.CreateImage(cv.GetSize(image), image.depth, 1)
-
g = cv.CloneImage(b)
-
r = cv.CloneImage(b)
-
cv.Split(image, b, g, r, None)
-
cv.ShowImage('a_window', r)
-
cv.WaitKey(0)
2. 最大值法
将彩色图像中的三分量亮度的最大值作为灰度图的灰度值。
F(i,j) = max(R(i,j), G(i,j), B(i,j))
代码示例:
-
image = cv.LoadImage('mao.jpg')
-
new = cv.CreateImage(cv.GetSize(image), image.depth, 1)
-
for i in range(image.height):
-
for j in range(image.width):
-
new[i,j] = max(image[i,j][0], image[i,j][1], image[i,j][2])
-
cv.ShowImage('a_window', new)
-
cv.WaitKey(0)
3.平均值法
将彩色图像中的三分量亮度求平均得到一个灰度值。
F(i,j) = (R(i,j) + G(i,j) + B(i,j)) / 3
代码示例:
-
image = cv.LoadImage('mao.jpg')
-
new = cv.CreateImage(cv.GetSize(image), image.depth, 1)
-
for i in range(image.height):
-
for j in range(image.width):
-
new[i,j] = (image[i,j][0] + image[i,j][1] + image[i,j][2])/3
-
cv.ShowImage('a_window', new)
-
cv.WaitKey(0)
4.加权平均法
根据重要性及其它指标,将三个分量以不同的权值进行加权平均。由于人眼对绿色的敏感最高,对蓝色敏感最低,因此,按下式对RGB三分量进行加权平均能得到较合理的灰度图像。
F(i,j) = 0.30R(i,j) + 0.59G(i,j) + 0.11B(i,j))
代码示例:
-
image = cv.LoadImage('mao.jpg')
-
new = cv.CreateImage(cv.GetSize(image), image.depth, 1)
-
for i in range(image.height):
-
for j in range(image.width):
-
new[i,j] = 0.3 * image[i,j][0] + 0.59 * image[i,j][1] + 0.11 * image[i,j][2]
-
cv.ShowImage('a_window', new)
-
cv.WaitKey(0)
上面的公式可以看出绿色(G 分量)所占的比重比较大,所以有时候也会直接取G 分量进行灰度化。
代码示例:
-
image = cv.LoadImage('mao.jpg')
-
new = cv.CreateImage(cv.GetSize(image), image.depth, 1)
-
for i in range(image.height):
-
for j in range(image.width):
-
new[i,j] = image[i,j][1]
-
cv.ShowImage('a_window', new)
-
cv.WaitKey(0)
—————————————傲娇的分割线———————————————
0x00. 特征识别
这里主要用到两个函数:
GoodFeaturesToTrack
和 extractSURF
-
GoodFeaturesToTrack: 在图像中寻找具有大特征值的角点。
-
SURF算法: 是一个稳健的图像识别和描述算法。
总之这俩个我目前也不清楚能用来干嘛,以后用到了在更新吧。
-
import cv2.cv as cv
-
import math
-
im = cv.LoadImage("img/church.png", cv.CV_LOAD_IMAGE_GRAYSCALE)
-
im2 = cv.CloneImage(im)
-
# Goodfeatureto track algorithm
-
eigImage = cv.CreateMat(im.height, im.width, cv.IPL_DEPTH_32F)
-
tempImage = cv.CloneMat(eigImage)
-
cornerCount = 500
-
quality = 0.01
-
minDistance = 10
-
corners = cv.GoodFeaturesToTrack(im, eigImage, tempImage, cornerCount, quality, minDistance)
-
radius = 3
-
thickness = 2
-
for (x,y) in corners:
-
cv.Circle(im, (int(x),int(y)), radius, (255,255,255), thickness)
-
cv.ShowImage("GoodfeaturesToTrack", im)
-
#SURF algorithm
-
hessthresh = 1500 # 400 500
-
dsize = 0 # 1
-
layers = 1 # 3 10
-
keypoints, descriptors = cv.ExtractSURF(im2, None, cv.CreateMemStorage(), (dsize, hessthresh, 3, layers))
-
for ((x, y), laplacian, size, dir, hessian) in keypoints:
-
cv.Circle(im2, (int(x),int(y)), cv.Round(size/2), (255,255,255), 1)
-
x2 = x+((size/2)*math.cos(dir))
-
y2 = y+((size/2)*math.sin(dir))
-
cv.Line(im2, (int(x),int(y)), (int(x2),int(y2)), (255,255,255), 1)
-
cv.ShowImage("SURF ", im2)
-
cv.WaitKey(0)
0x01. 人脸识别
可以使用 OpenCV 训练好的级联分类器来识别图像中的人脸,当然还有很多其他的分类器:例如表情识别,鼻子等,具体可在这里下载:
具体使用代码:
-
#import library - MUST use cv2 if using opencv_traincascade
-
import cv2
-
# rectangle color and stroke
-
color = (0,0,255) # reverse of RGB (B,G,R) - weird
-
strokeWeight = 1 # thickness of outline
-
# set window name
-
windowName = "Object Detection"
-
# load an image to search for faces
-
img = cv2.imread("mao.jpg")
-
# load detection file (various files for different views and uses)
-
cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
-
# preprocessing, as suggested by: http://www.bytefish.de/wiki/opencv/object_detection
-
# img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2))
-
# gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
-
# gray = cv2.equalizeHist(gray)
-
# detect objects, return as list
-
rects = cascade.detectMultiScale(img)
-
# display until escape key is hit
-
while True:
-
# get a list of rectangles
-
for x,y, width,height in rects:
-
cv2.rectangle(img, (x,y), (x+width, y+height), color, strokeWeight)
-
# display!
-
cv2.imshow(windowName, img)
-
# escape key (ASCII 27) closes window
-
if cv2.waitKey(20) == 27:
-
break
-
# if esc key is hit, quit!
-
exit()
效果:
——————————————————卖萌的分割线——————————————————
0x00. 图像二值化
图像二值化就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果。
将256个亮度等级的灰度图像通过适当的阈值选取而获得仍然可以反映图像整体和局部特征的二值化图像。
图像的二值化有利于图像的进一步处理,使图像变得简单,而且数据量减小,能凸显出感兴趣的目标的轮廓。
0x01. 图像二值化处理
在将图像二值化之前需要将其先灰度化,示例代码:
-
import cv2.cv as cv
-
image = cv.LoadImage('mao.jpg')
-
new = cv.CreateImage(cv.GetSize(image), image.depth, 1)
-
for i in range(image.height):
-
for j in range(image.width):
-
new[i,j] = max(image[i,j][0], image[i,j][1], image[i,j][2])
-
cv.Threshold(new, new, 10, 255, cv.CV_THRESH_BINARY_INV)
-
cv.ShowImage('a_window', new)
-
cv.WaitKey(0)
0x02. cv.Threshold
cv.Threshold(src, dst, threshold, maxValue, thresholdType)
函数 cvThreshold 对单通道数组应用固定阈值操作。
该函数的典型应用是对灰度图像进行阈值操作得到二值图像。
参数说明:
-
src:原始数组 (单通道 , 8-bit of 32-bit 浮点数)。
-
dst:输出数组,必须与 src 的类型一致,或者为 8-bit。
-
threshold:阈值
-
maxValue:使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值。
-
threshold_type:阈值类型
-
threshold_type=CV_THRESH_BINARY:
-
如果 src(x,y)>threshold ,dst(x,y) = max_value; 否则,dst(x,y)=0;
-
threshold_type=CV_THRESH_BINARY_INV:
-
如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value.
-
threshold_type=CV_THRESH_TRUNC:
-
如果 src(x,y)>threshold,dst(x,y) = max_value; 否则dst(x,y) = src(x,y).
-
threshold_type=CV_THRESH_TOZERO:
-
如果src(x,y)>threshold,dst(x,y) = src(x,y) ; 否则 dst(x,y) = 0.
-
threshold_type=CV_THRESH_TOZERO_INV:如果 src(x,y)>threshold,dst(x,y) = 0 ; 否则dst(x,y) = src(x,y).
from: https://segmentfault.com/a/1190000003755089
https://segmentfault.com/a/1190000003755100
https://segmentfault.com/a/1190000003755115