Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Python crop not working

Hello, I am relatively new to OpenCV. I have gleaned from tutorials and such that you can crop by using this script:

import cv2
import numpy as np
import video

cam = cv2.VideoCapture(0)
ret,vis = cam.read()    
crop = vis[100:400, 100:300]    
cv2.imshow("Img",vis)
cv2.imshow("Crop",crop)    
cv2.waitKey(0)

And this works fine. I get no errors.

However, when i put it into my main script, it doesn't work, i've narrowed it down to this section of code:

def PicTake(self):
    ret,vis = self.cam.read()
    x1,y1 = self.selection[0]
    x2,y2 = self.selection[1]
    a = 0
    taken = 0
    while taken == 0:
        if cv2.imread("C:\Python27\opencv\samples\python2\Major\Test"+str(a)+".png") == None:
            crop = vis[x1:y1, x2:y2]
            print crop
            cv2.imshow("crop",crop)
            cv2.imwrite("C:\Python27\opencv\samples\python2\Major\Test"+str(a)+".png",crop)
            taken = 1
        else:
            a+=1
    return ("Picture Taken")

where self.selection is just a list of two tuples [(x1,y1),(x2,y2)]. After the first if statement, print crop returns "[]" and empty list.

So yeah, why does it work with numbers and other situations fine, but not here?!

Any help is greatly appreciated, thanks!