记opencv3.4 环境下 OCR识别使用到的图像增强算法
黑色增强
void filteredblack(const Mat &inputImage) {
Mat hsvImage;
Mat jiangzao, jiangzao2, jiangzao3;
inputImage.copyTo(hsvImage);
double H = 0.0, S = 0.0, V = 0.0;
for (int i = 0; i < hsvImage.rows; i++)
{
for (int j = 0; j < hsvImage.cols; j++)
{
H = hsvImage.at(i, j)[0];
S = hsvImage.at(i, j)[1];
V = hsvImage.at(i, j)[2];
if (H <= 90 && S <= 90 && V <= 90) /*黑色判断阀值*/
{
hsvImage.at(i, j)[0] = 0;
hsvImage.at(i, j)[1] = 0;
hsvImage.at(i, j)[2] = 0;
}
}
}
blur(hsvImage, hsvImage, Size(5, 5));
blur(hsvImage, hsvImage, Size(4, 4));
blur(hsvImage, hsvImage, Size(3, 3));
hsvImage.copyTo(inputImage);
hsvImage.release();
}
拉普拉斯算子图像增强
void lapusi(Mat &src)
{
Mat image;
src.copyTo(image);
Mat imageEnhance;
Mat kernel = (Mat_(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);
filter2D(image, imageEnhance, CV_8UC3, kernel);
imageEnhance.copyTo(src);
}
伽马变换图像增强
void gama(Mat &src)
{
Mat image;// = imread("Test.jpg");
src.copyTo(image);
Mat imageGamma(image.size(), CV_32FC3);
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
imageGamma.at(i, j)[0] = (image.at(i, j)[0])*(image.at(i, j)[0])*(image.at(i, j)[0]);
imageGamma.at(i, j)[1] = (image.at(i, j)[1])*(image.at(i, j)[1])*(image.at(i, j)[1]);
imageGamma.at(i, j)[2] = (image.at(i, j)[2])*(image.at(i, j)[2])*(image.at(i, j)[2]);
}
}
//归一化到0~255
normalize(imageGamma, imageGamma, 0, 255, CV_MINMAX);
//转换成8bit图像显示
convertScaleAbs(imageGamma, imageGamma);
imageGamma.copyTo(src);
}
图像腐蚀
Mat kernel_erode = getStructuringElement(MORPH_RECT, Size(3, 3));
erode(src, src, kernel_erode);
图像亮度均化
void unevenLightCompensate(Mat &image, int blockSize)//图像亮度均化
{
if (image.channels() == 3) cvtColor(image, image, CV_BGR2GRAY);
Mat jiangzao, jiangzao2, jiangzao3;
blur(image, jiangzao, Size(4, 4)); //降噪处理
blur(jiangzao, jiangzao2, Size(5, 5)); //降噪处理
blur(jiangzao2, jiangzao3, Size(5, 5)); //降噪处理
double average = mean(image)[0];
int rows_new = ceil(double(image.rows) / double(blockSize));
int cols_new = ceil(double(image.cols) / double(blockSize));
Mat blockImage;
blockImage = Mat::zeros(rows_new, cols_new, CV_32FC1);
for (int i = 0; i < rows_new; i++)
{
for (int j = 0; j < cols_new; j++)
{
int rowmin = i * blockSize;
int rowmax = (i + 1)*blockSize;
if (rowmax > image.rows) rowmax = image.rows;
int colmin = j * blockSize;
int colmax = (j + 1)*blockSize;
if (colmax > image.cols) colmax = image.cols;
Mat imageROI = image(Range(rowmin, rowmax), Range(colmin, colmax));
double temaver = mean(imageROI)[0];
blockImage.at(i, j) = temaver;
}
}
blockImage = blockImage - average;
Mat blockImage2;
resize(blockImage, blockImage2, image.size(), (0, 0), (0, 0), INTER_CUBIC);
Mat image2;
image.convertTo(image2, CV_32FC1);
Mat dst = image2 - blockImage2;
dst.convertTo(image, CV_8UC1);
}
获取图像平均亮度
double getAvg(Mat img)
{
Mat gray;
if (img.channels() == 1)
{
gray = img;
}
else
{
cvtColor(img, gray, CV_RGB2GRAY);
}
Scalar scalar = mean(gray);
return scalar.val[0];
}
设置图像平均亮度
void setAvg(Mat scr, Mat dst, double avg)
{
double fpreAvg = getAvg(scr);
scr.convertTo(dst, scr.type(), avg / fpreAvg);
}
图像单通道转三通道
Mat convertTo3Channels( Mat binImg)
{
Mat three_channel = Mat::zeros(binImg.rows, binImg.cols, CV_8UC3);
vector channels;
if(binImg.channels()==1)
{
for (int i = 0; i < 3; i++)
{
channels.push_back(binImg);
}
merge(channels, three_channel);
return three_channel;
}
}