Skip to content

Instantly share code, notes, and snippets.

@nkint
nkint / lazy_set.py
Created February 9, 2011 18:26 — forked from rik0/lazy_set.py
import itertools as it
def setness(f):
        setness.calls = dict()
        def decorator(arg):
                seen = setness.calls.get( arg, set() )
                for i in seen: yield i
                for i in f(arg):
                        if i not in seen:
                                seen.add(i)
@nkint
nkint / opencv2qt
Last active March 6, 2017 08:42
cv::Mat to QPixmap
cv::Mat n = ...
QPixmap p = QPixmap::fromImage(QImage((unsigned char*) n.data,
n.cols,
n.rows,
QImage::Format_RGB888));
p = p.scaledToWidth(500);
label->setPixmap( p );
@nkint
nkint / gist:7695359
Created November 28, 2013 17:21
openc2qimage2gltexture
cv::Mat original = //...
QImage qtFrame(original.data, original.size().width, original.size().height, original.step, QImage::Format_RGB888);
qtFrame = qtFrame.rgbSwapped();
m_GLFrame = QGLWidget::convertToGLFormat(qtFrame);
glColor3f(1, 1, 1);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@nkint
nkint / gist:7790728
Created December 4, 2013 16:35
python as sh: print all the directory recursively
import os, sys
rootdir = sys.argv[1]
for root, subfolders, files in os.walk(rootdir):
dirs = [ os.path.abspath(rootdir)+ root[1:] + "/" + s for s in subfolders ]
for d in dirs: print d
@nkint
nkint / gist:8144791
Last active January 1, 2016 12:29
opencv: turn black pixels into alpha 0
displayDebug = ....
// turn black pixels into alpha 0
if(displayDebug.channels()==1)
cv::cvtColor(displayDebug, displayDebug, CV_GRAY2BGR);
cv::cvtColor(displayDebug, displayDebug, CV_BGR2BGRA);
for (cv::Mat4b::iterator it = displayDebug.begin<cv::Vec4b>(); it != displayDebug.end<cv::Vec4b>(); it++) {
if (*it == cv::Vec4b(0, 0, 0, 255)) {
*it = cv::Vec4b(0, 0, 0, 0);
}
@nkint
nkint / gist:8176516
Created December 30, 2013 00:41
opencv: draw a small mat inside a larger mat
cv::Rect roi( cv::Point( originX, originY ), smallImage.size() );
smallImage.copyTo( bigImage( roi ) );
@nkint
nkint / gist:8563954
Last active September 7, 2023 05:18
ffmpeg in the shell: extracting the first frame of video
i=1
for avi in *.mp4; do
name=`echo $avi | cut -f1 -d'.'`
jpg_ext='.jpg'
echo "$i": extracting the first frame of the video "$avi" into "$name$jpg_ext"
ffmpeg -loglevel panic -i $avi -vframes 1 -f image2 "$name$jpg_ext"
i=$((i+1))
done
@nkint
nkint / gist:8576156
Created January 23, 2014 10:15
opencv and python: concatenate video
import numpy as np
import cv2
import os
# this two lines are for loading the videos.
# in this case the video are named as: cut1.mp4, cut2.mp4, ..., cut15.mp4
videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.mp4']
videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))
video_index = 0
@nkint
nkint / gist:9089157
Last active April 11, 2022 12:35
opengl in qt: render string to QImage to texture
void displayText(const QString &text, const QColor &backgroundColor, const QColor &textColor, const Vec3D &pos)
{
// some magic number (margin, spacing, font size, etc..)
int fontSize = 15;
int text_width=text.size()*(fontSize/1.5)+5, text_height=fontSize+20;
// create the QImage and draw txt into it
QImage textimg(text_width, text_height, QImage::Format_RGB888);
{
QPainter painter(&textimg);
@nkint
nkint / gist:9496912
Created March 11, 2014 23:06
shell as your desk: list all files with a complete path
find `pwd`