英文:
src.type() == CV_8UC1 assertion failed when deskewing image
问题
Bitmap bitmap = bitmapDrawable.getBitmap();
Mat matImg = new Mat();
Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, matImg);
computeSkew(matImg);
public Mat deskew(Mat src, double angle) {
Point center = new Point(src.width()/2, src.height()/2);
Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1.0);
Size size = new Size(src.width(), src.height());
Imgproc.warpAffine(src, src, rotImage, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS);
return src;
}
public void computeSkew(Mat img) {
Imgproc.threshold( img, img, 200, 255, THRESH_BINARY );
Core.bitwise_not( img, img );
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Imgproc.erode(img, img, element);
Mat wLocMat = Mat.zeros(img.size(),img.type());
Core.findNonZero(img, wLocMat);
MatOfPoint matOfPoint = new MatOfPoint( wLocMat );
MatOfPoint2f mat2f = new MatOfPoint2f();
matOfPoint.convertTo(mat2f, CvType.CV_32FC2);
RotatedRect rotatedRect = Imgproc.minAreaRect( mat2f );
Point[] vertices = new Point[4];
rotatedRect.points(vertices);
List<MatOfPoint> boxContours = new ArrayList<>();
boxContours.add(new MatOfPoint(vertices));
Imgproc.drawContours( img, boxContours, 0, new Scalar(128, 128, 128), -1);
double resultAngle = rotatedRect.angle;
if (rotatedRect.size.width > rotatedRect.size.height)
{
rotatedRect.angle += 90.f;
}
Mat result = deskew( img, rotatedRect.angle );
}
Error:
2020-10-09 17:41:33.090 12013-12013/com.example.firstapp E/cv::error(): OpenCV(3.4.4) Error: Assertion failed (src.type() == CV_8UC1) in void cv::findNonZero(cv::InputArray, cv::OutputArray), file /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp, line 332
2020-10-09 17:41:33.092 12013-12013/com.example.firstapp E/org.opencv.core: core::findNonZero_10() caught cv::Exception: OpenCV(3.4.4) /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp:332: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'void cv::findNonZero(cv::InputArray, cv::OutputArray)'
2020-10-09 17:41:33.094 12013-12013/com.example.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstapp, PID: 12013
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { (has extras) }} to activity {com.example.firstapp/com.example.firstapp.Haupt}: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.4) /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp:332: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'void cv::findNonZero(cv::InputArray, cv::OutputArray)']
at android.app.ActivityThread.deliverResults(ActivityThread.java:5587)
...
英文:
My goal is to achieve image deskewing and I'm using OpenCV. My first step is to convert a bitmap to a Mat, after that I use the function computeSkew(matImg)
as you can see below. Unfortunately this isn't working as I get the following error message. Please help me to solve the problem.
Bitmap bitmap = bitmapDrawable.getBitmap();
Mat matImg = new Mat();
Bitmap bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, matImg);
computeSkew(matImg);
public Mat deskew(Mat src, double angle) {
Point center = new Point(src.width()/2, src.height()/2);
Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1.0);
Size size = new Size(src.width(), src.height());
Imgproc.warpAffine(src, src, rotImage, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS);
return src;
}
public void computeSkew(Mat img) {
Imgproc.threshold( img, img, 200, 255, THRESH_BINARY );
Core.bitwise_not( img, img );
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Imgproc.erode(img, img, element);
Mat wLocMat = Mat.zeros(img.size(),img.type());
Core.findNonZero(img, wLocMat);
MatOfPoint matOfPoint = new MatOfPoint( wLocMat );
MatOfPoint2f mat2f = new MatOfPoint2f();
matOfPoint.convertTo(mat2f, CvType.CV_32FC2);
RotatedRect rotatedRect = Imgproc.minAreaRect( mat2f );
Point[] vertices = new Point[4];
rotatedRect.points(vertices);
List<MatOfPoint> boxContours = new ArrayList<>();
boxContours.add(new MatOfPoint(vertices));
Imgproc.drawContours( img, boxContours, 0, new Scalar(128, 128, 128), -1);
double resultAngle = rotatedRect.angle;
if (rotatedRect.size.width > rotatedRect.size.height)
{
rotatedRect.angle += 90.f;
}
Mat result = deskew( img, rotatedRect.angle );
}
Error:
2020-10-09 17:41:33.090 12013-12013/com.example.firstapp E/cv::error(): OpenCV(3.4.4) Error: Assertion failed (src.type() == CV_8UC1) in void cv::findNonZero(cv::InputArray, cv::OutputArray), file /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp, line 332
2020-10-09 17:41:33.092 12013-12013/com.example.firstapp E/org.opencv.core: core::findNonZero_10() caught cv::Exception: OpenCV(3.4.4) /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp:332: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'void cv::findNonZero(cv::InputArray, cv::OutputArray)'
2020-10-09 17:41:33.094 12013-12013/com.example.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstapp, PID: 12013
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { (has extras) }} to activity {com.example.firstapp/com.example.firstapp.Haupt}: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.4) /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp:332: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'void cv::findNonZero(cv::InputArray, cv::OutputArray)'
]
at android.app.ActivityThread.deliverResults(ActivityThread.java:5587)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5628)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8347)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.4) /build/3_4_pack-android/opencv/modules/core/src/count_non_zero.cpp:332: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'void cv::findNonZero(cv::InputArray, cv::OutputArray)'
]
at org.opencv.core.Core.findNonZero_0(Native Method)
at org.opencv.core.Core.findNonZero(Core.java:1645)
at com.example.firstapp.Haupt.computeSkew(Haupt.java:652)
at com.example.firstapp.Haupt.onActivityResult(Haupt.java:469)
at android.app.Activity.dispatchActivityResult(Activity.java:8412)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5580)
... 11 more
答案1
得分: 0
问题出在你的 Core.findNonZero
调用上。你正在为它提供一个不兼容的 Mat
输入。输入应为单通道8位图像。根据你的代码,看起来你正在将一个彩色图像提供给你的去倾斜函数,并最终传递给 Core.findNonZero
调用。在进行阈值化之前,尝试先将图像转换为灰度图:
// 新代码
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);
// 你的代码
Imgproc.threshold(img, img, 200, 255, THRESH_BINARY);
// ...
// ...
英文:
The problem is with your Core.findNonZero
call. You are providing an incompatible input Mat
to it. The input is expected to be a single channel 8-bit image. From your code, it looks like you are providing a colour image to your deskew function and ultimately the Core.findNonZero
call. Try converting the image to grayscale first prior to thresholding:
// New code
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);
// Your code
Imgproc.threshold(img, img, 200, 255, THRESH_BINARY);
// ...
// ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论