Cropping Image for publishing on ROS

I’ve been trying to get a cropped image of the detection through the ROS deep learning DetectNet. This isn’t the complete function below, but snippets of what was added. The message for ROS and the cropping. The publishing code works when I send out the input_cvt. I’ve created a third cvt called cropped_cvt for the cropped image. I’m stuck at knowing how to get the cropped image data (imgOutput) into the ROS sensor_msg::image format. I see how it was done for the overlay and it used a cvt. I’m unsure how to get the cropped data in a form to do that same with it.

sensor_msgs::Image msgCropped;

const int4 crop_roi = make_int4(cx-det->Width()/2,cy+det->Height()/2,cx+det->Width()/2 ,cy-det->Height())/2;
uchar3* imgOutput = NULL;
cudaAllocMapped(&imgOutput,det->Width() , det->Height());
cudaCrop(input_cvt->ImageGPU(), imgOutput, crop_roi,  input_cvt->GetWidth(), input_cvt->GetHeight());

cropped_cvt->Convert(msgCropped, imageConverter::ROSOutputFormat);

I’ve also tried this after creating a cropped_cvt. The result is the original image without a crop, but at least there is an image published. Not sure why it wouldn’t be cropping the image.

const int4 crop_roi = make_int4(cx-det->Width()/2,cy+det->Height()/2,cx+det->Width()/2 ,cy-det->Height()/2);
cudaCrop(input_cvt->ImageGPU(), cropped_cvt->ImageGPU(), crop_roi,  input_cvt->GetWidth(), input_cvt->GetHeight());
input_cvt->Convert(msgCropped, imageConverter::ROSOutputFormat);

Hi @santormj1, I think what you want is along the lines of this:

sensor_msgs::Image msgCropped;

cropped_cvt->Resize(det->Width(), det->Height());

const int4 crop_roi = make_int4(cx-det->Width()/2,cy+det->Height()/2,cx+det->Width()/2 ,cy-det->Height())/2);
cudaCrop(input_cvt->ImageGPU(), cropped_cvt->ImageGPU(), crop_roi,  input_cvt->GetWidth(), input_cvt->GetHeight());

cropped_cvt->Convert(msgCropped, imageConverter::ROSOutputFormat);

Thank you! This is what I ended up with and I had a few extra questions. I believe the internal format is what I want for the imageConverter. Is that true?

since the image is always resizing do you know of a way to see this image? It seems to be rotating or some other processing at times, and I’d like to verify first.

cropped_width=(uint32_t)round(det->Width());
cropped_height=(uint32_t)round(det->Height());

cropped_cvt->Resize(cropped_width, cropped_height, imageConverter::InternalFormat);

const int4 crop_roi = make_int4((int)floor(cx-(det->Width()/2)),(int)floor(cy-(det->Height()/2)),(int)floor(cx+(det->Width()/2)),(int)floor(cy+(det->Height()/2)));

cropped_cvt->Convert(msgCropped, imageConverter::ROSOutputFormat);

Yes you are correct, it should be imageConverter::InternalFormat

Do you mean of a way to visualize it? You could try using the video_viewer node to view the topic, or some other ROS image topic viewer.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.