Divide an image into 5x5 blocks in python and compute histogram for each block

后端 未结 5 595
北恋
北恋 2020-12-28 19:32

Using Python, I have to:

  • Divide a Test_Image and Reference_image into 5x5 blocks,
  • Compute a histogram for each block, and
5条回答
  •  有刺的猬
    2020-12-28 20:20

    I have write this code two automatically split image into n rows and m columns. m and n are arguments and it is easily modifiable. After that it is easy compute histogram for each block which are also saving into the folder named patches.

    # Image path, number of rows 
    # and number of columns 
    # should be provided as an arguments
    import cv2
    import sys
    import os
    
    
    if not os.path.exists('patches'):
        os.makedirs('patches')
    
    
    
    nRows = int(sys.argv[2])
    # Number of columns
    mCols = int(sys.argv[3])
    
    # Reading image
    img = cv2.imread(sys.argv[1])
    #print img
    
    #cv2.imshow('image',img)
    
    # Dimensions of the image
    sizeX = img.shape[1]
    sizeY = img.shape[0]
    
    print(img.shape)
    
    
    for i in range(0,nRows):
        for j in range(0, mCols):
            roi = img[i*sizeY/nRows:i*sizeY/nRows + sizeY/nRows ,j*sizeX/mCols:j*sizeX/mCols + sizeX/mCols]
            cv2.imshow('rois'+str(i)+str(j), roi)
                    cv2.imwrite('patches/patch_'+str(i)+str(j)+".jpg", roi)
    
    
    
    cv2.waitKey()
    

提交回复
热议问题