在实战中,由于环境光的干扰,如果直接对图片进行算法处理,会错误的提取多余的特征,导致算法的准确度和速度大大降低。我们可以通过调整摄像机曝光或者使用亮度处理的函数,减少背景光的干扰。
#include
#include
using namespace cv;
using namespace std;
int pos = 0;
float alpha = 1;
float beta = 0;
Mat src, dst, dst1;
void set_brightness(int,void*) {
src = imread("test.jpg");
//cvtColor(src, src, COLOR_BGR2GRAY);
int height = src.rows;
int width = src.cols;
beta = pos;
dst = Mat::zeros(src.size(), src.type());
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
if (src.channels() == 3) {//如果读入图象是rgb图像
float b = src.at<Vec3b>(i, j)[0];
float g = src.at<Vec3b>(i, j)[1];
float r = src.at<Vec3b>(i, j)[2];
dst.at<Vec3b>(i, j)[0] = saturate_cast<uchar> (b * alpha + beta);
dst.at<Vec3b>(i, j)[1] = saturate_cast<uchar> (g * alpha + beta);
dst.at<Vec3b>(i, j)[2] = saturate_cast<uchar> (r * alpha + beta);
}
else if (src.channels() == 1) {如果是灰度图像
float m = src.at<uchar>(i, j);
dst.at<uchar>(i, j) = saturate_cast<uchar> (m * alpha + beta);
}
}
imshow("out", dst);
}
int main() {
namedWindow("out", 1);//创建窗口
createTrackbar("Brightness", "out", &pos, 100, set_brightness);//创建进度条
set_brightness(0,0);//调用设置亮度函数
imshow("input", src);
waitKey(0);
}
#include
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
// 读入图像,判断读入是否成功
string fileName = samples::findFile("path");
Mat src = imread(fileName, IMREAD_COLOR);
if (src.empty())
{
fprintf(stderr, "failed to load image: %s\n", fileName);
system("pause");
return EXIT_FAILURE;
}
Mat dst1, dst2, dst3;
dst1 = Mat::zeros(src.size(), src.type());
double alpha = 1.0;
double beta = 0.0;
double gama = 1.0;
// 提示并输入 α β γ 的值
cout << " Basic Linear Transforms " << endl;
cout << "-------------------------" << endl;
cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha;
cout << "* Enter the beta value [0-100]: "; cin >> beta;
cout << "* Enter the gamma value [-1,1]: "; cin >> gama;
// 直接使用循环遍历每一个像素,应用公式
double t1 = (double)getTickCount();
for (int row=0;row<src.rows;++row)
for(int col=0;col<src.cols;++col)
for (int channel = 0; channel < src.channels(); ++channel)
{
dst1.at<Vec3b>(row, col)[channel] = saturate_cast<uchar>(alpha * src.at<Vec3b>(row, col)[channel] + beta);
}
double time1 = ((double)getTickCount() - t1) / getTickFrequency();
cout << "Method by pixel use time:" << time1 << "(ms)" << endl;
// 调用 convertTo() 函数调整对比度和亮度
double t2 = (double)getTickCount();
src.convertTo(dst2, -1, alpha, beta);
double time2 = ((double)getTickCount() - t2) / getTickFrequency();
cout << "Method by pixel use time:" << time2 << "(ms)" << endl;
// 构建查找表
Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for (int i = 0; i < 256; ++i)
p[i] = saturate_cast<uchar>(pow(i / 255.0, gama) * 255.0);
// 使用查找表进行对比度亮度调整
double t3 = (double)getTickCount();
LUT(src, lookUpTable, dst3);
double time3 = ((double)getTickCount() - t3) / getTickFrequency();
cout << "Method by Gamma correct use time:" << time3 << "(ms)" << endl;
// 调整窗体大小,显示调整效果
namedWindow("original", WINDOW_NORMAL);
resizeWindow("original", Size(src.cols, src.rows));
imshow("original", src);
namedWindow("pixel set", WINDOW_NORMAL);
resizeWindow("pixel set", Size(src.cols, src.rows));
imshow("pixel set", dst1);
namedWindow("convertTo", WINDOW_NORMAL);
resizeWindow("convertTo", Size(src.cols, src.rows));
imshow("convertTo", dst2);
namedWindow("Gamma correct", WINDOW_NORMAL);
resizeWindow("Gamma correct", Size(src.cols, src.rows));
imshow("Gamma correct", dst3);
waitKey(0);
system("pause");
return EXIT_SUCCESS;
}
这个是自己调整参数,分别有α,β和γ
cvtColor(img, imgGray, COLOR_BGR2GRAY);
GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0);
Canny(imgBlur, imgCanny, 25, 75);
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
dilate(imgCanny, imgDil, kernel);
erode(imgDil, imgErode, kernel);
图像二值化( ImageBinarization)就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程。在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。相当于将输入图像中阈值外的像素全部置0,使得阈值内目标凸出,便于PC进行灯条识别。
直接在上面代码上进行修改,用已经灰度的图片进行二值化:
threshold(imgGray, dst, 100, 255, THRESH_BINARY_INV);
imshow("image dst", dst);
#include
#include
#include
#include
using namespace cv;
using namespace std;
void main() {
string path = "C:\\Users\\tanghaoran\\Pictures\\Saved Pictures\\3.png";
Mat img = imread(path);
Mat imgGray, imgBlur, imgCanny, imgDil, imgErode,dst;
//将照片转换为灰度
cvtColor(img, imgGray, COLOR_BGR2GRAY);
threshold(imgGray, dst, 100, 255, THRESH_BINARY_INV);
//高斯模糊
GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0);
//Canny边缘检测器 一般在使用Canny边缘检测器之前会做一些模糊处理
Canny(imgBlur, imgCanny, 25, 75);
//创建一个可以使用膨胀的内核
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
//图像膨胀
dilate(imgCanny, imgDil, kernel);
//图像侵蚀
erode(imgDil, imgErode, kernel);
//结果呈现
imshow("Image", img);
imshow("Image Gray", imgGray);
imshow("Image Blur", imgBlur);
imshow("Image Canny", imgCanny);
imshow("Image Dilation", imgDil);
imshow("Image Erode", imgErode);
imshow("image dst", dst);
waitKey(0);
}
这里只是简单演示一下操作。
在RM中,具体步骤应该是读取图像—>灰度化—>二值化—>轮廓提取,之后再进行其他操作,这样就可以避免上图所示的线条过多的情况从而提高机器人的识别率和准确度。