gpt4 book ai didi

Python+OpenCV实现图像融合的原理及代码

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 34 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python+OpenCV实现图像融合的原理及代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

根据导师作业安排,在学习数字图像处理(刚萨雷斯版)第六章 彩色图像处理 中的彩色模型后,导师安排了一个比较有趣的作业:

Python+OpenCV实现图像融合的原理及代码

融合原理为:

1 注意:遥感原rgb图image和灰度图grayimage为测试用的输入图像; 。

2 步骤:(1)将rgb转换为hsv空间(h:色调,s:饱和度,v:明度); 。

(2)用gray图像诶换掉hsv中的v; 。

(3)替换后的hsv转换回rgb空间即可得到结果.

书上只介绍了hsi彩色模型,并没有说到hsv,所以需要网上查找资料.

python代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import cv2
import numpy as np
import math
from matplotlib import pyplot as plt
def caijian(img): #裁剪图像与否根据选择图像大小而定,调用了opencv函数
weight = img.shape[ 0 ]
height = img.shape[ 1 ]
print (“图像大小为: % d * % d” % (weight,height))
img = cv2.resize(img,( int (weight / 2 ), int (height / 2 )),interpolation = cv2.inter_cubic)
return (img)
def graytograyimg(img):
grayimg = img1
weight = img.shape[ 0 ]
height = img.shape[ 1 ]
for i in range (weight):
for j in range (height):
grayimg[i,j] = 0.299img [i,j, 0 ] + 0.587img [i,j, 1 ] + 0.114img [i,j, 2 ]
return (grayimg)
def rgbtohsv(img):
b,g,r = cv2.split(img)
rows,cols = b.shape
h = np.ones([rows,cols],“ float ”)
s = np.ones([rows,cols],“ float ”)
v = np.ones([rows,cols],“ float ”)
print (“rgb图像大小: % d * % d” % (rows,cols))
for i in range ( 0 , rows):
for j in range ( 0 , cols):
max = max ((b[i,j],g[i,j],r[i,j]))
min = min ((b[i,j],g[i,j],r[i,j]))
v[i,j] = max
if v[i,j] 0 :
s[i,j] = 0
else :
s[i,j] = (v[i,j] - min ) / v[i,j]
if maxmin:
h[i,j] = 0 # 如果rgb三向量相同,色调为黑
elif v[i,j] = = r[i,j]:
h[i,j] = ( 60 * ( float (g[i,j]) - b[i,j]) / (v[i,j] - min ))
elif v[i,j] = = g[i,j]:
h[i,j] = 60 * ( float (b[i,j]) - r[i,j]) / (v[i,j] - min ) + 120
elif v[i,j] = = b[i,j]:
h[i,j] = 60 * ( float (r[i,j]) - g[i,j]) / (v[i,j] - min ) + 240
if h[i,j]< 0 :
h[i,j] = h[i,j] + 360
h[i,j] = h[i,j] / 2
s[i,j] = 255 * s[i,j]
result = cv2.merge((h,s,v)) # cv2.merge函数是合并单通道成多通道
result = np.uint8(result)
return (result)
def graytohsgry(grayimg,hsvimg):
h,s,v = cv2.split(hsvimg)
rows,cols = v.shape
for i in range (rows):
for j in range (cols):
v[i,j] = grayimg[i][j][ 0 ]
newimg = cv2.merge([h,s,v])
newimg = np.uint8(newimg)
return newimg
def hsvtorgb(img,rgb):
h1,s1,v1 = cv2.split(img)
rg = rgb.copy()
rows,cols = h1.shape
r,g,b = 0.0 , 0.0 , 0.0
b1,g1,r1 = cv2.split(rg)
print (“hsv图像大小为: % d * % d” % (rows,cols))
for i in range (rows):
for j in range (cols):
h = h1[i][j]
v = v1[i][j] / 255
s = s1[i][j] / 255
h = h2
hx = int (h / 60.0 )
hi = hx % 6
f = hx - hi
p = v( 1 - s)
q = v * ( 1 - fs)
t = v( 1 - ( 1 - f)s)
if hi0:
r,g,b = v,t,p
elif hi1:
r,g,b = q,v,p
elif hi2:
r,g,b = p,v,t
elif hi3:
r,g,b = p,q,v
elif hi4:
r,g,b = t,p,v
elif hi5:
r,g,b = v,p,q
r,g,b = (r255),(g255),(b255)
r1[i][j] = int ®
g1[i][j] = int (g)
b1[i][j] = int (b)
rg = cv2.merge([b1,g1,r1])
return rg
img = cv2.imread(“d: / rgb.bmp”)
gray = cv2.imread(“d: / gray.bmp”)
img = caijian(img)
gray = caijian(gray)
grayimg = graytograyimg(gray)
hsvimg = rgbtohsv(img)
hsgray = graytohsgry(grayimg,hsvimg)
rgbimg = hsvtorgb(hsgray,img)
cv2.imshow(“image”,img)
cv2.imshow(“grayimage”,grayimg)
cv2.imshow(“hsvimage”,hsvimg)
cv2.imshow(“hsgrayimage”,hsgray)
cv2.imshow(“rgbimage”,rgbimg)
cv2.waitkey( 0 )
cv2.destroyallwindows()

以上代码是在尽量不调用opencv函数的情况下编写,其目的是熟悉图像处理原理和python编程,注释很少,其中rgb转hsv原理,hsv转rgb原理,在csdn中都能找到,灰度图替换hsv中的v原理其实很简单,看代码就能明白,不用再找资料.

总结 。

以上所述是小编给大家介绍的python+opencv实现图像融合的原理及代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。

原文链接:https://blog.csdn.net/Skymelu/article/details/84767689 。

最后此篇关于Python+OpenCV实现图像融合的原理及代码的文章就讲到这里了,如果你想了解更多关于Python+OpenCV实现图像融合的原理及代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com