Skip to content

Instantly share code, notes, and snippets.

@cdsousa
Last active October 24, 2023 16:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdsousa/5c707f767cd766bb9df79f837b90a1a9 to your computer and use it in GitHub Desktop.
Save cdsousa/5c707f767cd766bb9df79f837b90a1a9 to your computer and use it in GitHub Desktop.
module OpenCV
export CvCapture, release, grabFrame, retrieveFrameIplImage, retrieveFrame, namedWindow, showImage, waitKey, destroyAllWindows
import Images
struct CvCapture
cvCapturePtr::Ptr{Cvoid}
sizes::Vector{Cint}
function CvCapture(device::Int = 0)
ptr = ccall((:cvCreateCameraCapture, "libopencv_highgui"), Ptr{Cvoid}, (Cint,), device)
new(ptr, zeros(Cint, 3))
end
end
function CvCapture(device::Int, resolution, fps=nothing)
cap = CvCapture(device)
w, h = resolution
CV_CAP_PROP_FRAME_WIDTH = 3
CV_CAP_PROP_FRAME_HEIGHT = 4
ccall((:cvSetCaptureProperty, "libopencv_highgui"), Cint, (Ptr{Cvoid}, Cint, Cdouble), cap.cvCapturePtr, CV_CAP_PROP_FRAME_WIDTH, resolution[1])
ccall((:cvSetCaptureProperty, "libopencv_highgui"), Cint, (Ptr{Cvoid}, Cint, Cdouble), cap.cvCapturePtr, CV_CAP_PROP_FRAME_HEIGHT, resolution[2])
gw = UInt(ccall((:cvGetCaptureProperty, "libopencv_highgui"), Cdouble, (Ptr{Cvoid}, Cint), cap.cvCapturePtr, CV_CAP_PROP_FRAME_WIDTH))
gh = UInt(ccall((:cvGetCaptureProperty, "libopencv_highgui"), Cdouble, (Ptr{Cvoid}, Cint), cap.cvCapturePtr, CV_CAP_PROP_FRAME_HEIGHT))
if w != gw || h != gh
release(cap)
error("Wrong resolution provided by source, asked for $(w)x$(h), got $(gw)x$(gh).")
end
if fps!=nothing
CV_CAP_PROP_FPS = 5
ccall((:cvSetCaptureProperty, "libopencv_highgui"), Cint, (Ptr{Cvoid}, Cint, Cdouble), cap.cvCapturePtr, CV_CAP_PROP_FPS, fps)
# gfps = ccall((:cvGetCaptureProperty, "libopencv_highgui"), Cdouble, (Ptr{Cvoid}, Cint), cap.cvCapturePtr, CV_CAP_PROP_FPS)
# if fps != gfps
# release(cap)
# error("Wrong FPS provided by source, asked for $fps, got $gfps.")
# end
end
cap
end
release(cap::CvCapture) = ccall((:cvReleaseCapture, "libopencv_highgui"), Cvoid, (Ptr{Ptr{Cvoid}},), cap.cvCapturePtr)
grabFrame(cap::CvCapture) = Bool(ccall((:cvGrabFrame, "libopencv_highgui"), Cint, (Ptr{Cvoid},), cap.cvCapturePtr))
struct IplImage; ptr::Ptr{Cvoid}; end
function retrieveFrameIplImage(cap::CvCapture)
IplImage(ccall((:cvRetrieveFrame, "libopencv_highgui"), Ptr{Cvoid}, (Ptr{Cvoid}, Cint), cap.cvCapturePtr, 0))
end
function retrieveFrame(cap::CvCapture)
iplimage = ccall((:cvRetrieveFrame, "libopencv_highgui"), Ptr{Cvoid}, (Ptr{Cvoid}, Cint), cap.cvCapturePtr, 0)
CV_8UC3 = 16
cveltype = ccall((:cvGetElemType, "libopencv_core"), Cint, (Ptr{Cvoid},), iplimage)
@assert cveltype == CV_8UC3
ccall((:cvGetDims, "libopencv_core"), Cint, (Ptr{Cvoid}, Ptr{Cint}), iplimage, cap.sizes)
@assert cap.sizes[3] == 0
h, w = cap.sizes[1], cap.sizes[2]
data = Ref{Ptr{Cuchar}}(0)
ccall((:cvGetRawData, "libopencv_core"), Cvoid, (Ptr{Cvoid}, Ptr{Ptr{Cuchar}}, Ptr{Cvoid}, Ptr{Cvoid}), iplimage, data, C_NULL, C_NULL)
unsafe_wrap(Matrix{Images.BGR{Images.N0f8}}, Ptr{Images.BGR{Images.N0f8}}(data[]), (w, h))
end
namedWindow(windowName, mode = 0) = ccall((:cvNamedWindow, "libopencv_highgui"), Cint, (Cstring, Cint), windowName, mode)
showImage(windowName, iplimage::IplImage) = ccall((:cvShowImage, "libopencv_highgui"), Cvoid, (Cstring, Ptr{Cvoid}), windowName, iplimage.ptr)
waitKey(milliseconds) = ccall((:cvWaitKey, "libopencv_highgui"), Cint, (Cint,), milliseconds)
destroyAllWindows() = ccall((:cvDestroyAllWindows, "libopencv_highgui"), Cvoid, ())
end
###################
# Example
cap = OpenCV.CvCapture(0, (1280,720))
win = OpenCV.namedWindow("cap")
for i=1:60
ret = OpenCV.grabFrame(cap)
if ret
iplImg = OpenCV.retrieveFrameIplImage(cap)
OpenCV.showImage("cap", iplImg)
OpenCV.waitKey(1)
else
break
end
end
OpenCV.destroyAllWindows()
OpenCV.waitKey(1)
OpenCV.release(cap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment