我已經拍攝的圖像,使用下面的代碼我的攝像頭:opencv的cv.GetSubRect和PIL的.crop()有區別嗎?
cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 960)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 544)
def record():
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
splitter(frame, 4)
我試圖把它與cv.GetSubRect裁剪成小塊:
def splitter(image, split):
width, height = cv.GetSize(image)
x = width/split
y = height/split
# Form the 4-tuples.
img_parts = []
for y_pos in range(split):
for x_pos in range(split):
xy_section = (x_pos*x, y_pos*y, (x_pos+1)*x, (y_pos+1)*y)
# print xy_section
img_parts.append(xy_section)
# The cropping.
cropped_parts = []
for part in img_parts:
# print "part "+str(part)
cropped = cv.GetSubRect(image, part) # Error is thrown here
cropped_parts.append(cropped)
但我不斷收到錯誤消息
OpenCV Error: Incorrect size of input array() in cvGetSubRect, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 1262
所拍攝的圖像的大小爲960x544,而我用4元組,它的下面是前四個列表拆分它:
- (0,0,240,136)
- (240,0,480,136)
- (480,0,720,136)
- (720,0,960,136 )
矩形按該順序進行取出,並在嘗試獲取第三個4元組(480,0,720,136)的矩形時引發錯誤。原始圖像比(960x544)大,所以出了什麼問題?一個相同的方法使用image.crop()在Python的PIL中工作,但opencv的替代方法似乎工作有點不同,我只是不知道如何?
找出問題所在。 opencv和pil在種植中使用4元組有很大的區別。我明天會更新答案。 – hannu40k 2012-07-24 20:31:10