Ask Your Question
0

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

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

drivenj17 gravatar image

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

Hi,

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: https://github.com/Itseez/opencv/blob...):

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

result Mat properties:

  • result.rows 2310
  • result.cols 5545
  • result.type 19
  • result.channel 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
  • toCrop.channel 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
1

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

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;
result.convertTo(result8u,CV_8U);
cvtColor(result8u,gray,COLOR_BGR2GRAY);
edit flag offensive delete link more

Comments

Thanks for this, it works :)

drivenj17 gravatar imagedrivenj17 ( 2015-06-04 13:04:11 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 30,649 times

Last updated: Jun 03 '15