Cudacrop mangling my image

My input image is 1080x1920

My output image is 810x1920

I tried crop_roi of (135,0,945,1920) but it kept complaining of invalid roi until I changed it to (135,0,944,1919).

Now cudacrop works without complaining but it mangles up my image. See attachment.

What could be going on here?

It looks like this could be the one pixel offset propagating down the image by borrowing one pixel from the next row at the end of the first row, tow pixels at the end of the second row etc. So I guess if my crop_roi was (135,0,945,1920, the issue would go away, however CudaCrop does not like that and keeps saying invalid roi.

Hi @cloud9ine, can you post the code you are using? Here is the sample I tested this with:

Does that make correct image for you?

Hi @dusty_nv

Your code is working properly on my acquired image with a crop ratio of 0.5. Here is my code. Code prior to this point puts the x coordinates for cropping in left and right and y coordinates in top and bottom.

	crop_roi = (left, top, right, bottom)
	imgOutput = jetson.utils.cudaAllocMapped(width=right-left, height=bottom-top, format=img.format)
	print (crop_roi, img.width, img.height, imgOutput.width, imgOutput.height)	
	jetson.utils.cudaCrop(img, imgOutput, crop_roi)

The code as written above results in the error below:

((135, 0, 945, 1920), 1080, 1920, 810, 1920)
Traceback (most recent call last):
  File "", line 91, in <module>
    jetson.utils.cudaCrop(img, imgOutput, crop_roi)
ValueError: jetson.utils -- cudaCrop() had an invalid ROI
[gstreamer] gstCamera -- stopping pipeline, transitioning to GST_STATE_NULL

You can see the crop_roi, input image dimensions, and the output image dimensions in the print output above. If I change the crop_roi in the above code as below, it works but results in the skewed image I posted earlier.

crop_roi = (left, top, right-1, bottom-1)

And the print output changes to:

((135, 0, 944, 1919, 1080, 1920, 810, 1920)

Here is how I replaced my code with yours in this section and it works properly without any complaint of invalid crop_roi.

`# determine the amount of border pixels (cropping around the center by half)`
	crop_factor = 0.5
	crop_border = ((1.0 - crop_factor) * 0.5 * img.width,
		       (1.0 - crop_factor) * 0.5 * img.height)

# compute the ROI as (left, top, right, bottom)
crop_roi = (crop_border[0], crop_border[1], img.width - crop_border[0], img.height - crop_border[1])

# allocate the output image, with the cropped size
imgOutput = jetson.utils.cudaAllocMapped(width=img.width * crop_factor,
	                                 height=img.height * crop_factor,
print (crop_roi, img.width, img.height, imgOutput.width, imgOutput.height)
# crop the image to the ROI
jetson.utils.cudaCrop(img, imgOutput, crop_roi)

Here is the output of the print:

((270.0, 480.0, 810.0, 1440.0), 1080, 1920, 540, 960)

Logically, since this works, my original version should have worked as well.

Next, I am going to revert to my original code and try to force your crop_roi numbers to result from my code and see if it starts to work then.

Interesting. When I force my variables to arrive at the same crop_roi as your code, it works without any error. I think the invalid roi is stemming from my bottom being 1920 - perhaps that is out of bounds for a 1920 image. I tried sanitizing my input to keep bottom to 1919 max and right to 1079 max, and it seems to work now.

Hi @cloud9ine, the ROI refers to the pixel indexes for (left, top, right, bottom), and not a width/height. So the maximum for right is width-1 and the maximum for bottom is height-1, because the pixel indexes start at 0.

Yes, I see that now. The reason I had the issue was, opencv seems to accept the image height and width as the bottom and right coordinates in its crop function without complaining. I’m using detecting bounds and adding a margin and then limiting it to the image bounds. So even if the detection never went to width and height, the addition of margin was causing them to hit those bounds. I have modified my functions to change the limit to height minus 1 and width minus 1.