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?