android opencv matrix,How to normalize OpenCV Android matrices?

tl;dr My KNearest training data and real data don't have the same dimensions and cause my app to crash. I suspect that either my preProces method of the way I instantiate my training data (drawable resource => bitmap => opencv matrix) is the reason for failure. Does any of you know a solution?

I've been trying to get a working demo of a simple OCR app with OpenCV for Android. I use the build in KNearest to recognize the characters. Before a KNearest object is capable of detecting anything, it has to be trained. For the training I use several character outlines.

281e40150789c871b4ac3f8ab540889d.png This is one of them (its a zero).

The training seems to work unsurprisingly it is capable to detect the supposed values of the training images. I wish it did that with other images as well (or at leas not crash my app). This is what I did to train the KNearest model:

Map images = new HashMap<>();

images.put('0', R.drawable.training0);

// Prepare two sets of data, the images and their values.

Mat trainingImages = new Mat();

Mat trainingLabels = new Mat();

for (int i = 0; i < 50; i++) {

for (Map.Entry entry : images.entrySet()) {

Bitmap bitmapImage = BitmapFactory.decodeResource(

this.getResources(), entry.getValue());

Mat matImage = new Mat();

Utils.bitmapToMat(bitmapImage, matImage);

trainingLabels.push_back(new MatOfInt(entry.getKey() - '0'));

trainingImages.push_back(

preProces(

matImage, new Rect(0, 0, matImage.width(), matImage.height())));

}

}

mKNearest.train(trainingImages, Ml.ROW_SAMPLE, trainingLabels);

The preProces method does nothing more than normalizing a matrix. This is what my preProces method looks like:

private Mat preProces(Mat image, Rect poi) {

Mat cutout = new Mat(image, poi);

Mat resized = new Mat(10, 10, CvType.CV_32F);

Mat converted = new Mat();

Imgproc.resize(cutout, resized, resized.size());

resized.reshape(1, 1).convertTo(converted, CvType.CV_32F);

return converted;

}

Segmenting the image to find (possible) characters was not that difficult, I was able to draw rectangles around the (possible) characters. Once that is done I just pass every point of interest through my preProces method before I pass it into the mKNearest.findNeareset(...) method. This is when the crash happens. The training data and the real data don't seem to have the same dimensions, something the preProces method should solve.

My guess is that either my preProces method fails or that loading drawable resources as bitmap and then converting them to matrices is the reason why it fails. I'd like to know if some of you had similar problems and how you've solved it.

Update: It seems there is quite a bit of noise in the matrices which where created out of a bitmap. Could this be the problem, if so how does one remove the noise?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值