Repository navigation
Fix for JavaCamera2View not displaying colors correctly on some devices - #14342
Conversation
…a2View also shows correct colors when the Image contains I420 or YV12 data
|
/cc @floe Could you take a look on this patch? |
|
Just had a quick look, pretty busy ATM - wouldn't it be cleaner to call through to a suitable native cvtColor* method? Byte-wise copying in Java is probably rather ineffective... |
|
I agree the Byte-wise copying is a suboptimal solution, but currently I don't see any other solution for 2 reasons:
If we want to support all Android devices, the 2 reasons above are why we either need to copy bytes, or we need a cvtColor function that takes 3 Mats as input and takes into account possible row stride. In my opinion the best way to ensure that OpenCV works seamlessly with in Android in future would be to add a cvtColor function that handles all Android Images with format YUV_420_888. Android recommends using this generic YUV_420_888 format. The new cvtColor function would need to take as inputs:
Also note that for devices that provide NV21 YUV data, the code runs just as fast as before, the conversion code is unchanged in that case. The copying of bytes in this pull request will only happen on device that show incorrect colors with the current implementation. So even though the copying is an imperfect solution, I believe it is better than doing an incorrect conversion. |
|
@Victorlouisdg Thank you for clarification! Possible ways for optimizations here: |
|
Well, looks like old code patch is almost still the same (without slow copying). Can we memoize already processed images (for case of sequential calls)?
Please also note that |
|
Ok I will add the memoizing to How would you check the lifetime of the derived |
|
New one PR is fine.
I don't suggest to add code for checking this in runtime. I mean we should review code more carefully on this scope. Looks like this code part is fine. Suspicious code is here:
Android documentation states that this buffer is "direct":
Hopefully, its lifetime is not less than associated "Image". |
resolves #13526
This pullrequest changes
Changed the way Images are converted into rgba Mats so that JavaCamera2View also shows correct colors when the Image contains I420 or YV12 data.
Converting Images to Mats now checks the properties of the Image Planes to choose the correct conversion method. Two main case can be discerned:
Another thing I noticed is that in case of non-interleaved chroma channels, for certain resolutions, the row stride of the chroma channels did not equal the width of the chroma channels. For example, resolution 720x480 gave a chroma channel width of 360, but the row stride was 368 on my device, meaning there were 8 extra padding bytes that should not be copied into the Mat. This also caused artefacts (green lines and spread out colors). This was fixed by copying the chroma channel ByteBuffer contents row by row when the row stride does not equals the width of the channel.