Python+OpenCV:直方图反向投影(Histogram Backprojection)

82 篇文章 20 订阅

Python+OpenCV:直方图反向投影(Histogram Backprojection)

Algorithm in Numpy

1. First we need to calculate the color histogram of both the object we need to find (let it be 'M') and the image where we are going to search (let it be 'I').

import numpy as np
import cv2 as cvfrom matplotlib import pyplot as plt
#roi is the object or region of object we need to find
roi = cv.imread('rose_red.png')
hsv = cv.cvtColor(roi,cv.COLOR_BGR2HSV)
#target is the image we search in
target = cv.imread('rose.png')
hsvt = cv.cvtColor(target,cv.COLOR_BGR2HSV)
# Find the histograms using calcHist. Can be done with np.histogram2d also
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. Find the ratio . Then backproject R, ie use R as palette and create a new image with every pixel as its corresponding probability of being target. ie B(x,y) = R[h(x,y),s(x,y)] where h is hue and s is saturation of the pixel at (x,y).

After that apply the condition B(x,y)=min[B(x,y),1].

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

3. Now apply a convolution with a circular disc, B=D∗B, where D is the disc kernel.

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. Now the location of maximum intensity gives us the location of object. If we are expecting a region in the image, thresholding for a suitable value gives a nice result.

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

That's it !!

Backprojection in OpenCV

####################################################################################################
# 图像直方图反向投影(Histogram Backprojection)
def lmc_cv_image_histogram_backprojection():
    """
        函数功能: 图像直方图反向投影(Histogram Backprojection)。
    """

    # 图像直方图反向投影(Histogram Backprojection)

    # 读取图像
    roi = lmc_cv.imread('D:/99-Research/Python/Image/messi_roi.jpg')
    hsv_roi = lmc_cv.cvtColor(roi, lmc_cv.COLOR_BGR2HSV)
    target = lmc_cv.imread('D:/99-Research/Python/Image/messi.jpg')
    hsv_target = lmc_cv.cvtColor(target, lmc_cv.COLOR_BGR2HSV)
    target = lmc_cv.cvtColor(target, lmc_cv.COLOR_BGR2RGB)

    # calculating object histogram
    roihist = lmc_cv.calcHist([hsv_roi], [0, 1], None, [180, 256], [0, 180, 0, 256])
    # normalize histogram and apply backprojection
    lmc_cv.normalize(roihist, roihist, 0, 255, lmc_cv.NORM_MINMAX)
    dst = lmc_cv.calcBackProject([hsv_target], [0, 1], roihist, [0, 180, 0, 256], 1)
    # Now convolute with circular disc
    disc = lmc_cv.getStructuringElement(lmc_cv.MORPH_ELLIPSE, (5, 5))
    lmc_cv.filter2D(dst, -1, disc, dst)
    # threshold and binary AND
    ret, thresh = lmc_cv.threshold(dst, 50, 255, 0)
    thresh = lmc_cv.merge((thresh, thresh, thresh))
    res = lmc_cv.bitwise_and(target, thresh)
    res = np.hstack((target, thresh, res))

    pyplot.figure('Image Display 1')
    pyplot.imshow(res, cmap='gray')
    pyplot.xticks([])
    pyplot.yticks([])
    pyplot.title('Histogram Backprojection')
    pyplot.show()

    # 根据用户输入保存图像
    if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
        # 销毁窗口
        pyplot.close()
    return

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值