Skip to content

cvtColor: fix inplace processing#6760

Closed
alalek wants to merge 1 commit intoopencv:masterfrom
alalek:issue_6653
Closed

cvtColor: fix inplace processing#6760
alalek wants to merge 1 commit intoopencv:masterfrom
alalek:issue_6653

Conversation

@alalek
Copy link
Copy Markdown
Member

@alalek alalek commented Jul 1, 2016

resolves #6653

@alalek
Copy link
Copy Markdown
Member Author

alalek commented Jul 4, 2016

👍

opencv-pushbot pushed a commit that referenced this pull request Jul 4, 2016
@mshabunin mshabunin closed this Jul 5, 2016
@mshabunin
Copy link
Copy Markdown
Contributor

PR is merged, but GitHub does not show correct status.

@zchrissirhcz
Copy link
Copy Markdown
Contributor

zchrissirhcz commented May 9, 2021

@alalek Hi, I'm late here, but observed that this PR makes "inplace rgb2bgr" operation slow for cv::Mat.

Specifically, for a h=4032, w=3024 rgb image, on android arm64 phone (QCOM855) with OpenCV4.5 (neon enabled, carotine lib used), this code:

cv::Mat image = cv::read("rgb.png");
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);

costs 15 ms, while my own naive implentation only costs 5 ms

void rgb2bgr_inplace_naive(unsigned char* buf, size_t height, size_t width)
{
    unsigned char tmp = 0;
    size_t len = height * width *3;
    for (size_t i=0; i<len; i+=3) {
        tmp = buf[i];
        buf[i] = buf[i+2];
        buf[i+2] = tmp;
    }
}

and I figured out the "magic" is the change in this commit, i.e.:

        if (_src.getObj() == _dst.getObj()) // inplace processing (#6653)
            _src.copyTo(src);  // !! this copy makes more time cost
        else
            src = _src.getMat();

Is there any better solution, that keeps arm neon performance for inplace rgb2bgr, and also resolve #6653 ?

@alalek
Copy link
Copy Markdown
Member Author

alalek commented May 11, 2021

Using implementations with 2 pointers with equal addresses would cause bugs (due to compiler optimizations, see #7819 as example).
Dedicated "inplace" implementation with one pointer is required.

Consider using of 2 different Mat objects in cvtColor() (and other calls) to avoid unnecessary copyTo() call. Also you can use swap() to replace Mat contents.

@zchrissirhcz
Copy link
Copy Markdown
Contributor

Tested with the following code, which is more faster (3ms on my android arm64):

cv::Mat image = cv::read("rgb.png");
cv::Mat image_copy = image;
cv::cvtColor(image_copy, image, cv::COLOR_BGR2RGB);

Is the usage OK? In this usage, _src.getObj() == _dst.getObj() is false, but image.data == image_copy.data is true.

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.

cvtColor and UMat error.

3 participants