2

Here is a python code to read a sequence ( 0.jpg ...1052.jpg) of images with VideoCapture module of openCV.

import cv2
import numpy
cap = cv2.VideoCapture("/path to/RGB/I%04d.jpg",cv2.CAP_IMAGES).

while(1):
   ret,frame = cap.read()
   cv2.imshow('image',frame)
   cv2.waitKey()
   print(frame.shape)

I get this error

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow.

It seems that the frame object is empty!

petezurich
  • 9,280
  • 9
  • 43
  • 57
Jeanne
  • 1,241
  • 3
  • 19
  • 28
  • 1
    Well, that's what you get when you keep printing even when there are no more images to read. – Gerard H. Pille Mar 05 '18 at 18:59
  • Its probably trying to read image 1053 - and `cap` is None. - Anyhow - you did not really formulate a question, more state the obvious, so whats up? – Patrick Artner Mar 05 '18 at 19:00
  • Good read btw: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) - I think nr 3 today – Patrick Artner Mar 05 '18 at 19:02
  • @GerardH.Pille, I mean i get an error from the first iteration. – Jeanne Mar 05 '18 at 19:12
  • Are you trying to capture images, or show information of existing images? Before printing, show the contents of "ret". But I can't find documentation explaining the return value. – Gerard H. Pille Mar 05 '18 at 19:15
  • @GerardH.Pille, I edit the question by adding lines to show image but it does not work. – Jeanne Mar 05 '18 at 19:44
  • You can do as you please, or you can do what I tell you to. Before printing or showing, test the return value (ret) of cap.read. – Gerard H. Pille Mar 05 '18 at 20:41
  • @GerardH.Pille, The problem comes form the names of images. Instead of having this format 0.jpg..., I have to get this format 0000.jpg... – Jeanne Mar 05 '18 at 20:44
  • The problem comes also from ignoring the return value of cap read. see http://stackoverflow.com/questions/35242735/ddg#35245814 and try "/path to/RGB/I%d.jpg" – Gerard H. Pille Mar 05 '18 at 20:50

2 Answers2

1
cap = cv2.VideoCapture("<path>/%04.jpg",cv2.CAP_IMAGES)

Either rename your images to be padded with 3 zeros to the left sinze %04 pads with zero to left to keep number such as: 0001, 0002, ..., 1000, etc.

Or I would run a for loop that iterates from 1, 2, ..., n and from there capture img name and then do image reading with:

import cv2
import numpy

# Make sure range starts correctly.
nbrofpictures = 1000
for x in range(1, nbrofpictures):
    #make sure path is correct.
    path = '/<yourpath>/%d.jpg' % x
    print (path)
    frame = cv2.imread(path)
    cv2.imshow('image',frame)
    cv2.waitKey()
    print(frame.shape)
Emir Husic
  • 717
  • 5
  • 16
  • it was just a typo. The problem is in the first line "I%04d.jpg" – Jeanne Mar 05 '18 at 20:00
  • How to specify it? I think I have to rename image from 0.jpg to 0000.jpg....1052.jpg – Jeanne Mar 05 '18 at 20:11
  • I can't yet find a working solution for number 1,2,3,4...1000. I however have a solution if you have numbers like 0001, 0002, 0003, ..., 1000. /%04d.jpg",cv2.CAP_IMAGES) – Emir Husic Mar 05 '18 at 20:15
1

The problem comes from the name of images files.

1) script shell to rename files.

images=$(for i in `ls *.jpg`; do LEN=`expr length $i`; echo  $i; done | sort -n)
j=1
for i in $images; do
  new=$(printf "%04d.jpg" "$j") #04 pad to length of 4
  mv -i -- "$i" "$new"
  let j=j+1
   #we can add a condition on j to rename just the first 999 images.
done

2) read images sequence image:

import cv2
import numpy
cap = cv2.VideoCapture("/path to/RGB/I%04d.jpg",cv2.CAP_IMAGES).

while(1):
   ret,frame = cap.read()
   cv2.imshow('image',frame)
   cv2.waitKey()
   print(frame.shape)
Jeanne
  • 1,241
  • 3
  • 19
  • 28