在python中将图像划分为5x5块,并为每个块计算直方图 [英] Divide an image into 5x5 blocks in python and compute histogram for each block
问题描述
使用Python我必须将(2)Test_Image和Reference_image分成5x5块,并为每个块计算直方图,并将其与另一个图像中的相同块进行比较。对于例如: image1(1,1)
与 image2(1,1)
。
我必须比较两个图像之间的相似性(应该是变换不变)。
我现在所做的是使用 hist = numpy.histogram(image,bins = 256)
计算整个图像的直方图想要划分图像并且稍后针对所有这些块计算直方图。我也想用Bhattacharya的系数来衡量相似性。
任何人都可以指导我如何通过这一个。提前感谢:)
using Python i have to divide(2) an Test_Image and Reference_image into 5x5 blocks and compute histogram for each block and compare it with the same block in the other image. For Ex : image1(1,1 )
with image2(1,1)
.
I have to compare similarity between two images (should be transform invariant).
What i have done till now is calculate histogram of a whole image using "hist=numpy.histogram(image,bins=256)
"
I want to divide a image and later compute for histogram for all those blocks . I also want to use Bhattacharya s coefficient to measure the similarity.
Can anyone guide me with how to go through this one. Thanks in advance :)
推荐答案
不知道是否是这样的你要找的,
这是brute-force version.and它可能是相当slow.but它做的工作
你必须决定如何做的边界虽然。
除非窗口完全匹配,否则不包括边界
Not sure if it is something like this you are looking for, This is the brute-force version.and it's probably quite slow.but it does the job You have to decide what to do with the boundaries though. This will not include the boundary unless the window fits exactly
import numpy as numpy
grey_levels = 256
# Generate a test image
test_image = numpy.random.randint(0,grey_levels, size=(11,11))
# Define the window size
windowsize_r = 5
windowsize_c = 5
# Crop out the window and calculate the histogram
for r in range(0,test_image.shape[0] - windowsize_r, windowsize_r):
for c in range(0,test_image.shape[1] - windowsize_c, windowsize_c):
window = test_image[r:r+windowsize_r,c:c+windowsize_c]
hist = numpy.histogram(window,bins=grey_levels)
下面是结果和完整图像是在结束。
r,c表示窗口的topleft角
Below is the result and the full image is at the end. r,c represents the topleft corner of the window
r=0,c=0
[[ 63 173 131 205 239]
[106 37 156 48 81]
[ 85 85 119 60 228]
[236 79 247 1 206]
[ 97 50 117 96 206]]
r=0,c=5
[[108 241 155 214 183]
[202 2 236 183 225]
[214 141 1 185 115]
[ 4 234 249 95 67]
[232 217 116 211 24]]
r=5,c=0
[[179 155 41 47 190]
[159 69 211 41 92]
[ 64 184 187 104 245]
[190 199 71 228 166]
[117 56 92 5 186]]
r=5,c=5
[[ 68 6 69 63 242]
[213 133 139 59 44]
[236 69 148 196 215]
[ 41 228 198 115 107]
[109 236 191 48 53]]
[[ 63 173 131 205 239 108 241 155 214 183 42]
[106 37 156 48 81 202 2 236 183 225 4]
[ 85 85 119 60 228 214 141 1 185 115 80]
[236 79 247 1 206 4 234 249 95 67 203]
[ 97 50 117 96 206 232 217 116 211 24 242]
[179 155 41 47 190 68 6 69 63 242 162]
[159 69 211 41 92 213 133 139 59 44 196]
[ 64 184 187 104 245 236 69 148 196 215 91]
[190 199 71 228 166 41 228 198 115 107 82]
[117 56 92 5 186 109 236 191 48 53 65]
[177 170 114 163 101 54 80 25 112 35 85]]
这篇关于在python中将图像划分为5x5块,并为每个块计算直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!