I am somehow new to python and would like to get rid of use of for loop as it makes the program very slow. So I would like vectorize it. however, I am not sure how would I use it with two conditions Here I shared the code and would like someone could hit me..
image to be imported
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
def nothing(color):
pass
cv.namedWindow('Trackbar ')
cv.resizeWindow('Trackbar',800,200)
cv.createTrackbar('v_min','Trackbar ',0,255,nothing)
cv.createTrackbar('v_max','Trackbar ',0,255,nothing)
while True:
img_org = cv.imread('sat.png',cv.IMREAD_COLOR)
img_np=np.array(img_org)
img2_np=np.array(img_org)
value_min=cv.getTrackbarPos('v_min','Trackbar ')
value_max=cv.getTrackbarPos('v_max','Trackbar ')
height, width = img_np.shape[:2]
for j in range (0,width) :
for i in range (0,height):
for k in range (0,2):
if (value_min < img_np[i,j,k] <= value_max):
img2_np[i,j] = ((img_np[i,j]-value_min)/(value_max-value_min))*255
else:
img2_np[i,j] = 0
#Here is the vectorization to remove all for and if loop (what I think )
# img_np[(value_min < img_np.any() <= value_max)]=((img_np-value_min)/(value_max-value_min))*255 | img_np[(value_min > img_np.any() >= value_max)]=0
cv.imshow('FINAL IMAGE',img_np)
cv.imshow('image',img_org)
kk = cv.waitKey(1000) & 0xFF # large wait time to remove freezing
if kk == 113 or kk == 27:
break
I am expecting to convert or I mean to remove all (for, if/else loop) with vectorize so less time.. so I tried first to with one if condition and seems working however, when I tried to add with two conditions like (|) else seems not working
img2_np[i][j][k]
in your if/else statement? I'm not quite sure what you want to happen when k=0 gives you theif
side and k=1 gives you the else side. (ps. In numpy, it's equivalent and more conventional to writeimg2_np[i, j, k]
wheni
,j
, andk
are integers.