目录
1、图像像素比较
1.1 比较函数
1.2 图像最大值最小值寻找
2、图像像素逻辑操作
3、图像二值化
3.1 固定阈值二值化
3.2 自适应阈值二值化
1、图像像素比较
1.1 比较函数
1.2 图像最大值最小值寻找
Mat img = imread("F:/testMap/bijiao.png");
Mat white = imread("F:/testMap/white.png");
Mat black = imread("F:/testMap/black.png");
Mat Min, Max;
min(img, white, Min);
max(img, black, Max);
Mat gray,gray_black;
cvtColor(img,gray,COLOR_BGR2GRAY);
cvtColor(black,gray_black,COLOR_BGR2GRAY);
double minVal,maxVal;
Point minLoc,maxLoc;
minMaxLoc(gray,&minVal,&maxVal,&minLoc,&maxLoc,gray_black);
2、图像像素逻辑操作
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat a = (Mat_<uchar>(1,2) << 0,5);
Mat b = (Mat_<uchar>(1,2) << 0,6);
Mat Xor,Or,Not,And;
bitwise_not(a,Not);
cout << "a Not" << Not << endl;
bitwise_and(a, b, And);
bitwise_or(a, b, Or); bitwise_xor(a,b,Xor);
cout << And << endl; cout << Or << endl; cout << Xor << endl;
Mat img = imread("F:/testMap/bijiao.png");
Mat mark = imread("F:/testMap/black.png");
Mat result;
bitwise_and(img, mark, result);
Mat img_inv;
//bitwise_not(img, img_inv);
cvtColor(mark,mark,COLOR_BGR2GRAY);
bitwise_not(img,img_inv,mark);
Mat mark_black = imread("F:/testMap/black.png");
min(img,mark_black,img);
img = img + img_inv;
system("pause");
return 0;
}
3、图像二值化
3.1 固定阈值二值化
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("F:/testMap/lena.png");
if (img.empty()){
cout << "请确认图像文件名称是否正确" << endl;
return -1;
}
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
Mat img_B, img_B_V, gray_B, gray_B_V, gray_T, gray_T_V, gray_TRUNC;
//彩色图像二值化
threshold(img, img_B, 125, 255, THRESH_BINARY);
threshold(img, img_B_V, 125, 255, THRESH_BINARY_INV);
//灰度图BINARY二值化
threshold(gray,gray_B,125,255,THRESH_BINARY);
threshold(gray,gray_B_V,125,255,THRESH_BINARY_INV);
//灰度图像TOZERO变换
threshold(gray,gray_T,125,255,THRESH_TOZERO);
threshold(gray,gray_T_V,125,255,THRESH_TOZERO_INV);
//灰度图像TRUNC变换
threshold(gray,gray_TRUNC,125,255,THRESH_TRUNC);
//灰度图像大津法和三角形法二值化
Mat img_Thr = imread("F:/testMap//threshold.jpg",IMREAD_GRAYSCALE);
Mat img_Thr_O, img_Thr_T;
threshold(img_Thr,img_Thr_O,100,255,THRESH_BINARY | THRESH_OTSU); //明暗渐变的图像得出的结果不理想
threshold(img_Thr, img_Thr_T,125,255,THRESH_BINARY | THRESH_TRIANGLE); //明暗渐变的图像得出的结果不理想
system("pause");
return 0;
}
3.2 自适应阈值二值化
//自适应阈值二值化
Mat adaptive_mean,adaptive_gauss;
//均值法
adaptiveThreshold(img_Thr,adaptive_mean,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,55,0);
//高斯法
adaptiveThreshold(img_Thr,adaptive_gauss,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY,55,0);