You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//
// Created by UnknownObject on 2022/11/4.
// Copyright (c) 2022 UnknownNetworkService. All rights reserved.
//
//OCR的图像预处理算法
#include "ocr_text.h"
namespace uns
{
//像素检查limit阈值是黑色的浓淡文字是图片中最黑的部分
bool OCRSupport::PixelCheck(const cv::Vec3b &pixel)
{
if (pixel[0] <= limit)
if (pixel[1] <= limit)
if (pixel[2] <= limit)
return true;
return false;
}
//图片裁切
cv::Mat OCRSupport::CutImageSize(const cv::Mat &img)
{
return img(roi);
}
//图片预处理,将彩色图片转换为黑白图片,确保文字是黑色的
cv::Mat OCRSupport::ConvertImage(const cv::Mat &img)
{
cv::imwrite("/sdcard/MainCar/OCR/ocr_old.jpg", img);
cv::Mat result(img.size(), img.type());
for (int r = 0; r < img.rows; r++)
{
for (int c = 0; c < img.cols; c++)
{
if (PixelCheck(img.at<cv::Vec3b>(r, c)))
result.at<cv::Vec3b>(r, c) = cv::Vec3b(0, 0, 0);
else
result.at<cv::Vec3b>(r, c) = cv::Vec3b(255, 255, 255);
}
}
cv::imwrite("/sdcard/MainCar/OCR/ocr_process.jpg", result);
return result;
}
};
//导出的自检函数
extern "C" JNIEXPORT
jstring JNICALL Java_com_uns_maincar_cpp_1interface_EnvTest_OCRTextTest(JNIEnv *env, jclass _this)
{
std::string version = "OCR Text Library Found, Version: " + std::string(OCR_TEXT_VERSION);
return env->NewStringUTF(version.c_str());
}
//导出的图像预处理函数
extern "C" JNIEXPORT
jobject JNICALL
Java_com_uns_maincar_cpp_1interface_OCR_ProcessImage(JNIEnv *env, jclass _this, jobject image,
jboolean self_test)
{
cv::Mat source;
uns::OCRSupport ocr_supp;
BitmapToMat(env, image, source);
cv::Mat img = (self_test ? ocr_supp.ConvertImage(source) : ocr_supp.ConvertImage(
ocr_supp.CutImageSize(source)));
jobject bmp = GenerateBitmap(env, img.cols, img.rows);
MatToBitmap(env, img, bmp);
return bmp;
}