I'm new to Python. I'm using Glob to Batch Process multiple image files. When I run my code, it processes and saves all image files as it should, but only one image file is affected by my code all other image files are untouched by my code. I need all images to be affected by my code. When I run my code on the two Checker Board image files, only one image file is affected the other image file is untouched by my code as shown in the images.
I tried researching more to find a solution, but I couldn't find one here is my code.
Thanks in advance,
import numpy as np
import cv2
import os
import glob
from skimage import img_as_ubyte
image_list = []
path = "opencv/imgs/*.*"
for file in glob.glob(path):
print(file)
img = cv2.imread(file)
a = cv2.imread(file)
image_list.append(img)
print(a)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,200,apertureSize=3)
img_blur = cv2.GaussianBlur(gray, (3,5), 2)
edges = cv2.Canny(image=img_blur, threshold1=25, threshold2=50) # Canny Edge Detection
# Apply HoughLinesP method to
# to directly obtain line end points
lines_list =[]
lines = cv2.HoughLinesP(
edges, # Input edge image
1, # Distance resolution in pixels
np.pi/2, # Angle resolution in radians
threshold=225, # Min number of votes for valid line
minLineLength=60, # Min allowed length of line
maxLineGap=400) # Max allowed gap between line for joining them
# Iterate over points
for points in lines:
# Extracted points nested in the list
x0,y1,x0,y2=points[0]
# Draw the lines joing the points
# On the original image
cv2.line(img,(x0,y1),(x0,y2),(0,255,0),4)
# Maintain a simples lookup list for points
lines_list.append([(x0,y1),(x0,y2)])
image_list = np.array(image_list)
# Save the result image
img_number = 1
for image in range(image_list.shape[0]):
input_img = image_list[image,:,:]
lines_image = img_as_ubyte(input_img)
cv2.imwrite(path+str(img_number)+".jpg", lines_image)
img_number +=1
print()
(andprint(type(...))
,print(len(...))
, etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"
and it helps to see what code is really doing.for
-loop. You calculateedge
inside for-loop (so you do it for all images) but later you calculateHoughLinesP
outsidefor
-loop so you do it only for one file. And rest of code has the same problem. If you would useprint()
to see which line of code is executed and what you have in variables then you should see problem. Maybe create function which gets filename and it make all calculations for one file - and it save this file - and later run this function infor file in glob.glob(path):
for
-loop. You should write directlyimg
- and you should do it insidefor file in glob.glob(path):