0

I have a binary image of 650x650 size. I want to create patches of 50x50. This means that I need 169 patches. I want to examine if there is in every patch is at least "ONE" element. I need also the result to be pairs of every patch.

Here there is an example of 2d: 2d example

So far so good.When I implement the view_as_blocks function from skimage.util.shape it returns a list of (13,13,50,50).

The way i search for "ONES" will be numpy.where but i am lost in dimensions... here is my code:

def distinguish_patches_for_label(image_path):
    im1=cv2.imread(image_path)

    im2 = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
    im3=(im2>100).astype(np.uint8) 
    im4 = cv2.resize(im3, (650,650)) 
    patches=view_as_blocks(im4, block_shape=(50,50))

    for i in range(patches.shape[0]):
        for j in range(patches.shape[0]): 
            indexes_normal=list(zip(*np.where(patches[i,j,:,:]== 0)))

    for i in range(patches.shape[0]):
        for j in range(patches.shape[0]):


            indexes_XDs=list(zip(*np.where(patches[i,j,:,:]== 1)))


    list1=[]
    list2=[]
    for i in range(len(indexes_normal)):
        list1.append(indexes_normal[i][0])
        list2.append(indexes_normal[i][1])
    zipped_indexes_normal=list(zip(list1,list2))


    list3=[]
    list4=[]
    for i in range(len(indexes_XDs)):
        list3.append(indexes_XDs[i][0])
        list4.append(indexes_XDs[i][1])
    zipped_indexes_XDs=list(zip(list3,list4))


    return zipped_indexes_normal,zipped_indexes_XDs
  • 1
    Your question would gain with more precision as to where is your problem exactly. Don't just dump a full code sheet like that and let people decipher what is inside... Guide the people that are willing to help you. Also always provide your error/output message. – Atralb Nov 17 '19 at 08:04

1 Answers1

1

I'm not exactly sure what you are trying to get as your output, but the below will find the number of 1s in each patch:

import numpy as np
from skimage import io, util


def count_ones_in_patches(image_path):
    image = io.imread(image_path)
    patches = util.view_as_blocks(image, (50, 50))
    ones_per_patch = np.sum(patches, axis=(2, 3))
    return ones_per_patch

Using the numpy axis= keyword argument is a very good idea in general. =)

Juan
  • 5,433
  • 21
  • 23
  • Thank you for replying. I am not interested in counting 'ones' but just identifying if there is at least one 'one' in the patch!!.. By the way, what does axis=(2, 3) in a 4-dimensional array represent? – THEODOROPOULOS DIMITRIS Nov 19 '19 at 11:00
  • @THEODOROPOULOSDIMITRIS checking if there is a one in the patch is then a matter of: `ones_present = count_ones_in_patches(image_path) > 0`. `axis=(2, 3)` sums values along axes 2 and 3 (remember that Python indexing starts at 0, so these are the last two axes). – Juan Nov 20 '19 at 22:12