先上代碼。菇绵。肄渗。
#include <cv.h>
#include<highgui\highgui.hpp>
#include <string>
#include <cstdio>
void slat(cv::Mat &img, int n);
void colorReduce(cv::Mat &img, int div = 64);
void sharpen(const cv::Mat &img, cv::Mat &outimg);
using namespace std;
string sample_png = "D:\\sample.png";
string sample2_png = "D:\\sample2.png";
string shit_png = "D:\\shit.png";
//column 列 x 寬
//row 行 y 高
// img.at<type>(int y, int x);
int main()
{
cv::Mat image = cv::imread(sample_png);
cv::namedWindow(sample_png);
cv::imshow(sample_png, image);
//1
//椒鹽噪聲
//cv::Mat imageOut = cv::Mat(image);
//拷貝構(gòu)造函數(shù) 圖像數(shù)據(jù)共用的是一塊內(nèi)存翎嫡。
cv::namedWindow(sample_png + "_OUT1");
cv::Mat imageOut = cv::Mat(image);
slat(imageOut, 1024);
cv::imshow(sample_png + "_OUT1", imageOut);
//椒鹽噪聲 結(jié)束
//2
//cv::Mat_<type> 指定元素 image也不變 引用
// Mat_& operator = (const Mat& m) 引用
cv::namedWindow(sample_png + "_OUT2");
cv::Mat_ <cv::Vec3b> imageOut2 = image.clone();
for (int i = 0; i < 10240; i++)
{
int x = rand() % imageOut2.cols;
int y = rand() % imageOut2.rows;
imageOut2(y, x)[0] = 255;
imageOut2(y, x)[1] = 255;
imageOut2(y, x)[2] = 255;
}
cv::imshow(sample_png + "_OUT2", image);
cv::imwrite("D:\\sample_save.png", image);
//cv::Mat_ 結(jié)束
//3
//指針操作 函數(shù)聲明里設(shè)置默認(rèn)參數(shù)
cv::namedWindow(sample_png + "_OUT3");
cv::Mat imageOut3 = image.clone();
colorReduce(imageOut3,128);
cv::imshow(sample_png + "_OUT3", imageOut3);
cv::imshow(sample_png, image);
//指針操作 結(jié)束
//4
//clone 圖像復(fù)制
cv::Mat imgClone = image.clone();
//clone 結(jié)束
//5
//create 圖像
//create 如果現(xiàn)有的和原來的一樣 就回直接返回翅雏,不會(huì)進(jìn)行操作
//判斷源圖片和現(xiàn)有圖片是不是一樣的 不管out圖片有沒有分配內(nèi)存 都沒問題了 相當(dāng)于做了一個(gè)檢查
cv::Mat imgCreate;
imgCreate.create(cv::Size(image.cols,image.rows), image.type());
cv::namedWindow(sample_png + "_OUT5");
cv::imshow(sample_png + "_OUT5", imgCreate);
//create
//6
//銳化 拉普拉斯 0 -1 0
// -1 5 -1
// 0 -1 0
//遍歷圖像鄰域
//// 8bit, color or not
//IMREAD_UNCHANGED = -1,
// 8bit, gray
// IMREAD_GRAYSCALE = 0,
// ?, color
// IMREAD_COLOR = 1,
// any depth, ?
// IMREAD_ANYDEPTH = 2,
// ?, any color
// IMREAD_ANYCOLOR = 4
cv::Mat image2 = cv::imread(sample2_png,0);
cv::namedWindow(sample2_png);
cout << image2.channels()<<endl;
cv::imshow(sample2_png, image2);
cv::namedWindow(sample2_png + "_OUT6");
cv::Mat outimg;
sharpen(image2, outimg);
cv::imshow(sample2_png + "_OUT6", outimg);
//遍歷圖像鄰域 結(jié)束
//7
//ROI
cv::Mat shit = cv::imread(shit_png);
cv::namedWindow(sample_png + "_OUT7");
cv::Mat_ <cv::Vec3b> imageOut7 = image.clone();
//cv::Mat imageROI = imageOut7(cv::Rect(imageOut7.cols - shit.cols - 500, imageOut7.rows - shit.rows - 50, shit.cols, shit.rows));
cv::Mat imageROI = imageOut7(cv::Range(50, 50 + shit.cols), cv::Range(200, 200 + shit.rows));
//imageROI = imageROI + shit;
imageROI = imageROI - shit;
imageOut7.copyTo(shit, imageOut7);
cv::imshow(sample_png + "_OUT7", imageOut7);
cv::waitKey(0);
return 0;
}
void slat(cv::Mat &img, int n)
{
for (int i = 0; i < n; i++)
{
int x = rand() % img.cols;
int y = rand() % img.rows;
if (img.channels() == 1)
{
img.at<uchar>(y, x) = 255;
}
else if (img.channels() == 3)
{
//typedef Vec<uchar, 3> Vec3b
img.at<cv::Vec3b>(y, x)[0] = 255;
img.at<cv::Vec3b>(y, x)[1] = 255;
img.at<cv::Vec3b>(y, x)[2] = 255;
}
}
}
void colorReduce(cv::Mat &img, int div)
{
int nl = img.rows;
int nc = img.cols*img.channels();
for (int i = 0; i < nl; i++)
{
//每行首地址
uchar* data = img.ptr<uchar>(i);
for (int j = 0; j < nc; j++)
{
data[j] = data[j] / div*div + div / 2;
}
}
}
void colorReduce(cv::Mat &img, cv::Mat &outimg ,int div)
{
outimg.create(img.rows, img.cols, img.type());
int nl = img.rows;
int nc = img.cols*img.channels();
for (int i = 0; i < nl; i++)
{
//每行首地址
uchar* data = img.ptr<uchar>(i);
for (int j = 0; j < nc; j++)
{
data[j] = data[j] / div*div + div / 2;
}
}
}
//cv::saturate_cast<uchar>();
//溢出保護(hù)
void sharpen(const cv::Mat &img, cv::Mat &outimg)
{
outimg.create(img.size(), img.type());
int h = img.rows;
int w = img.cols;
int w2 = w - 1;
int h2 = h - 1;
for (int y = 1; y < h2; y++)
{
const uchar* pre = img.ptr<const uchar>(y - 1);//上一行
const uchar* cur = img.ptr<const uchar>(y);//當(dāng)前行
const uchar* next = img.ptr<const uchar>(y + 1);//下一行
uchar* output = outimg.ptr<uchar>(y);
for (int x = 1; x < w2; x++)
{
//cv::saturate_cast<uchar>( );
*output++ = cv::saturate_cast<uchar>(5 * cur[x] - cur[x - 1] - cur[x + 1] - pre[x] - next[x]);
}
}
//彩色 outimg.row(0).setTo(cv::Scalar(1,2,3));
outimg.row(0).setTo(cv::Scalar(0));
outimg.row(h2).setTo(cv::Scalar(0));
outimg.col(0).setTo(cv::Scalar(0));
outimg.col(w2).setTo(cv::Scalar(0));
}
1.讀圖片 第二個(gè)參數(shù)是類型
enum
{
// 8bit, color or not
IMREAD_UNCHANGED =-1,
// 8bit, gray
IMREAD_GRAYSCALE =0,
// ?, color
IMREAD_COLOR =1,
// any depth, ?
IMREAD_ANYDEPTH =2,
// ?, any color
IMREAD_ANYCOLOR =4
};
string sample_png = "D:\\sample.png";
cv::Mat image = cv::imread(sample_png);
2.Mat_類型和Mat很像祈坠,在初始的時(shí)候指定了矩陣的元素的類型矢劲。
Mat_重載了(),可以直接訪問元素芬沉,比起Mat的a看起來跟更方便一些丸逸。vec 向量黄刚。
/* \typedef
Shorter aliases for the most popular specializations of Vec<T,n>
*/
typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;
typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;
typedef Vec<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;
typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;
typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;
imageOut2 = image.clone();這種是復(fù)制,否則圖像內(nèi)容共享同一塊內(nèi)存區(qū)域检吆。
cv::Mat_ <cv::Vec3b> imageOut2 = image.clone();
3.也可以用指針遍歷矩陣元素蹭沛,例子是void colorReduce(cv::Mat &img, int div)這個(gè)函數(shù)致板。一行一行的遍歷。uchar* data = img.ptr<uchar>(i);然后一個(gè)循環(huán)集嵌,訪問一行的數(shù)據(jù)根欧。
4.outimg.create(img.rows, img.cols, img.type());
這個(gè)可以改變圖像的大小凤粗,但是并不會(huì)改變圖像的數(shù)據(jù)嫌拣。高明的地方在于异逐,如果要改變的大小和原圖是一樣大的灰瞻,就直接返回酝润。
這有啥用呢要销。我們有時(shí)候想直接對(duì)原圖進(jìn)行操作蕉陋,有些時(shí)候想對(duì)原圖創(chuàng)建一個(gè)副本進(jìn)行操作凳鬓。void sharpen(const cv::Mat &img, cv::Mat &outimg)
這個(gè)函數(shù)垦梆,兩個(gè)引用參數(shù)托猩,img是原圖京腥,outimg 是結(jié)果的圖像的引用公浪。如果是想在原圖上進(jìn)行運(yùn)算欠气,輸出圖像那個(gè)參數(shù)那就直接傳原圖就好。要是想把結(jié)果保存到新圖像上去宜鸯。顾翼。就傳進(jìn)去另一個(gè)Mat就好了适贸。其中第一舉的outimg.create(img.size(), img.type()); 的作用就是保證輸出是合法的拜姿。
5.opencv 重載了大部分算數(shù)運(yùn)算符谒获。例如+和-
圖像進(jìn)行加減運(yùn)算的時(shí)候批狱,要注意格式一樣的問題赔硫。
RIO 可以認(rèn)為是圖像的一小部分,一個(gè)矩形。和原圖共享內(nèi)存耘成。
下面是創(chuàng)建ROI的兩種方法。
cv::Mat imageROI = imageOut7(cv::Rect(imageOut7.cols - shit.cols - 500, imageOut7.rows - shit.rows - 50, shit.cols, shit.rows));
cv::Mat imageROI = imageOut7(cv::Range(50, 50 + shit.cols), cv::Range(200, 200 + shit.rows));
程序運(yùn)行結(jié)果圖
原圖有噪聲,說明骡显,加噪聲的imageOut2和原圖的數(shù)據(jù)是公用的一塊內(nèi)存。
cv::Mat_ <cv::Vec3b> imageOut2 = image.clone();
colorReduce的運(yùn)行結(jié)果
銳化后的結(jié)果
像素?cái)?shù)學(xué)操作的結(jié)果