Files
HandWritten-Analisys/测试/本机测试/2-侯-算法/handwriting/binary.cpp
yanshui177 962de04ffb 笔迹鉴别程序
考试的笔迹鉴别程序,分辨出不同人写的笔迹
2017-05-17 16:50:37 +08:00

48 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 程序名binary.c
功能:读入图像文件,进行二值化
*/
#pragma once
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
int* binary(IplImage* img,int bithro)
{
int height,width,step,channels;
uchar *data;
int i,j;
static int black[1000]; //C语言不提倡返回一个局部变量的地址以外的功能所以你必须定义的局部变量如静态变量。
/* 获取图像信息*/
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
/*二值化,并统计黑像素的个数*/
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)//对图像每个点进行二值化,原值为128
data[i*step+j*channels]=(data[i*step+j*channels]>bithro)?255:0;
}
/*计算每一行的黑像素个数*/
int tempBlackPixel=0;
memset(black,0,1000); //##初始化内存这里用做清零black数组
for(i=height-1;i>0;i--)
{
for(int j=0;j<width;j++)
{
if(data[i*step+j*channels]==0) //计算黑色的像素数
tempBlackPixel+=1;
}
black[height-i]=tempBlackPixel; //black记录黑色像素数
tempBlackPixel=0;
}
//二值化,并统计黑像素的个数**********
return black;
}