目錄
1、圖像像素比較
1.1 比較函數(shù)
?1.2 圖像最大值最小值尋找
2、圖像像素邏輯操作
3、圖像二值化
3.1 固定閾值二值化
3.2 自適應閾值二值化
1、圖像像素比較
1.1 比較函數(shù)
?1.2 圖像最大值最小值尋找
?文章來源地址http://www.zghlxwxcb.cn/news/detail-521521.html
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 自適應閾值二值化
文章來源:http://www.zghlxwxcb.cn/news/detail-521521.html
?
//自適應閾值二值化
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);
到了這里,關于圖像像素操作與二值化的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!