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)
1

2 Answers 2

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"""
Sign up to request clarification or add additional context in comments.

6 Comments

What if OP wants images in particular order?
That should be specified in the question itself, isn't it?
@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")
You can user r'your/path' or double forward slashes to avoid this problem. The answer in this post will help you : stackoverflow.com/questions/18084554/…
I have updated my answer. You need to provide the full path of the image.
|
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.

5 Comments

All the images are not named as 'frame1.jpg', 'frame2.jpg', etc.
Question says: move1 contains images named as frame1.jpg , frame2.jpg , frame3.jpg... !
@pissall any more reasons to support your downvote ? Please help me to make this answer more informative.
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! "
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.