Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpha-blending using smoothed mask #72

Merged
merged 2 commits into from
Apr 18, 2021
Merged

Conversation

phlash
Copy link
Collaborator

@phlash phlash commented Apr 17, 2021

Using a smoothed segmentation mask to alpha blend between background and foreground (captured video), this is a simple not-quite-light-wrapping (there is no blurred background component) fix to the hard edges we have presently. Might address #67.
For reference: https://www.premiumbeat.com/blog/what-is-light-wrapping-tips-tutorials/ provides a reasonable description of light wrapping.

@@ -76,6 +76,29 @@ cv::Mat convert_rgb_to_yuyv( cv::Mat input ) {
return yuyv;
}

cv::Mat alpha_blend(cv::Mat srca, cv::Mat srcb, cv::Mat mask) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there something for this already in OpenCV?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing obvious: it has a constant alpha blend function addWeighted, and masked binary blending via copyTo or add each with a mask parameter.
There is the usual half answered SO question: https://stackoverflow.com/questions/41263010/how-to-blend-images-using-a-mask-in-opencv and an unanswered OpenCV forum one: https://answers.opencv.org/question/28439/blending-two-images-with-masked-roi/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh - literally where I got my code from..

deepseg.cc Outdated Show resolved Hide resolved
deepseg.cc Outdated Show resolved Hide resolved
Copy link
Collaborator

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

One possible option might be to avoid the division by 255 and try to get this somehow to use a division by 256. But that for another time …

@phlash
Copy link
Collaborator Author

phlash commented Apr 18, 2021

Initial timing measurements indicate this adds 0.5-1.0 msec to the post-processing compared to bg.copyTo() operation.

@phlash phlash merged commit b6f295c into floe:experimental Apr 18, 2021
@phlash phlash deleted the alpha-mask branch April 18, 2021 15:33
@floe
Copy link
Owner

floe commented Apr 18, 2021

Ah, that's a good idea, thanks. What happens if I cherry-pick this into main, would that mess anything up down the line?

@BenBE
Copy link
Collaborator

BenBE commented Apr 18, 2021

No, this should be fine. Git detects when you merge things where you've had the same patch applied already.

phlash added a commit to phlash/backscrub that referenced this pull request May 4, 2021
* Alpha-blending using smoothed mask

* Tidy up alpha blending, avoid float math
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants