2021SC@SDUSC
高斯滤波处理(消除高斯噪声)
Mat MyClass::getGass(Mat image, bool show){
Mat cimage;
GaussianBlur(image, cimage, Size(3, 3), 0);
if (show)
imshow("高斯滤波图", cimage);
return cimage;
}
处理结果:

?Sobel x-y差处理(只考虑x轴,消除y轴不必要信息):
Mat MyClass::getSobel(Mat image, bool show){
Mat cimageX16s, cimageY16s, imageSobelX, imageSobelY, out;
Sobel(image, cimageX16s, CV_16S, 1, 0, 3, 1, 0, 4);
Sobel(image, cimageY16s, CV_16S, 0, 1, 3, 1, 0, 4);
convertScaleAbs(cimageX16s, imageSobelX, 1, 0);
convertScaleAbs(cimageY16s, imageSobelY, 1, 0);
out = imageSobelX - imageSobelY;
if (show)
imshow("Sobelx-y差 图", out);
return out;
}
处理结果:

?均值滤波处理(消除高频噪声)
Mat MyClass::getBlur(Mat image, bool show){
Mat cimage;
blur(image, cimage, Size(3, 3));
if (show)
imshow("均值滤波图", cimage);
return cimage;
}
?处理结果:

?
二值化处理(使图像中数据量大为减少,从而能凸显出目标的轮廓)
Mat MyClass::getThold(Mat image, bool show){
Mat cimage;
threshold(image, cimage, 112, 255, CV_THRESH_BINARY);
if (show)
imshow("二值化图", cimage);
return cimage;
}
处理结果:

?闭运算处理(扩大轴之间的间隙)
Mat MyClass::getBys(Mat image, bool show){
morphologyEx(image, image, MORPH_CLOSE, element);
if (show)
imshow("闭运算图", image);
return image;
}
处理结果:

?腐蚀膨胀(消去干扰点和合并条码区域)
Mat MyClass::getErode(Mat image, bool show){
//Mat cimage;
erode(image, image, element);
if (show)
imshow("腐蚀图", image);
return image;
}
Mat MyClass::getDilate(Mat image, bool show){
for (int i = 0; i < 3; i++)
dilate(image, image, element);
if (show)
imshow("膨胀图", image);
return image;
}
处理结果:

?

?
获取ROI(为Zbar处理作预处理)
Mat MyClass::getRect(Mat image, Mat simage, bool show){
vector<vector<Point>> contours;
vector<Vec4i> hiera;
Mat cimage;
findContours(image, contours, hiera, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<float>contourArea;
for (int i = 0; i < contours.size(); i++)
{
contourArea.push_back(cv::contourArea(contours[i]));
}
//找出面积最大的轮廓
double maxValue; Point maxLoc;
minMaxLoc(contourArea, NULL, &maxValue, NULL, &maxLoc);
//计算面积最大的轮廓的最小的外包矩形
RotatedRect minRect = minAreaRect(contours[maxLoc.x]);
//为了防止找错,要检查这个矩形的偏斜角度不能超标
//如果超标,那就是没找到
if (minRect.angle<2.0)
{
//找到了矩形的角度,但是这是一个旋转矩形,所以还要重新获得一个外包最小矩形
Rect myRect = boundingRect(contours[maxLoc.x]);
//把这个矩形在源图像中画出来
//rectangle(srcImage,myRect,Scalar(0,255,255),3,LINE_AA);
//看看显示效果,找的对不对
//imshow(windowNameString,srcImage);
//将扫描的图像裁剪下来,并保存为相应的结果,保留一些X方向的边界,所以对rect进行一定的扩张
myRect.x = myRect.x - (myRect.width / 20);
myRect.width = myRect.width*1.1;
Mat resultImage = Mat(srcimage, myRect);
return resultImage;
}
for (int i = 0; i<contours.size(); i++)
{
Rect rect = boundingRect((Mat)contours[i]);
//cimage = simage(rect);
rectangle(simage, rect, Scalar(0), 2);
if (show)
imshow("转变图", simage);
}
return simage;
}
?处理结果:

最后识别处理结果:

?
?
?
|