Skip to content

imgproc: cvtColor: remove to copy edge pixels for COLOR_Bayer*_VNGs.#27226

Merged
asmorkalov merged 3 commits intoopencv:4.xfrom
Kumataro:fix27225
Apr 25, 2025
Merged

imgproc: cvtColor: remove to copy edge pixels for COLOR_Bayer*_VNGs.#27226
asmorkalov merged 3 commits intoopencv:4.xfrom
Kumataro:fix27225

Conversation

@Kumataro
Copy link
Copy Markdown
Contributor

@Kumataro Kumataro commented Apr 13, 2025

Close #27225
Close #5089
Related opencv/opencv_extra#1249

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@Kumataro
Copy link
Copy Markdown
Contributor Author

The effect of this patch can be seen in these images. https://github.com/opencv/opencv_extra/pull/1249/files

These pixels on the left/top/right/bottom edges will not be copied from inner pixels.
The four lines on the bottom edge in particular change clearly.

@Kumataro
Copy link
Copy Markdown
Contributor Author

Kumataro commented Apr 13, 2025

I think this test result is false-positive on this pull request.

CI tests the amount of changing files. If >1MiB, there is warning.
This pull request includes update OpenCV extra files, it updates 4 files about 470 KiB and 1 files about 1MiB.
So it is OK.

https://pullrequest.opencv.org/buildbot/builders/precommit_docs/builds/111134/steps/patch%20size%20opencv_extra/logs/stdio

commands lock was released
"stat" returned: (33206, 7291548, 64772, 1, 100000, 100000, 2879162, 1744550319, 1744550320, 1744550320)
File size: 2879162 bytes = 2811 KiB = 2 MiB
Patch size default limit exceeded: 1024 KiB

@Kumataro
Copy link
Copy Markdown
Contributor Author

Kumataro commented Apr 14, 2025

I found a bug in the workaround for small sizes. I fixed it.

When inputted image size is too small(<8), it works with interpolate mode. But it had not been cared for *VNG codes, so all behaviour are same even if convert codes are changed.

@asmorkalov asmorkalov added this to the 4.12.0 milestone Apr 16, 2025
@asmorkalov asmorkalov self-requested a review April 16, 2025 07:52
@asmorkalov asmorkalov self-assigned this Apr 16, 2025
@asmorkalov
Copy link
Copy Markdown
Contributor

@Kumataro Thanks a lot for the contribution! The test image with random noise shows the fix very well. Please ignore the warning on the patch size in extra.

@asmorkalov
Copy link
Copy Markdown
Contributor

The PR was discussed on the Core team meeting. We decided to merge the PR to fix functional part with further optimization plan. copyMakeBorder is not efficient solution and we want to get rid of it in long term: #27256

@asmorkalov asmorkalov merged commit 86a963c into opencv:4.x Apr 25, 2025
46 of 55 checks passed
fengyuentau pushed a commit to fengyuentau/opencv that referenced this pull request Apr 25, 2025
imgproc: cvtColor: remove to copy edge pixels for COLOR_Bayer*_VNGs. opencv#27226 

Close opencv#27225
Close opencv#5089
Related opencv/opencv_extra#1249

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
@Kumataro
Copy link
Copy Markdown
Contributor Author

Thank you very much for considering this matter !!
I agree with you. I also think that this code could be refactored.

The reasons for choosing this fix provided here for your information :


The current implementation assumes an offset of (2,2). And there are many magic number 2.

const ushort* brow0 = buf + ((y - 2) % brows)*bufstep + 2;
const ushort* brow1 = buf + ((y - 1) % brows)*bufstep + 2;
const ushort* brow2 = buf + (y % brows)*bufstep + 2;
static const float scale[] = { 0.f, 0.5f, 0.25f, 0.1666666666667f, 0.125f, 0.1f, 0.08333333333f, 0.0714286f, 0.0625f };
srow = bayer + y*bstep + 2;
bool greenCell = greenCell0;
i = 2;
#if CV_SIMD128
int limit = greenCell ? std::min(3, N-2) : 2;
#else
int limit = N - 2;
#endif

And If the offset is removed, it requires to split the processing into a central part that can be implemented using SIMD, and other parts that can be wrapping edge without SIMD.
The number of conditional branching should be reduced as much as possible for SIMD.

However, it is very difficult to refactor for it. I prioritized removing the copies on the edges.

@asmorkalov asmorkalov mentioned this pull request Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cv::Demosaicing with VNG method - output is offset by two pixels vertically Bayer VNG Pixel Offset

2 participants