0

We know that we have thresholds in python, binary, inv binary, tozero, truncate and otsu, but problem with me is i want to apply first of all range threshold that is, i want to change color of pixels that are in specific range say, all pixels having color 0-100 should be changed to white, remaining should have same colors as they had.

I did it via looping the image, i am just wondering is there faster and better way to do it in python?

def ApplyCustomRangeThreshold(self,min = 0,max = 100):
    print self.sceneImage.shape
    itemVal = 0

    imageWidth = self.sceneImage.shape[1]
    imageHeight = self.sceneImage.shape[0]

    xPos = 0
    yPos = 0
    print imageHeight
    print imageWidth

    totalPx = 0

    while xPos < imageWidth:
        while yPos < imageHeight:
            currentPx = self.sceneImage[yPos,xPos]
            totalPx = totalPx + 1
            if currentPx[0] >= min and currentPx[0] < 100:
                self.sceneImage.itemset((yPos, xPos, 0), 255)
                self.sceneImage.itemset((yPos, xPos, 1), 255)
                self.sceneImage.itemset((yPos, xPos, 2), 255)

            yPos = yPos + 1

        yPos = 0
        xPos = xPos + 1


    print totalPx
    print self.sceneImage.size



    _,self.sceneImage = cv2.threshold(self.sceneImage,200,255,cv2.THRESH_BINARY)

I solved the problem by using np liberary to move in the image using condition, technique called masking so now the above code looks like this all the pixels that are in range of min and max would remain same all other will change to the value provided in changeTo

def ApplyCustomRangeThreshold(self,image,min = 120,max = 200,changeTo = 255):
  maska =  image >= min
  maskb =  image <= max
  actualMask = np.logical_not(np.logical_and(maska,maskb))

  #all those pixels that are not satisfying condition mask a and b

  image[ np.where ( actualMask ) ] = changeTo 
  _,image= cv2.threshold(image,200,255,cv2.THRESH_BINARY) #to make image in black and white
AbdulMueed
  • 1,327
  • 12
  • 19
  • 1
    _"We know that we have thresholds in python"_. Huh? I've never heard of thresholds before. Are you talking about a third-party library? – Kevin Jan 04 '16 at 12:54
  • Based on a quick search, looks like a opencv function - http://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html#gsc.tab=0 – Anshu Prateek Jan 04 '16 at 13:06
  • No kevin, i guess its my mistake, i meant threshold in opencv/cv2.threshold used for simple/basic image filteration operations. – AbdulMueed Jan 06 '16 at 13:15
  • I did this by using simple np techniques i know this could be done so now my code looks different i am updating my question with the correct and proper answer. by the way thanks for the link that was good for increasing threshold knowledge – AbdulMueed Jan 06 '16 at 13:18

0 Answers0