python使用numpy实现直方图反向投影示例

作者:益达888 时间:2022-09-18 11:28:55 

最近跟着OpenCV2-Python-Tutorials在学习python_opencv中直方图的反向投影时,第一种方法是使用numpy实现将图中的红色玫瑰分割出来,教程给的代码缺了一句函数,导致实现不出来。

自己加上了后(也不知到这样加对不对)代码和效果如下:


代码:
import cv2
import numpy as np
roi = cv2.imread('./data/rose_red.jpg')
hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
#target is the image we search in
target = cv2.imread('./data/rose.jpg')
cv2.imshow('target',target)
hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
# Find the histograms using calcHist. Can be done with np.histogram2d also
M = cv2.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 256] )
print(M)
I = cv2.calcHist([hsvt],[0, 1], None, [180, 256], [0, 180, 0, 256] )
h,s,v = cv2.split(hsvt)
#斜体是自己加上的
R=M/I
print(R.shape)
B = R[h.ravel(),s.ravel()]
print(B)
B = np.minimum(B,1)
print(B)
B = B.reshape(hsvt.shape[:2])
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
B=cv2.filter2D(B,-1,disc)
B = np.uint8(B)
cv2.normalize(B,B,0,255,cv2.NORM_MINMAX)
cv2.imshow('B',B)
ret,thresh = cv2.threshold(B,2,255,0)
cv2.imshow('thresh',thresh)
res = cv2.bitwise_and(target,target,mask=thresh)
cv2.imshow('res',res)
cv2.waitKey(0)

效果:

rose_red.jpg

python使用numpy实现直方图反向投影示例

rose.jpg

python使用numpy实现直方图反向投影示例

result:

python使用numpy实现直方图反向投影示例

来源:https://blog.csdn.net/qq_29023939/article/details/81082040

标签:python,numpy,直方图,反向投影
0
投稿

猜你喜欢

  • javascript跨域原因以及解决方案分享

    2024-04-10 10:44:32
  • MySQL8.0.27安装过程中卡在Initializing Database中并报错的解决

    2024-01-19 06:35:05
  • python使用htmllib分析网页内容的方法

    2022-05-22 13:28:33
  • Python实现计算文件MD5和SHA1的方法示例

    2023-12-07 06:55:46
  • Pytorch中retain_graph的坑及解决

    2022-12-20 16:21:09
  • Oracle中命名块之存储过程的详解及使用方法

    2023-07-15 00:28:23
  • Ubuntu 16.04/18.04 安装Pycharm及Ipython的教程

    2023-11-03 03:49:29
  • python实现ftp客户端示例分享

    2021-07-06 08:41:18
  • Python中super关键字用法实例分析

    2023-12-08 06:11:46
  • mysql 基础教程之库与表的详解

    2024-01-19 13:14:38
  • python实现的系统实用log类实例

    2022-08-02 18:50:57
  • python爬虫之百度API调用方法

    2021-11-18 17:17:13
  • 如何用python多次调用exe文件运行不同的结果

    2023-07-08 03:01:24
  • python 列表常用方法超详细梳理总结

    2022-04-18 20:34:27
  • pymongo中group by的操作方法教程

    2021-03-23 05:44:40
  • python爬取招聘要求等信息实例

    2021-01-27 21:22:36
  • 用CSS定义 li 样式

    2007-09-28 20:56:00
  • python+pytest自动化测试函数测试类测试方法的封装

    2021-12-26 21:03:14
  • python PyAutoGUI 模拟鼠标键盘操作和截屏功能

    2022-11-12 08:16:41
  • 网页设计求职全攻略

    2008-07-09 18:56:00
  • asp之家 网络编程 m.aspxhome.com