Comments (7)
There are no background extraction methods implemented, such as this, but seems like it would be pretty trivial to remove black background in the example image you've given with DCV.
I cannot test it out at the moment, but I believe basic thresholding would work:
Image image = imread("your_image.png");
auto threshValue = 10; // some low value, experiment on this...
auto alpha = image
.sliced[0 .. $, 0 .. $, 1] // take green channel (most probably will work fine)
.threshold!ubyte(10); // pixel with value below the 10 will be 0, above will be 255.
// if your image is RGB, convert to RGBA and apply alpha
Image rgba = new Image(image.width, image.height, ImageFormat.IF_RGBA, BitDepth.BD_8);
rbga.sliced[0 .. $, 0 .. $, 0 .. 3][] = image.sliced[]; // copy pixel values
rgba.sliced[0 .. $, 0 .. $, 3][] = alpha[]; // copy alpha values
Note that threshValue
can be adjusted - I only gave it value of 10 as a guess, but you can sample your background, and use values more close to it. Also, you can experiment with ranged threshold, where you give lower and upper threshold value.
This is basic idea I would use to solve your problem. Hope I'm of any help, and please let me know if there's anything other that I may help you with.
from dcv.
(in ideally wepb, but D do not have native encoder/decoder :( )
Regarding this matter, my plan is to bind C libraries for image i/o, so if there's a nice wepb C library out there, we should support this format in future.
from dcv.
Hey, one more note- since there's a certain amount of color channel shift in the example image, I suppose taking green channel for thresholding might not work well (it would most probably cut out blue and red regions on borders). Consider using rgb2gray with Rgb2GrayConvertion.MEAN
, in the case green channel does not give you desirable results.
from dcv.
Just found some time, and wanted to try out the solution I proposed above. I got following result (changed only the threshValue
, and used rgb2gray
):
As said, you may tweak params a bit, but that's about it. Hope this solves your problem.
Cheers!
from dcv.
@ljubobratovicrelja very cool! Big thanks! I will try it when finish other work.
Could you add this code to example? I think a lot of people have similar task.
Later I will test it on this image:
from dcv.
Could you add this code to example?
Sure! Thanks for suggestion.
Well, this next example image looks bit more challenging. It has lot of underexposed areas in its foreground. I'm quite sure thresholding method won't work that well- you'd have holes in those dark areas in the middle. But you could try using closing morphological operation iteratively (with smaller box kernel, say with size 5) to stitch them up:
// ... thesholding code shown above
foreach(iter; 0 .. iterations) // chose number of iterations empirically (easiest solution)
{
alpha = alpha.close(boxKernel!ubyte(5, 5));
}
Again, I'm can't test this out at the moment, but will do later today.
from dcv.
Well, seems like the closing op won't do much good either. Here is the alpha result from only threshold on the second image:
And, here is with closing:
In cases like this, maybe taking a convex hull of all white regions would solve the case- it's the first thing that comes to mind. You can also try with segmentation algorithms like watershed, but since those dark regions are so large, I'm afraid you'd get similar artifacts. Do these images maybe come in 16bit? It would help significantly if you could use 16 bit, over 8 bit for this task.
Btw, here is the code I've been getting these results with:
Image image = imread("5c9e2f14-5ee4-11e6-961c-eb8198d09c21.jpg");
auto alpha = image
.sliced
.rgb2gray(emptySlice!(2, ubyte), Rgb2GrayConvertion.MEAN)
.asType!float
.conv(gaussian!float(3, 5, 5))
.threshold!ubyte(1); // pixel with value below the 10 will be 0, above will be 255.
alpha.imwrite(ImageFormat.IF_MONO, "alpha1.png");
alpha = alpha.close(radialKernel!ubyte(51));
alpha.imwrite(ImageFormat.IF_MONO, "alpha2.png");
// apply alpha
Image rgba = new Image(image.width, image.height, ImageFormat.IF_RGB_ALPHA, BitDepth.BD_8);
rgba.sliced[0 .. $, 0 .. $, 0 .. 3][] = image.sliced[]; // copy pixel values
rgba.sliced[0 .. $, 0 .. $, 3][] = alpha[]; // copy alpha values
rgba.imwrite("im.png");
from dcv.
Related Issues (20)
- betterC mir-cv HOT 6
- use documentation unittests to guarantee docs stays in sync with code and compiles (http://dcv.dlang.io/) HOT 3
- can we make glfwInit optional? HOT 16
- cannot use dcv dependency in a dub.json, http://code.dlang.org/packages/dcv.json broken
- undefined error when calling dcv.imgproc.convolution.conv HOT 7
- wrappers for opencv HOT 16
- git history not very linear; why not use `git pull --rebase`instead of `git pull`? HOT 1
- Create CONTRIBUTING file
- plotting without X-server HOT 1
- simplify API's with isSlice HOT 6
- (most) bool return functions should instead throw on error
- Video IO Example HOT 12
- Error: function ffmpeg.libavformat.avformat.AVIndexEntry.flags(int v) cannot be overloaded with another extern(C) HOT 2
- Error: recursive alias declaration (using dmd git master) HOT 3
- core/utils.d(72,23): Error: cannot implicitly convert expression iota([shape[0]]) HOT 2
- update to mir-algorithm v0.9.1 HOT 1
- dcv/source/dcv/io/video/input.d(149,38): Deprecation: function `ffmpeg.libavcodec.avcodec.avpicture_get_size` is deprecated
- Broken example on the homepage? HOT 4
- is this library dead? HOT 7
- ImageFormat dependency has issues with some jpg?? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dcv.