-
-
Notifications
You must be signed in to change notification settings - Fork 55.6k
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
support construct submatrix from numpy array #22822
base: 4.x
Are you sure you want to change the base?
Conversation
@huww98 Could you add simple test with the feature? |
I added simple test for ROI in Python. The feature works good to me with Python 3.6.9. |
I got some unexpected results while writing my test. I'm debugging this. img = np.array([
[1,1,1],
[1,10,1],
[1,1,1],
], dtype=np.uint8)
kernel = np.full((3,3), 1/9, dtype=np.float32)
submatrix = img[1:2, 1:2]
result_submatrix = cv.filter2D(submatrix, -1, kernel)
self.assertEqual(result_submatrix[0,0], 2) got
|
Should we add a new parameter |
It looks like the issue is related to filter2D, but not ROI. BORDER_DEFAULT is equivalent to
Output:
|
@alalek What do you think on |
As direct test of sub-matrix support, I suggesting you to add |
[15, 16, 17, 18, 19]], dtype=np.uint8) | ||
roi_min, roi_max, _, _ = cv.minMaxLoc(m[1:3, 1:4]) | ||
self.assertEqual(roi_min, 6) | ||
self.assertEqual(roi_max, 13) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test passes without the fix.
Made experiments with C++ code. The first case with original matrix 3.x produce correct result. It means that filter2d works well.
Output:
|
This is because we are not copying over the stride for dims with size 1. So the matrix in my test case is recognized as 9x1 rather than 3x3. After further investigation, I find it challenging to deal with the automatic copy. It is hard to anticipate what will happen when a submatrix is passed in, but it needs copying.
My plan is to abandon this PR. And design a more explicit API for ROI. e.g.:
Then we can just refuse to proceed if copying is needed in this new API. What do you think? |
Some APIs behave differently when operating on a small mat, or an ROI of a large mat. For example
cv::filter2D
. However, python cannot construct a Mat with ROI. This patch enables python to invoke these behaviors that rely on ROI.This may change the behavior of some old code. But I think the new behavior is more suitable and matches the expectation of C++ programmers.
I think this is a "Small feature", so the base branch is 4.x.
Not sure whether or where I should add some docs about this?
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.