【问题标题】:how to divide images into 8x8 blocks and merge them back using opencv?如何将图像划分为 8x8 块并使用 opencv 将它们合并回来?
【发布时间】:2021-08-11 14:02:06
【问题描述】:

如何将图像划分为 8x8 块(并对块进行 DCT),然后使用 opencv 将它们合并回来。我已将图像划分为块,但我不知道如何通过实现重复数据删除将其合并回来。 这是我将图像划分为块的代码

   `import time
    import cv2
    import numpy as np
    from hashlib import md5
    hashs=[] 
    def compute_image_path(path):
      img=cv2.imread(path)
      #img=cv2.resize(img,(512,512))
      print(img.shape)
      print(img.size/1024)
      start=time.time()
      imgs=np.split(img,8,axis=0)
       for i,ix in enumerate(imgs):
        ig=np.split(ix,8,axis=1)
         for j,iy in enumerate(ig):
           if(j==1):
              cv2.imshow(str(i*8+j),iy)
      
     cv2.imwrite("C:\\Users\\prave\\Desktop\\Comp\\"+str(i*8+j)+".jpg",iy)
        hashs.append(md5(iy.tostring()).hexdigest())                   
       end=time.time()
       print(end-start)
       print(len(hashs))
       print(len(set(hashs)))    
       cv2.waitKey(0)
       print(hashs[0])
       print(hashs[1])      
       if __name__ == "__main__":
        #img=np.zeros((512,512,3))
        #img[:200,:,0]=100
        #img[200:400,:,1]=100
        #img[400:,:,2]=100
        #cv2.imshow("input",img)        
        #cv2.waitKey(0)
        compute_image_path("C:\\Users\\prave\\Desktop\\IMG_2849.jpg")`

【问题讨论】:

  • 你能再具体一点吗?这些块是否像网格一样,因此将图像分成 2x2 块并将它们重新合并在一起?还是该块是较大图像的子图像?
  • 如果您知道每件作品的去向,您可以简单地使用 np.hstacknp.vstack ,但是是的,您需要更好地解释您的问题,正如 GenError 指出的那样,请通过How to askHow to create a minimum complete verifiable example
  • 我已经把图片分成了8*8块

标签: python opencv


【解决方案1】:

解决此问题的一种方法是使用 numpy.block 将块重新堆叠在一起:

import numpy as np

# 20x20 image
img = np.random.randint(0,9,(20,20))

# List of 4 5x20 image slices
sliced = np.split(img,4,axis=0)

# List of 4 lists of 4 5x5 image blocks
blocks = [np.split(img_slice,4,axis=1) for img_slice in sliced]

# stacking them back together
img_stacked = np.block(blocks)

# testing if the stacking works right
print((img==img_stacked).all())

但这只有在您拥有正确顺序的列表列表时才有效(首先拆分轴 0,然后是轴 1)。更一般地,hstackvstackconcatenate 可以将较小的数组(图像)放在一起。

【讨论】:

  • 先生,如果我想通过比较块哈希技术对图像进行重复数据删除,我应该怎么做
  • 我正在尝试实现最接近相同重复数据删除的 DICE 协议
  • 对不起,这不是我的专业领域
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
相关资源
最近更新 更多