Ask Your Question

How to convert from CV_16S to CV_8U || CV_16U || CV_32F? cvtColor assertion failed

asked 2015-06-03 18:46:55 -0500

drivenj17 gravatar image

updated 2015-06-03 18:51:09 -0500


I am having trouble converting a Mat to gray scale using the following (more details below)

cv::cvtColor(result, gray, COLOR_BGR2GRAY);

result is a Mat. It is the output of image stitching (from the sample:

Mat result, result_mask; blender->blend(result, result_mask);

result Mat properties:

  • result.rows 2310
  • result.cols 5545
  • result.type 19
  • 3
  • result.depth 3

I am trying to convert this Mat result to grayscale as a first step in another cropping function:

cv::Mat gray;
cv::cvtColor(result, gray, COLOR_BGR2GRAY);

but fails at the assertion on line "cv::cvtColor(result, gray, COLOR_BGR2GRAY);":

OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cv::cvtColor, file C:\builds\master_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\color.cpp, line 7343

If I imwrite the result Mat above, and imread it back in like so:

imwrite(result_name, result);
Mat toCrop = imread(result_name, CV_LOAD_IMAGE_COLOR);
cv::Mat gray;
cv::cvtColor(toCrop, gray, COLOR_BGR2GRAY);

Then there are no errors

toCrop Mat properties after reading back in are:

  • toCrop.rows 2310
  • toCrop.cols 5545
  • toCrop.type 16
  • 3
  • toCrop.depth 0

Any ideas on how to get this working without writing and reading, using the original Mat result to convert to grayscale? I believe the depth is the issue. The result is I've been searching how to do a conversion of the depth but no progress..

Thanks for your support

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-03 21:01:15 -0500

i found the solution here

use result.convertTo() to convert it from CV_16S to CV_8U than you can convert color to gray.

Mat result8u,gray;
edit flag offensive delete link more


Thanks for this, it works :)

drivenj17 gravatar imagedrivenj17 ( 2015-06-04 13:04:11 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-06-03 18:46:55 -0500

Seen: 22,326 times

Last updated: Jun 03 '15