This algorithm consists of reading all images in a folder ending with clipped.tiff and the for loop, which changes the gamma value of all scanned images, is to be accelerated.
How is it possible to parallelize and speed up this simple algorithm?
SinglestreetsFolder = "/dop_shapefile/"
shp_list = [x for x in os.listdir(SinglestreetsFolder) if x.endswith("clipped.tiff")]
for i in range(0, len(shp_list), 1):
originalPath = "/dop_shapefile/" + shp_list[i]
original = cv2.imread(originalPath)
adjusted = adjust_gamma(original, gamma=0.3)
cv2.imwrite("/gamma/" + shp_list[i] + "_gamma.tiff", adjusted)
print "status_Gamma: ", i + 1, "/", len(shp_list)
Another algorithm
This algorithm consists of reading all images in a folder ending with .tiff and the for loop, that executes the ConnectedComponentLabeling algorithm (with customizations) to speed up.
SinglestreetsFolder = "/gabor/"
shp_list = [x for x in os.listdir(SinglestreetsFolder) if x.endswith(".tiff")]
img = cv2.imread(SinglestreetsFolder + shp_list[0], 0)
wholeimage = np.zeros(shape=(len(img), len(img[0]), 3))
erosion = np.ones((2, 2), np.uint8) # kernel: erosion
for j in range(0, len(shp_list), 1):
if "motorway" in shp_list[j]:
N = 40 # pixel threshold (city: ca. 10, motorway: ca. 40)
else:
N = 10 # pixel threshold (city: ca. 10, motorway: ca. 40)
img = cv2.imread(SinglestreetsFolder + shp_list[j], 0)
connectivity = 8 # 4- OR 8-connectivity connected component labeling
if "reverse" in shp_list[j]:
img = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)[1] # ensure binary
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, erosion)
retval, labels = cv2.connectedComponents(img, connectivity)
num = labels.max()
# If the count of pixels less than a threshold, then set pixels to `0` (background)
for i in range(1, num + 1):
pts = np.where(labels == i)
if len(pts[0]) < N:
labels[pts] = 0
# Map component labels to hue val
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(labels)
labeled_img = cv2.merge([blank_ch, blank_ch, blank_ch])
# set bg label to black
labeled_img[label_hue == 0] = 0
wholeimage = np.where(labeled_img == 0, wholeimage, labeled_img)
else:
img = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)[1] # ensure binary
retval, labels = cv2.connectedComponents(img, connectivity)
num = labels.max()
# If the count of pixels less than a threshold, then set pixels to `0` (background)
for i in range(1, num + 1):
pts = np.where(labels == i)
if len(pts[0]) < N:
labels[pts] = 0
# Map component labels to hue val
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(labels)
labeled_img = cv2.merge([blank_ch, blank_ch, blank_ch])
# set bg label to black
labeled_img[label_hue == 0] = 0
wholeimage = np.where(labeled_img == 0, wholeimage, labeled_img)
print "status_CCL: ", j + 1, "/", len(shp_list)
cv2.imwrite("/ccl/streets_gabor_ccl.tiff", wholeimage)
My solution, but "wholeimage" is black at the end
SinglestreetsFolder = "/gabor/"
shp_list = [x for x in os.listdir(SinglestreetsFolder) if x.endswith(".tiff")]
def gabor(params):
img = cv2.imread(SinglestreetsFolder + shp_list[0], 0)
wholeimage = np.zeros(shape=(len(img), len(img[0]), 3))
erosion = np.ones((2, 2), np.uint8) # kernel: erosion
j, image_name = params
if "motorway" in image_name:
N = 40 # pixel threshold (city: ca. 10, motorway: ca. 40)
else:
N = 10 # pixel threshold (city: ca. 10, motorway: ca. 40)
img = cv2.imread(SinglestreetsFolder + image_name, 0)
connectivity = 8 # 4- OR 8-connectivity connected component labeling
if "reverse" in image_name:
img = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)[1] # ensure binary
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, erosion)
# img = cv2.erode(img, erosion, iterations=1)
retval, labels = cv2.connectedComponents(img, connectivity)
num = labels.max()
# If the count of pixels less than a threshold, then set pixels to `0` (background)
for i in range(1, num + 1):
pts = np.where(labels == i)
if len(pts[0]) < N:
labels[pts] = 0
# Map component labels to hue val
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(labels)
labeled_img = cv2.merge([blank_ch, blank_ch, blank_ch])
# set bg label to black
labeled_img[label_hue == 0] = 0
wholeimage = np.where(labeled_img == 0, wholeimage, labeled_img)
else:
img = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)[1] # ensure binary
retval, labels = cv2.connectedComponents(img, connectivity)
num = labels.max()
# If the count of pixels less than a threshold, then set pixels to `0` (background)
for i in range(1, num + 1):
pts = np.where(labels == i)
if len(pts[0]) < N:
labels[pts] = 0
# Map component labels to hue val
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(labels)
labeled_img = cv2.merge([blank_ch, blank_ch, blank_ch])
# set bg label to black
labeled_img[label_hue == 0] = 0
wholeimage = np.where(labeled_img == 0, wholeimage, labeled_img)
if __name__ == '__main__':
p = Pool()
list(p.imap(gabor, enumerate(shp_list)))
cv2.imwrite(/ccl/streets_gabor_ccl.tiff", wholeimage)
Poolmight be useful for your needs, if the actions you want to perform are independent.