Last active
November 7, 2024 06:58
-
-
Save k-okada/9f0a6e7db67ca18f31c8d6ed9010c5b0 to your computer and use it in GitHub Desktop.
example of mixing opencv and opengl programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
#include <GL/glut.h> | |
cv::VideoCapture cap; | |
void opencv_init() | |
{ | |
cap = cv::VideoCapture(0); | |
} | |
void opencv_loop() | |
{ | |
cv::Mat frame; | |
cap.read(frame); | |
cv::imshow("Frame", frame); | |
unsigned char key = cv::waitKey(1); | |
if ( key == 27 ) | |
{ | |
exit(1); | |
} | |
} | |
void keyboard(unsigned char key, int x, int y) | |
{ | |
if ( key == 27 ) | |
{ | |
printf("exitting"); | |
exit(1); | |
} | |
} | |
void init(int width, int height) | |
{ | |
glClearColor(0.0, 0.0, 0.0, 1.0); | |
glEnable(GL_DEPTH_TEST); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0); | |
} | |
void display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluLookAt(0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
glColor3f(1.0, 0.0, 0.0); | |
glutWireTeapot(1.0); | |
glFlush(); | |
} | |
void reshape(int width, int height) | |
{ | |
glViewport(0, 0, width, height); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0); | |
} | |
void idle() | |
{ | |
printf("running within idle callback\n"); | |
opencv_loop(); | |
} | |
int main (int argc, char **argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); | |
glutInitWindowSize(300,300); | |
glutInitWindowPosition(100, 100); | |
glutCreateWindow("teapot"); | |
glutDisplayFunc(display); | |
glutReshapeFunc(reshape); | |
glutKeyboardFunc(keyboard); | |
init(300, 300); | |
glutIdleFunc(idle); | |
opencv_init(); | |
glutMainLoop(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import cv2 | |
from OpenGL.GL import * | |
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
cap = None | |
# Loading Camera and Nose image and Creating mask | |
def opencv_init(): | |
global cap | |
cap = cv2.VideoCapture(0) | |
def opencv_loop(): | |
global cap | |
_, frame = cap.read() | |
cv2.imshow("Frame", frame) | |
key = cv2.waitKey(1) | |
if key == 27: | |
# break | |
sys.exit(1) | |
def idle(): | |
print("running within idle callback") | |
opencv_loop() | |
def main(): | |
glutInit(sys.argv) | |
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH) | |
glutInitWindowSize(300, 300) # window size | |
glutInitWindowPosition(100, 100) # window position | |
glutCreateWindow(b"teapot") # show window | |
glutDisplayFunc(display) # draw callback function | |
glutReshapeFunc(reshape) # resize callback function | |
glutKeyboardFunc(keyboard) # Keyboard event callback | |
init(300, 300) | |
i = 0 | |
glutIdleFunc(idle) | |
opencv_init() | |
glutMainLoop(); | |
def keyboard(key, x, y): | |
if key == chr(27): | |
print("exitting") | |
sys.exit() | |
def init(width, height): | |
""" initialize """ | |
glClearColor(0.0, 0.0, 0.0, 1.0) | |
glEnable(GL_DEPTH_TEST) # enable shading | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
##set perspective | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) | |
def display(): | |
""" display """ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
##set camera | |
gluLookAt(0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) | |
##draw a teapot | |
glColor3f(1.0, 0.0, 0.0) | |
glutWireTeapot(1.0) # wireframe | |
glFlush() # enforce OpenGL command | |
def reshape(width, height): | |
"""callback function resize window""" | |
glViewport(0, 0, width, height) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) | |
if __name__ == "__main__": | |
main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
#include <GL/glut.h> | |
#include <GL/freeglut.h> // for glutMainLoopEvent | |
cv::VideoCapture cap; | |
void opencv_init() | |
{ | |
cap = cv::VideoCapture(0); | |
} | |
void opencv_loop() | |
{ | |
cv::Mat frame; | |
cap.read(frame); | |
cv::imshow("Frame", frame); | |
unsigned char key = cv::waitKey(1); | |
if ( key == 27 ) | |
{ | |
exit(1); | |
} | |
} | |
void keyboard(unsigned char key, int x, int y) | |
{ | |
if ( key == 27 ) | |
{ | |
printf("exitting"); | |
exit(1); | |
} | |
} | |
void init(int width, int height) | |
{ | |
glClearColor(0.0, 0.0, 0.0, 1.0); | |
glEnable(GL_DEPTH_TEST); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0); | |
} | |
void display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluLookAt(0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
glColor3f(1.0, 0.0, 0.0); | |
glutWireTeapot(1.0); | |
glFlush(); | |
} | |
void reshape(int width, int height) | |
{ | |
glViewport(0, 0, width, height); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0); | |
} | |
int main (int argc, char **argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); | |
glutInitWindowSize(300,300); | |
glutInitWindowPosition(100, 100); | |
glutCreateWindow("teapot"); | |
glutDisplayFunc(display); | |
glutReshapeFunc(reshape); | |
glutKeyboardFunc(keyboard); | |
init(300, 300); | |
int i = 0; | |
opencv_init(); | |
while (true) | |
{ | |
printf("running within while loop %d\n", i); | |
glutMainLoopEvent(); | |
opencv_loop(); | |
i++; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import cv2 | |
from OpenGL.GL import * | |
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
cap = None | |
# Loading Camera and Nose image and Creating mask | |
def opencv_init(): | |
global cap | |
cap = cv2.VideoCapture(0) | |
def opencv_loop(): | |
global cap | |
_, frame = cap.read() | |
cv2.imshow("Frame", frame) | |
key = cv2.waitKey(1) | |
if key == 27: | |
# break | |
sys.exit(1) | |
def main(): | |
glutInit(sys.argv) | |
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH) | |
glutInitWindowSize(300, 300) # window size | |
glutInitWindowPosition(100, 100) # window position | |
glutCreateWindow(b"teapot") # show window | |
glutDisplayFunc(display) # draw callback function | |
glutReshapeFunc(reshape) # resize callback function | |
glutKeyboardFunc(keyboard) # Keyboard event callback | |
init(300, 300) | |
i = 0 | |
opencv_init() | |
while True: | |
print("running within while loop {}".format(i)) | |
glutMainLoopEvent() | |
opencv_loop() | |
i += 1 | |
def keyboard(key, x, y): | |
if key == chr(27): | |
print("exitting") | |
sys.exit() | |
def init(width, height): | |
""" initialize """ | |
glClearColor(0.0, 0.0, 0.0, 1.0) | |
glEnable(GL_DEPTH_TEST) # enable shading | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
##set perspective | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) | |
def display(): | |
""" display """ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glMatrixMode(GL_MODELVIEW) | |
glLoadIdentity() | |
##set camera | |
gluLookAt(0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) | |
##draw a teapot | |
glColor3f(1.0, 0.0, 0.0) | |
glutWireTeapot(1.0) # wireframe | |
glFlush() # enforce OpenGL command | |
def reshape(width, height): | |
"""callback function resize window""" | |
glViewport(0, 0, width, height) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) | |
if __name__ == "__main__": | |
main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: cv_gl_1 cv_gl_2 | |
cv_gl_1: cv_gl_1.c | |
g++ -o cv_gl_1 cv_gl_1.c `pkg-config opencv --cflags --libs` -lGL -lGLU -lglut | |
cv_gl_2: cv_gl_2.c | |
g++ -o cv_gl_2 cv_gl_2.c `pkg-config opencv --cflags --libs` -lGL -lGLU -lglut | |
clean: | |
rm cv_gl_1 cv_gl_2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment