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