0

My code is the following:

import cv2; import numpy as np

class MyClass:
    def __init__(self,imagefile):
        self.image = cv2.imread(imagefile)

        #image details
        self.h,self.w = self.image.shape[:2]
        #self.bPoints, self.wPoints = np.array([[0,0]]),np.array([[0,0]])
        self.bPoints, self.wPoints = [],[]

        #CAUTION! Points are of the form (y,x)
        # Point filtering
        for i in xrange(self.h):
            for j in xrange(self.w):
                if self.th2.item(i,j) == 0:
                    #self.bPoints = np.append([[i,j]], self.bPoints, axis=0)
                    self.bPoints.append((i,j))
                else:
                    self.wPoints.append((i,j))
                    #self.wPoints = np.append([[i,j]], self.wPoints, axis=0)

        #self.bPoints = self.bPoints[:len(self.bPoints) - 1]
        #self.wPoints = self.wPoints[:len(self.wPoints) - 1]
        self.bPoints, self.wPoints = np.array(self.bPoints), np.array(self.wPoints)

I want to find and separate the white from the black points. I have commented the lines that show a possible (but very-very slow) solution via numpy. Can you recommend me a better and faster solution? I will appreciate it if you do so!

Thanks

2
  • maybe it needs adding a sentence on what you're trying to achieve
    – berak
    Commented Jul 28, 2014 at 17:37
  • As I have mentioned above, I want to append the black points of the binary image to self.bPoints and the white ones to self.wPoints array whose shape is (2,2)
    – bolzano
    Commented Jul 28, 2014 at 19:37

1 Answer 1

1

I'm assuming self.th2 is a numpy array. This might take some adjustment if that is not the case. Basically, this uses the np.where function to determine all the indices which are 0 or 255.

import cv2; import numpy as np

class MyClass:
    def __init__(self,imagefile):
        self.image = cv2.imread(imagefile)

        #image details
        self.h,self.w = self.image.shape[:2]
        #self.bPoints, self.wPoints = np.array([[0,0]]),np.array([[0,0]])
        self.bPoints, self.wPoints = [],[]

        #CAUTION! Points are of the form (y,x)
        # use the np.where method instead of a double loop. 
        # make sure self.th2 is a numpy array
        indx = np.where(self.th2==0)
        for i,j in zip(indx[0], indx[1]):
            self.bPoints.append((i,j))

        indx = np.where(self.th2==255)
        for i,j in zip(indx[0], indx[1]):
            self.wPoints.append((i,j))

        # Point filtering
        #for i in xrange(self.h):
        #    for j in xrange(self.w):
        #        if self.th2.item(i,j) == 0:
        #            #self.bPoints = np.append([[i,j]], self.bPoints, axis=0)
        #            self.bPoints.append((i,j))
        #        else:
        #            self.wPoints.append((i,j))
        #            #self.wPoints = np.append([[i,j]], self.wPoints, axis=0)

        #self.bPoints = self.bPoints[:len(self.bPoints) - 1]
        #self.wPoints = self.wPoints[:len(self.wPoints) - 1]
        self.bPoints, self.wPoints = np.array(self.bPoints), np.array(self.wPoints)
2
  • thank you! much faster! but note that white value is 255 and not 1
    – bolzano
    Commented Jul 28, 2014 at 20:33
  • I was thinking of np.where() but I didn't know how to manipulate it! It's undoubtedly a great way to make indexes!
    – bolzano
    Commented Jul 29, 2014 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.