Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts

Sunday, September 5, 2021

Fixing Failure to Load Video in OpenCV-Python on Linux

The failure to load video in OpenCV-Python usually happened with older Linux distributions. In my case, it happened in Ubuntu 18.04. The failure in video loading can be detected in python code similar to this:

import cv2

..

cap = cv2.VideoCapture(video_file_path)

if cap.isOpened():

    print("Video file loaded successfully")

else:

    print("Failed to load video!") 

    return

..

The failure to load the video very possibly is caused by erratic OpenCV-Python installation. In my case it was caused by very old pip version. Old pip version will trigger unexpected problems, in my case the old pip version (pip version 9.x) forces OpenCV recompilation when installing OpenCV-python. The old pip version installed the result of the recompilation at the end of pip's execution. Due to erratic pip behaviour, the resulting opencv-python is not working properly. Anyway, as a sidenote: I'm using python 3 venv virtual environment when I encountered this problem. 

THE FIX:

The fix is quite simple:

  1. Uninstall your current (unworkable) OpenCV-python. In my case, simply invoking "pip unistall opencv-python" works. I carried-out this command inside the virtual environment that has the erratic OpenCV-python. 
  2. Upgrade your pip version into more up-to-date version with "pip install --upgrade pip" command. Don't be afraid to carry-out this step if you're using python virtual environment because it won't affect your machine in any way. 
  3. Re-install OpenCV-python with pip with the following command: "pip install opencv-python" or if you want to specify the specific OpenCV-python version you can do that as well, as in (installing opencv-python version 4.3.0.38): "pip install opencv-python==4.3.0.38" 
This isssue is explained in more detail here: https://pypi.org/project/opencv-python/ (point 2 in the Installation section). It took me an hour to figure out the problem because I've never encountered the issue before. Hopefully this helps you out if you have similar problem.

Monday, September 7, 2020

Removing "Still" Frames from Video Files with Ffmpeg

 Ffmpeg is a very versatile tool for video pre-processing and post-processing. One of its most useful feature is removing frames which doesn't have a lot of changes in it, i.e. relatively static background. The parameter to use for this purposes is the mpdecimate flag, the presentation timeframe (setpts)flag might also be used for synchronization purposes. In my case, the command I used to throwaway the relatively static vidio frames as follows:

ffmpeg -i input_vid.mp4 -vf mpdecimate,setpts=N/FRAME_RATE/TB output_vid.mp4

Details about the mpdecimate flag can be read over at: https://ffmpeg.org/ffmpeg-filters.html#decimate-1, and details about the setpts parameter is at https://ffmpeg.org/ffmpeg-filters.html#toc-setpts_002c-asetpts. Hopefully, this is helpful for you.

Monday, June 13, 2016

GraphViz Tutorial for The Uninitiated

This is not a tutorial per se. But, it's an example of how a complex graph can be generated by GraphViz DOT. You need to head over to  https://github.com/pinczakko/GraphViz-Samples for the source code. But, for the impatient, this is the result:
Rather complex graph generated from GraphViz DOT
At least this sample shows you how powerful GraphViz is, after investing even just a couple of hours learning the ropes. The bonus is, you can combine your GraphViz DOT code to your Doxygen comments and generate the graph in your code documentation. Isn't that powerful? Head over to http://www.stack.nl/~dimitri/doxygen/manual/diagrams.html for that.

Hopefully, this ease the pain creating your code documentation ;-)


Tuesday, March 10, 2015

GLSL 3.30 in Intel Haswell CPU

This error could show up while trying to run your OpenGL program that uses GLSL on Haswell:
$ error GLSL 3.30 not supported.. 
It's very probably because you don't specifically ask the OpenGL implementation (in this case mesa) for Core Profile because only Haswell OpenGL core profile supports GLSL 3.30, as shown in this Haswell glxinfo dump:
....
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Haswell Mobile 
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.4.5
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
....
OpenGL version string: 3.0 Mesa 10.4.5
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
....
As you can see, the non-core profile only supports up-to GLSL 1.30. Now, how do you go about asking core profile in your OpenGL application? If you are using freeglut, you can use these functions:

