0

I read an image(the original image) into an CvMat using cvLoadImage, and assign its data pointer to another CvMat by an intermediate data block, and then show this CvMat using cvShowImage, but only a small region of the original image was displayed, in a window with the same size as the original image.

char* filename = "image\\neuron.tif"; // an 8bits grayscale image
IplImage*  iplImage = cvLoadImage(filename);
int width = iplImage->width;
int height = iplImage->height;
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);
cvShowImage("Original", iplImage);  // showed properly
cvWaitKey();

CvMat* header = cvCreateMatHeader(height, width, CV_8UC1);
CvMat* img_mat = cvGetMat(iplImage, header);


unsigned char* img_host = (unsigned char*)malloc(height * width * sizeof(unsigned char));// the intermediate data block, on which I need to apply a cuda operation
img_host = img_mat->data.ptr;
CvMat* blur_mat = cvCreateMat(height, width, CV_8UC1);
blur_mat->data.ptr = img_host;
cvNamedWindow("Blurred", CV_WINDOW_AUTOSIZE);
cvShowImage("Blurred", blur_mat);  // the problem described
cvWaitKey();

image output Actually the problem occured when I try "img_host = img_mat->data.ptr", but I have no idea what's wrong.

3
  • change cvLoadImage(filename) to cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE); Commented Jan 11, 2017 at 3:39
  • 1
    Any particular reason you're using obsolete C api? Commented Jan 11, 2017 at 8:37
  • @Miki I would like to have other CUDA operations on the image data matrix, so I chose C api... really ashamed that I posted this messy code Commented Jan 12, 2017 at 9:27

1 Answer 1

1

Your problem is that your input image is not really a single channel grayscale 8UC1 image. What you are seeing is the first third of the image where each BGR pixel is displayed as 3 grayscale pixels.

To fix this you can either read your input in grayscale, as @eyllanesc suggested in the comments: cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);.

Alternatively, you need to declare your target as 8UC3.

In any case, your code is very messy and there are at least two memory leaks here:

unsigned char* img_host = (unsigned char*)malloc(height * width * sizeof(unsigned char));// the intermediate data block, on which I need to apply a cuda operation
img_host = img_mat->data.ptr; // LEAKS malloced buffer data 

CvMat* blur_mat = cvCreateMat(height, width, CV_8UC1);
blur_mat->data.ptr = img_host; // LEAKS cvCreateMat() buffer data

I really really recommend that you use the higher level C++ API with cv::Mat instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.