Converting RGB to Grayscale with Jetson-Utils

I have been trying to use the imageIO library and cudaConvertColor function to load an RGB image, convert it to grayscale, then save the image to a new file.

Below is my code:

#include <jetson-utils/cudaColorspace.h>
#include <jetson-utils/cudaMappedMemory.h>
#include <jetson-utils/imageIO.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[]) {
    uchar4  *src=NULL;
    uint8_t *dst=NULL;
    int     width=0, height=0;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s image_source image_dest\r\n", argv[0]);
        return EXIT_FAILURE;
    }

    if (!loadImage(argv[1], (void**)&src, &width, &height, IMAGE_RGBA8)) {
        fprintf(stderr, "Error loading image\r\n");
        return EXIT_FAILURE;
    }
    cudaAllocMapped(&dst, width*height*sizeof(uint8_t));
    cudaConvertColor(src, IMAGE_RGBA8, dst, IMAGE_GRAY8, width, height);

    if (!saveImage(argv[2], dst, width, height)) {
        fprintf(stderr, "Failed to save image to %s\r\n", argv[2]);
    }

    cudaFree(src);
    cudaFree(dst);
    return EXIT_SUCCESS;
}

When trying to build the program, I ran into the following error.

/usr/local/include/jetson-utils/imageFormat.inl(234): error: static assertion failed with “invalid image format type - supported types are uchar3, uchar4, float3, float4”
static_assert(__image_format_assert_false::value, “invalid image format type - supported types are uchar3, uchar4, float3, float4”);
^
detected during:
instantiation of “imageFormat imageFormatFromType() [with T=uint8_t]”

I looked into the file that was throwing the error, and I found the following:

template<> inline imageFormat imageFormatFromType<uchar3>()	{ return IMAGE_RGB8; }
template<> inline imageFormat imageFormatFromType<uchar4>()	{ return IMAGE_RGBA8; }
template<> inline imageFormat imageFormatFromType<float3>()	{ return IMAGE_RGB32F; }
template<> inline imageFormat imageFormatFromType<float4>()	{ return IMAGE_RGBA32F; }

As an experiment, I added the following line, rebuilt, and the error went away. Doing this, I was able to perform the RGB to grayscale conversion with cudaConvertColor.

template<> inline imageFormat imageFormatFromType<uint8_t>() { return IMAGE_GRAY8; }

Is adding this line necessary? Are there other ways to setup the cudaConvertColor to perform RGB to grayscale that doesn’t involve modifying the underlying libraries/includes?

Sorry for the late response.
Is this still an issue to support? Any result can be shared?

No worries, I was wondering if using cudaConvertColor to convert an RGB image to a grayscale image and save the grayscale image was supported.

I was getting a “static assert” error with the saveImage command when trying to save the grayscale image, but I was able to fix the error by adding an imageFormatFromType for grayscale types (i.e. uint8_t) as mentioned in the original post. As this modifies the underlying library code, specifically jetson-utils/imageFormat.inl, I was wondering if this is the best solution, or if there is a way to convert RGB to grayscale and save the grayscale image that doesn’t result in the static assert error.

Hi

Thanks for sharing this info.

Is the output expected?
If yes, could you submit a PR to the jetson_utils GitHub so our internal can review and integrate it?

https://github.com/dusty-nv/jetson-utils/pulls

Thanks.

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