I'm trying to use the Zbar library's QR code detection methods on images I extract with OpenCV's camera methods. Normally the QR code detection methods work with images (jpg, png, etc.) on my computer, but I guess the captured frames of OpenCV are different.
Is there a way of making the captured frame into a PIL Image?
我正在尝试使用Zbar库的QR码检测方法对我用OpenCV的相机方法提取的图像。通常,QR码检测方法可以在我的计算机上使用图像(jpg,png等),但我猜测OpenCV的捕获帧是不同的。有没有办法将捕获的帧变成PIL图像?
Thank you.
from PIL import Image
import zbar
import cv2.cv as cv
capture = cv.CaptureFromCAM(1)
imgSize = cv.GetSize(cv.QueryFrame(capture))
img = cv.QueryFrame(capture)
#SOMETHING GOES HERE TO TURN FRAME INTO IMAGE
img = img.convert('L')
width, height = img.size
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
zbar_img = zbar.Image(width, height, 'Y800', img.tostring())
# scan the image for barcodes
scanner.scan(zbar_img)
for symbol in zbar_img:
print symbol.data
3 个解决方案
#1
66
With the python CV2, you can also do this:
使用python CV2,您还可以执行以下操作:
import Image, cv2
cap = cv2.VideoCapture(0) # says we capture an image from a webcam
_,cv2_im = cap.read()
cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im)
pil_im.show()
#2
8
I think I may have found the answer. I'll edit later with results.
我想我可能找到了答案。我稍后会用结果编辑。
OpenCV to PIL Image
OpenCV到PIL图像
import Image, cv
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
Source: http://opencv.willowgarage.com/documentation/python/cookbook.html
#3
4
Are you trying to obtain a RGB image? If that is the case, you need to change your parameters from this:
你想要获得RGB图像吗?如果是这种情况,您需要更改以下参数:
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
to that:
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 3)
pi = Image.fromstring("RGB", cv.GetSize(cv_im), cv_im.tostring())
since it is documented almost nowhere, but the 'L' parameter of Image.fromstring is for 8-bit B&W images. Besides, you need to change the argument of your cv.CreateImage function from 1 (single channel image) to 3 (3 channels=RGB).
因为它几乎没有记录,但Image.fromstring的'L'参数适用于8位B&W图像。此外,您需要将cv.CreateImage函数的参数从1(单通道图像)更改为3(3通道= RGB)。
Hope it works for you. Cheers
希望对你有效。干杯
#1
66
With the python CV2, you can also do this:
使用python CV2,您还可以执行以下操作:
import Image, cv2
cap = cv2.VideoCapture(0) # says we capture an image from a webcam
_,cv2_im = cap.read()
cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im)
pil_im.show()
#2
8
I think I may have found the answer. I'll edit later with results.
我想我可能找到了答案。我稍后会用结果编辑。
OpenCV to PIL Image
OpenCV到PIL图像
import Image, cv
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
Source: http://opencv.willowgarage.com/documentation/python/cookbook.html
#3
4
Are you trying to obtain a RGB image? If that is the case, you need to change your parameters from this:
你想要获得RGB图像吗?如果是这种情况,您需要更改以下参数:
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
to that:
cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 3)
pi = Image.fromstring("RGB", cv.GetSize(cv_im), cv_im.tostring())
since it is documented almost nowhere, but the 'L' parameter of Image.fromstring is for 8-bit B&W images. Besides, you need to change the argument of your cv.CreateImage function from 1 (single channel image) to 3 (3 channels=RGB).
因为它几乎没有记录,但Image.fromstring的'L'参数适用于8位B&W图像。此外,您需要将cv.CreateImage函数的参数从1(单通道图像)更改为3(3通道= RGB)。
Hope it works for you. Cheers
希望对你有效。干杯