...
#include <gl\freeglut.h>
...
int main(int argc, char * argv[])
{
...
glutInitContextVersion(3,3);
glutInitContextProfile(GLUT_CORE_PROFILE);
...
}
Remember that the initialization code above must be called when you initialize your OpenGL environment. Also, it only applies if you use freeglut library.

Saturday, March 29, 2014

OpenGL Texture is not (just) Picture

I'm a beginner in OpenGL for sure and one of the most confusing thing (read: naming convention) that has plagued me in these last two days is OpenGL texture. Contrary to the naming for functions, constants and objects used for OpenGL texture, everything related to texture on those "interfaces" don't really apply to image/picture only. It's much more general than that. OpenGL texture is basically a "lookup-table" (array) and it can store anything as long as the data type matches with the texture that you use in your code. The naming for OpenGL texture "interface" that usually implies the use of image/picture is a legacy of the past. It was used since the day of fixed-function video card and used until now to preserve API backward-compatibility--at least that's one of the reason that I'm aware of.

Anyway, these are some useful links regarding OpenGL texture:
http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/tutorial.html#arrays1
http://www.arcsynthesis.org/gltut/Texturing/Tutorial%2014.html

Wednesday, February 26, 2014

OpenGL Application Development in Windows 8.1 64-bit

I've been doing some OpenGL application development with Visual Studio 2012 for a week or so but I found it rather frustrating because M$ doesn't provide all headers required by C99 standards. Moreover, I have yet to configure GLSL syntax highlighting successfully in Visual Studio 2012. Well, it turned out it's much better to carryout OpenGL-related development with MinGW and using CodeBlocks as the IDE.

You can download CodeBlocks from www.codeblocks.org and use the 64-bit MinGW distribution from http://nuwen.net/mingw.html. The only missing library would be freeglut for MinGW which you can download from http://www.transmissionzero.co.uk/software/freeglut-devel/. The MinGW distribution from Stephan T. Lavavej (nuwen.net) already has GLEW in it, but it doesn't have glut or freeglut.

The way I setup my development environment as follows:
  1. Use CodeBlocks as the IDE, i.e. download and use CodeBlocks distribution that DOESN'T include MinGW because we are going to use the one from nuwen.net.
  2. Use nuwen.net MinGW as the source for base GCC, GLEW and GLM tools, libraries and includes.
  3. Use Freeglut for MinGW as the glut replacement.
Assuming you have setup all of the tools above correctly (consult their README files), it's time to create a sort of "Hello World" application in OpenGL. Head to http://en.wikibooks.org/wiki/OpenGL_Programming and try the first tutorial source code. This is how I create project in CodeBlocks for that tutorial:
  1. Create an empty project in CodeBlocks.
  2. Add all tutorial 01 source code from the basics arc (http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction) into the empty project.
  3. Fix all required settings before building the project, i.e. compiler include directory search paths and libraries search paths. The details are explained below.
The following screen captures show the settings that I changed to compile tutorial 01 successfully. The tabs, menus and entries highlighted (in red box) are those that you should change according to your MinGW installation details.





Once everything compiled and linked successfully, you can run the application. This is how the application should look like.


Well done. Now, you can proceed developing all other OpenGL application in Windows without thinking about C99 and GLSL syntax highlighting matters :-).

Anyway, you could actually setup the compiler and linker setting in per-project basis in CodeBlocks via the Project|Build Option menu. Clicking this menu will open a similar dialog box as shown in the screenshot above. However, it will only apply to currently active project.

UPDATE:
-----------
- The CodeBlocks steps I mentioned above are rather brute force approach. I'll update it with a better solution in the future.

Tuesday, February 18, 2014

Up-to-Date OpenGL Tutorial for Beginner

I found the Swiftless series article on OpenGL at: OpenGL 2 Tutorials and OpenGL 4 Tutorials
to be very compelling for newbie on the subject. They are particularly easy to follow for complete newbie.
One thing that sets them apart from other articles on the subject is they cover recent implementation of OpenGL. Therefore, it's easier to follow on present day OpenGL library available on recent systems and also features supported by present day GPUs.

Nice stuff Swiftless. Kudos to your work :-)

If you want a quite lengthy but deep introduction, head to Learning Modern 3D Graphics Programming. This is the most complete tutorial I found on the Net so far. Well, it's very well structured for my taste. I think, it's even better than http://en.wikibooks.org/wiki/OpenGL_Programming.