Skip to content
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

opencv_grabber should use YARP_cv utilities #2161

Closed
Nicogene opened this issue Dec 20, 2019 · 2 comments
Closed

opencv_grabber should use YARP_cv utilities #2161

Nicogene opened this issue Dec 20, 2019 · 2 comments
Assignees

Comments

@Nicogene
Copy link
Member

Nicogene commented Dec 20, 2019

opencv_grabber converts the cv::Mat to a yarp::sig::image with a memcpy.

cv::Mat frame_rgb;
cv::cvtColor(frame, frame_rgb, cv::COLOR_BGR2RGB);
// Copy the captured image to the output image
memcpy(image.getRawImage(), frame_rgb.data, sizeof(unsigned char) * frame_rgb.rows * frame_rgb.cols * frame_rgb.channels());

We should use instead fromCvMat (introduced in #1932) that does not involves copies:

template<typename T>
yarp::sig::ImageOf<T> yarp::cv::fromCvMat(::cv::Mat& cvImage)
{
constexpr size_t align_8_bytes = 8;
constexpr size_t align_4_bytes = 4;
yarp::sig::ImageOf<T> outImg;
// Checking cv::Mat::type() compatibility with the T PixelType
assert(yarp::cv::type_code<T>::value == cvImage.type());
if (convert_code_from_cv<T>::value >= 0)
{
::cv::cvtColor(cvImage, cvImage, convert_code_from_cv<T>::value);
}
// Check the cv::Mat alignment
if (cvImage.step % align_8_bytes == 0) {
outImg.setQuantum(align_8_bytes);
}
else if (cvImage.step % align_4_bytes == 0) {
outImg.setQuantum(align_4_bytes);
}
outImg.setExternal(cvImage.data, cvImage.cols, cvImage.rows);
return outImg;
}

@Nicogene Nicogene added Component: Devices Issue Type: Feat/Enh Req This issue requests some new feature or enhancement labels Dec 20, 2019
@PeterBowman
Copy link
Member

I'm afraid it is not viable, or at least not as straightforward as a simple invocation of yarp::cv::fromCvMat. Since no copies are involved, but rather yarp::sig::Image holds a pointer to a memory location managed by an instance of cv::Mat, the lifetime of the latter is critical. However, this cv::Mat dies as soon as the getImage method is exited, therefore the wrapping grabberdual device attempts to forward a bogus yarp::sig::Image over the network (the memory location of the desired image may be erased at that point).

@drdanz
Copy link
Member

drdanz commented Oct 5, 2021

Fixed by @PeterBowman in #2713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants