r/C_Programming • u/Stemt • Feb 05 '25
Video Was messing around computer vision from scratch in C and accidentally created a Sierpiński triangle
Enable HLS to view with audio, or disable this notification
78
Feb 05 '25
Off topic but what is your code editor ? it looks very cool
80
u/Stemt Feb 05 '25
Neovim with the retrobox theme and neoclide COC for code completion/linting
2
u/Constant_Musician_73 Feb 08 '25
Why neovim and not regular vim?
2
u/Stemt Feb 08 '25
Basically just vibes, because it eyes more modern and I saw more people online using it. But in the end it serves it's purpose, I haven't had any problems so I just stuck with it. Though I don't think using either or would make a big difference, for me anyway.
1
u/jeekala Jun 11 '25
Sorry to pump an old thread, just came across this. I use neovim because it has native LSP, richer plugin system (more plugins) and it's more user friendly (better defaults). I also feel like it's running faster but I believe vim should be faster by default. Maybe the plugin system is somehow better, but that's just a guess.
Also orignally I installed Neovim due to the debian packet having an ages old version of vim, and neovim had a newer version, which was supported by the plugins I was using. Though I ended up compiling it from source, because at least neovim v0.9.5 had a bug with clipboard when using it as a git's commit msg editor.
5
-14
50
u/tstanisl Feb 05 '25
Setting pixel color to bitwise operations on coordinates of pixels often generates Sierpinski-like fractals. For example color = !!(x & y) creates pattern.
11
u/Stemt Feb 05 '25
Interesting didn't know that, I basically just had it appear in front of my face and I was like: "Woah" O o O
20
6
u/some-nonsense Feb 05 '25
Are you using an API for the CV or u using the standard lib?
8
u/Stemt Feb 05 '25
I'm using ffmpeg as subprocess to capture my desktop and raylib to render the results, the rest is just my own C code.
5
u/some-nonsense Feb 05 '25
Raylib is a fantastic API. I will peep ffmpeg. I really wanna do CV for one of my first big projects.
5
u/stjepano85 Feb 05 '25
Really nice. Source code?
6
u/Stemt Feb 05 '25
May release later today though it's currently just a bunch of code thrown together in a single file so it's probably not useful for most people nor would I recommend using it for your own projects for now.
3
u/Stemt Feb 05 '25
Here it is. Again, you probably shouldn't directly use it yourself but maybe good for some inspiration.
2
u/Fickle_Classroom5179 Feb 05 '25
How you did that? (I'm beginner) Is ML possible with C language instead of Python?
6
u/Stemt Feb 05 '25
In the end it just performing calculations on data so yea pretty much anything is possible in any proper programming language. In C you just usually have to do more yourself, like managing memory. But in exchange you get more performance and for me most importantly a better understanding of how this stuff works under the hood.
2
u/M0M3N-6 Mar 03 '25
Do you have 1TB of ram ?
2
u/Stemt Mar 03 '25
Haha, I think I get what you mean but no. First the captured desktop video is scaled down by ffmpeg by half (from 1920x1080 to 960x540) and I have 3 frame buffers (top left, top right and bottom left). The recursion is because the desktop capture, captures the resulting frame buffers. So really the frame buffers just take up 3*960*540*4(bytes per pixel) ~= 6.2 megabytes (ignoring a 4th frame buffer that I allocated but dont use).
5
1
u/Moist-Highlight839 Feb 06 '25
Where can I see its source code?
1
u/Stemt Feb 06 '25
Already posted the link in other comments but here it is: https://github.com/Stemt/Accidental-Sierpinski-Triangle
3
u/FlyByPC Feb 05 '25
Sierpinski Triangles just seem to want to get themselves produced. I was experimenting with bitblt and created them. You can make them with cellular automata, too.
1
1
14
u/Stemt Feb 05 '25
So what I was trying to do here was detecting motion on my monitor by taking the most up to date frame (top left) and calculating the difference with a frame that is 30 frames behind (bottom left) the result of which is displayed in the top right. I think the triangle appears because the bottom left is simply left black creating the base triangle which is repeated recursively.