-
Notifications
You must be signed in to change notification settings - Fork 86
added cropping of capture/background #153
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
127009b
65627e8
7b41672
cb0fe4f
3eeb58b
01e9a75
f81e26e
112e979
22472ad
7dc9bcc
6894b3e
1769f7e
e9a5242
0f62a57
81c1c83
4c5f72c
ffb9883
7353ff8
5a92b93
5ee0d4a
44dbb6a
4c6431e
975b96f
481cb47
f80a8a6
ea1e350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,6 +9,8 @@ | |||||
#include <opencv2/imgproc.hpp> | ||||||
#include <opencv2/highgui.hpp> | ||||||
|
||||||
#include <lib/libbackscrub.h> | ||||||
|
||||||
// Internal state of background processing | ||||||
struct background_t { | ||||||
int debug; | ||||||
|
@@ -183,11 +185,14 @@ int grab_background(std::shared_ptr<background_t> pbkd, int width, int height, c | |||||
if (pbkd->video) { | ||||||
// grab frame & frame no. under mutex | ||||||
std::unique_lock<std::mutex> hold(pbkd->rawmux); | ||||||
cv::resize(pbkd->raw, out, cv::Size(width, height)); | ||||||
cv::Rect_<int> crop = calcCropping(pbkd->raw.cols,pbkd->raw.rows,width,height); | ||||||
jjsarton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
cv::resize(pbkd->raw(crop), out, cv::Size(width, height)); | ||||||
frm = pbkd->frame; | ||||||
} else { | ||||||
// resize still image as requested into out | ||||||
cv::resize(pbkd->raw, out, cv::Size(width, height)); | ||||||
cv::Rect_<int> crop = calcCropping(pbkd->raw.cols,pbkd->raw.rows,width,height); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updates my cropping branch accordingly. Your suggestion is within my code |
||||||
cv::resize(pbkd->raw(crop), out, cv::Size(width, height)); | ||||||
out = pbkd->raw; | ||||||
frm = 1; | ||||||
} | ||||||
return frm; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,7 +365,11 @@ bool bs_maskgen_process(void *context, cv::Mat &frame, cv::Mat &mask) { | |
|
||
// scale up into full-sized mask | ||
cv::Mat tmpbuf; | ||
cv::resize(ctx.ofinal(ctx.in_roidim),tmpbuf,ctx.mroi.size()); | ||
// with body-pix-float-050-8.tflite the size of ctx.ofinal is 33x33 | ||
// and the wanted roi may be greater as 33x33 so we can crash with | ||
// cv::resize(ctx.ofinal(ctx.in_roidim),tmpbuf,ctx.mroi.size()); | ||
ctx.ofinal.copyTo(tmpbuf); | ||
cv::resize(tmpbuf, tmpbuf, ctx.mroi.size()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is correct? Selecting an ROI from the final output is done because we may have centred the frame into the model earlier (line 289), this change removes the selection step. The calculations at line 237 onward should ensure that After going away and thinking about this - this is a separate bug that you do not need to fix in this PR. Let's raise another issue for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I modified the original code because I had a crash within opencv. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have just tested this again. With the old code, using backscrub/models/body-pix-float-050-8.tflite I get again a crash, with my correction this work well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep - it's because they have an output stride variable in the model which defaults to 32, so we get input dim/32 == 257/32 ~= 33. I'm happy to leave the crash for now, you are officially "not making it worse" with this PR 😁, and we'll probably want to revisit the logic in multiple places to fix this properly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now covered by issue #156, and thus can be fixed by a separate PR that properly deals with the fact that we do not account for models that have different input and output sizes at all. |
||
|
||
// blur at full size for maximum smoothness | ||
cv::blur(tmpbuf,ctx.mroi,ctx.blur); | ||
|
@@ -375,3 +379,25 @@ bool bs_maskgen_process(void *context, cv::Mat &frame, cv::Mat &mask) { | |
return true; | ||
} | ||
|
||
cv::Rect calcCropping(int cw, int ch, int vw, int vh) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I suggest:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am lazy, but you are right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, not pushed |
||
// if the input and output aspect ratio are not the same | ||
// we can crop the source image. For example if the | ||
// input image has a 16:9 (1280x720) ratio and the output is 4:3 (960x720) | ||
// we will return the cropRegion set as x=160, width=960, y=0, height=720 | ||
// which is the centered part of the original image | ||
cv::Rect cropRegion = {0, 0, 0, 0}; | ||
float sc = (float)vw / cw; | ||
float st = (float)vh / ch; | ||
sc = st > sc ? st : sc; | ||
|
||
int sx = (int)(vw / sc) - cw; | ||
cropRegion.x = (sx < 0 ? -sx : sx) / 2; | ||
|
||
int sy = (int)(vh / sc) - ch; | ||
cropRegion.y = (sy < 0 ? -sy : sy) / 2; | ||
|
||
cropRegion.width = cw - cropRegion.x * 2; | ||
cropRegion.height = ch - cropRegion.y * 2; | ||
|
||
return cropRegion; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.