0

Hi i've a script to run image process on a image. But i'm trying to get a loop or another way to read multiple images from a file

e.g

C:\Users\student\Desktop\Don\program (opencv version)\Images\move1

move1 contains images named as frame1.jpg , frame2.jpg , frame3.jpg...

The script i'm using to run the image process is something like

img = cv2.imread('frame1.jpg')


mimg = cv2.medianBlur(img,15) 

gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY) 

ret,th1 = cv2.threshold(gimg, 160,255,cv2.THRESH_BINARY)

ret,th2 = cv2.threshold(th1, 160,255,cv2.THRESH_BINARY_INV)

cv2.imwrite('threshbinaryinv.jpg', th2)

My script above could only read images that i manually keyed in e.g 'frame1.jg'. Sorry i'm very new to python. Thanks!

EDIT This the code i edited with you guys help.. still getting error as "Traceback (most recent call last): File "C:\Users\student\Desktop\Don\program (opencv version)\prog.py", line 32, in gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY) #convert RBG to Grayscale cv2.error: D:\Build\OpenCV\opencv-3.3.1\modules\imgproc\src\color.cpp:11048: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor"

CODE

path_of_images = 'C:/Users/student/Desktop/Don/program  (opencv version)/Images'

list_of_images = os.listdir(path_of_images)

for image in list_of_images:

img = cv2.imread(os.path.join(path_of_images, image))

mimg = cv2.medianBlur(img,15) 

gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY) 

ret,th1 = cv2.threshold(gimg, 160,255,cv2.THRESH_BINARY)

ret,th2 = cv2.threshold(th1, 160,255,cv2.THRESH_BINARY_INV) 

cv2.imwrite('threshbinaryinv.jpg', th2)
Dopa5
  • 11
  • 1
  • 2
  • Possible duplicate of [How to read images from a directory? (Python OpenCV)](https://stackoverflow.com/questions/48001890/how-to-read-images-from-a-directory-python-opencv) – Kinght 金 Jan 29 '18 at 07:39

2 Answers2

1

You can use os.listdir() to get the names of all images in your specified path which is "C:\Users\student\Desktop\Don\program (opencv version)\Images". Then you can loop over the names of images like :

import os    
import cv2
path_of_images = r"C:\Users\student\Desktop\Don\program (opencv version)\Images"
list_of_images = os.listdir(path_of_images)
for image in list_of_images:
    img = cv2.imread(os.path.join(path_of_images, image))
    """Your code here"""
pissall
  • 7,109
  • 2
  • 25
  • 45
  • What if OP wants images in particular order? – ZdaR Jan 29 '18 at 07:29
  • That should be specified in the question itself, isn't it? – pissall Jan 29 '18 at 07:31
  • @pissall hey, i tried the code.. but it also gave the same error as the one ZdaR gave.. os.listdir(ERROR)("C:\Users\student\Desktop\Don\program (opencv version)\Images") – Dopa5 Jan 29 '18 at 08:04
  • You can user r'your/path' or double forward slashes to avoid this problem. The answer in this post will help you : https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path – pissall Jan 29 '18 at 09:01
  • I have updated my answer. You need to provide the full path of the image. – pissall Jan 29 '18 at 09:29
  • Can you try using a raw string as I mentioned in my answer? Also post the error. – pissall Jan 30 '18 at 08:45
0

It can be done using a for loop and generating a new str file name and then processing it as:

IMG_FOLDER_PREFIX = "absolute/path/to/frame"
IMG_EXTENSION = ".jpg"
NUM_IMAGES = 10

for i in xrange(NUM_IMAGES):
    image_path = IMG_FOLDER_PREFIX + str(i) + IMG_EXTENSION
    img = cv2.imread(image_path)
    # Other Image Processing.

A better way to iterate images would be os.listdir, glob, etc. but in that case you may have lesser control over the order of files traversed.

ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • All the images are not named as 'frame1.jpg', 'frame2.jpg', etc. – pissall Jan 29 '18 at 07:30
  • 1
    Question says: *move1 contains images named as frame1.jpg , frame2.jpg , frame3.jpg...* ! – ZdaR Jan 29 '18 at 07:32
  • @pissall any more reasons to support your downvote ? Please help me to make this answer more informative. – ZdaR Jan 29 '18 at 07:33
  • And also says "My script above could only read images that i manually keyed in e.g 'frame1.jg'. Sorry i'm very new to python. Thanks! " – pissall Jan 29 '18 at 07:33
  • @ZdaR Hey! i tried your code from above. i got an error at IMG_FOLDER_PREFIX = "C:\Users\student\Desktop\Don\program (opencv version)\Images\move1" . the error was (unicode error) 'unicodeescape' codec can't bytes in position 2-3: truncated \UXXXXXXXX escape – Dopa5 Jan 29 '18 at 08:02