OpenCV(4.1.0) Error: Assertion failed (((0x28442211 >> ((traits::Depth<_Tp>::value) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\opencv\build\include\opencv2\core\mat.inl.hpp, line 1145
I used this code in Visual Studio 2017 to convert the 16-bit grayscale image to 8-bit, but the following question appeared when I started executing the exe file. ##
#include<iostream>
#include <opencv2/opencv.hpp>
#include <string>
#include <stdio.h>
//#include "pch.h"
//#include <direct.h>
using namespace std;
using namespace cv;
int main(void) {
char buff1[100];
char buff2[100];
for (int i = 1; i < 21; i++) {
//sprintf_s(buff1, "E:\\Mask_RCNN-master\\My Images\\labelme_json\\%d_json\\label.png",i);
//sprintf_s(buff2, "E:\\Mask_RCNN-master\\My Images\\cv2_mask\\%d.png", i);
sprintf_s(buff1, "./My Images/labelme_json/%d_json/label.png", i);
sprintf_s(buff2, "./My Images/cv2_mask/%d.png", i);
//sprintf(buff1,"/media/lj/FA68-10A6/test_drug/disp/disp_%d.png",i);
//sprintf(buff2,"/media/lj/FA68-10A6/test_drug/disp_8/disp_%d.png",i);
//Mat src;
//Mat dst;
Mat src = imread(buff1, IMREAD_UNCHANGED);
Mat ff = Mat::zeros(src.rows, src.cols, CV_8UC1);
for(int k = 0; k<src.rows; k++)
{
for (int kk=0; kk<src.cols; kk++)
{
int n = src.at<ushort>(k, kk);
ff.at<uchar>(k, kk) = n;
}
}
imwrite(buff2,ff);
}
return 0;
}
I get the error: ##
- OpenCV(4.1.0) Error: Assertion failed (((0x28442211 >> ((traits::Depth<_Tp>::value) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\opencv\build\include\opencv2\core\mat.inl.hpp, line 1145
- OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(4.1.0) Error: Assertion failed (((0x28442211 >> ((traits::Depth<_Tp>::value) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\opencv\build\include\opencv2\core\mat.inl.hpp, line 1145
I hope to get your advice. Thank you
don't. use
src.convertTo(dst,CV_8U);
instead.Do you mean that one line of code can replace
Mat ff = Mat::zeros(src.rows, src.cols, CV_8UC1);
withsrc.convertTo(dst,CV_8U);
no, a single line will replace your whole, ill-fated for-loops