您的位置:首页 > 编程语言 > Python开发

python OpenCV学习笔记直方图反向投影的实现

2018-02-07 14:00 1121 查看

本文介绍了python OpenCV学习笔记直方图反向投影的实现,分享给大家,具体如下:

官方文档 – https://docs.opencv.org/3.4.0/dc/df6/tutorial_py_histogram_backprojection.html

它用于图像分割或寻找图像中感兴趣的对象。简单地说,它创建一个与我们的输入图像相同大小(但单通道)的图像,其中每个像素对应于属于我们对象的像素的概率。输出图像将使我们感兴趣的对象比其余部分更白。

该怎么做呢?我们创建一个图像的直方图,其中包含我们感兴趣的对象。为了得到更好的结果,对象应该尽可能地填充图像。而颜色直方图比灰度直方图更受青睐,因为对象的颜色比灰度强度更能定义对象。然后,我们在我们的测试图像上“反向投射”这个直方图,我们需要找到这个对象,换句话说,我们计算每个像素的概率,并显示它。在适当的阈值上产生的输出结果使我们得到了一个单独的结果。

Numpy中的算法

1、首先,我们需要计算我们需要找到的对象的颜色直方图(让它为'M')和我们将要搜索的图像(让它为'I')。

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
# roi是我们需要找到的对象或区域
roi = cv.imread('rose_red.png')
hsv = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
# target是我们搜索的图像
target = cv.imread('rose.png')
hsvt = cv.cvtColor(target, cv.COLOR_BGR2HSV)
# 用calcHist来找直方图,也可以用np.histogram2d
M = cv.calcHist([hsv], [0,1], None, [180,256], [0,180,0,256])
I = cv.calcHist([hsvt], [0,1], None, [180,256], [0,180,0,256])

2、找到比率 R=M/I。然后背面投射R ,使用R作为调色板,并创建一个新的图像,每个像素作为其对应的目标概率。B(x,y) = R[h(x,y),s(x,y)],其中h是(x,y)坐标像素的色调,s是饱和度。之后,B(x,y)=min[B(x,y),1]

h, s, v = cv.split(hsvt)
B = R[h.ravel(), s.ravel()]
B = np.munimum(B, 1)
B = B.reshape(hsvt.shape[:2])

3、应用一个圆盘卷积,B = D * B,其中D是圆盘内核

disc = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5,5))
cv.filter2D(B, -1, disc, B)
B = np.uint8(B)
cv.normalize(B, B, 0, 255, cv.NORM_MINMAX)

4、现在,最大强度的位置给了我们物体的位置。如果我们期望图像中有一个区域,给出一个合适的阈值会有一个很好的结果。

ret, thresh = cv.threshold(B, 50, 255, 0)

OpenCV中的投影

OpenCV提供一个内置的函数cv.calcbackproject()。它的参数几乎与cv.calcHist()函数相同。它的一个参数是直方图,它是这个对象的直方图,我们必须找到它。另外,在传递给backproject函数之前,对象的直方图应该是标准化的。它返回概率图像。然后,我们将图像与磁盘内核进行卷积,并应用阈值。下面是我的代码和输出:

import numpy as np
import cv2 as cv
roi = cv.imread('rose_red.png')
hsv = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
target = cv.imread('rose.png')
hsvt = cv.cvtColor(target, cv.COLOR_BGR2HSV)
# 计算对象的直方图
roihist = cv.calcHist([hsv], [0,1], None, [180,256], [0,180,0,256])
# 标准化直方图,并应用投影
cv.normalize(roihist, roihist, 0, 255, cv.NORM_MINMAX)
dst = cv.calcBackProject([hsvt], [0,1], roihist, [0,180,0,256], 1)
# 与磁盘内核进行卷积
disc = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5,5))
cv.filter2D(dst, -1, disc, dst)
# 阈值、二进制按位和操作
ret, thresh = cv.threshold(dst, 50, 255, 0)
thresh = cv.merge((thresh, thresh, thresh))
res = cv.bitwise_and(target, thresh)
res = np.vstack((target, thresh, res))
cv.imwrite('res.jpg', res)

下面是一个例子。使用蓝色矩形中的区域作为示例对象,提取想提取全部内容。

关于这两种技术的原理可以参考我上面贴的链接,下面是示例的代码:

0x01. 绘制直方图

import cv2.cv as cv
def drawGraph(ar,im, size): #Draw the histogram on the image
minV, maxV, minloc, maxloc = cv.MinMaxLoc(ar) #Get the min and max value
hpt = 0.9 * histsize
for i in range(size):
intensity = ar[i] * hpt / maxV #Calculate the intensity to make enter in the image
cv.Line(im, (i,size), (i,int(size-intensity)),cv.Scalar(255,255,255)) #Draw the line
i += 1
#---- Gray image
orig = cv.LoadImage("img/lena.jpg", cv.CV_8U)
histsize = 256 #Because we are working on grayscale pictures which values within 0-255
hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([orig], hist) #Calculate histogram for the given grayscale picture
histImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values
drawGraph(hist.bins, histImg, histsize)
cv.ShowImage("Original Image", orig)
cv.ShowImage("Original Histogram", histImg)
#---------------------
#---- Equalized image
imEq = cv.CloneImage(orig)
cv.EqualizeHist(imEq, imEq) #Equlize the original image
histEq = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([imEq], histEq) #Calculate histogram for the given grayscale picture
eqImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values
drawGraph(histEq.bins, eqImg, histsize)
cv.ShowImage("Image Equalized", imEq)
cv.ShowImage("Equalized HIstogram", eqImg)
#--------------------------------
cv.WaitKey(0)

0x02. 反向投影

import cv2.cv as cv
im = cv.LoadImage("img/lena.jpg", cv.CV_8U)
cv.SetImageROI(im, (1, 1,30,30))
histsize = 256 #Because we are working on grayscale pictures
hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([im], hist)
cv.NormalizeHist(hist,1) # The factor rescale values by multiplying values by the factor
_,max_value,_,_ = cv.GetMinMaxHistValue(hist)
if max_value == 0:
max_value = 1.0
cv.NormalizeHist(hist,256/max_value)
cv.ResetImageROI(im)
res = cv.CreateMat(im.height, im.width, cv.CV_8U)
cv.CalcBackProject([im], res, hist)
cv.Rectangle(im, (1,1), (30,30), (0,0,255), 2, cv.CV_FILLED)
cv.ShowImage("Original Image", im)
cv.ShowImage("BackProjected", res)
cv.WaitKey(0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息