OpenCV系列笔记五:Accessing pixel values

Overview

图片本就是由一个一个的像素点组成,我们有时候需要对一些特殊的像素点就行操作,而操作像素点的第一个问题就是如何获取到这些像素点。OpenCV为我们提供了at方法。

Code
Mat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

void salt(cv::Mat image, int n){
// C++11 random number generator
std::default_random_engine generator;
std::uniform_int_distribution<int>
randomRow(0, image.rows -1);
std::uniform_int_distribution<int>
randomCol(0, image.cols -1);

int i, j;
for(int k=0; k<n; k++){
// random image coordinate
i = randomCol(generator);
j = randomRow(generator);

if(image.type() == CV_8UC1){
// gray image, single-channel 8-bit image
image.at<uchar>(j,i) = 255;
}else if (image.type() == CV_8UC3){
// color image, 3-channel image
image.at<cv::Vec3b>(j,i)[0] = 255;
image.at<cv::Vec3b>(j,i)[1] = 255;
image.at<cv::Vec3b>(j,i)[2] = 255;
// or in this way
//image.at<cv::Vec3b>(j,i) = cv::Vec3b(255,255,255);

}
}
}


int main(){
cv::Mat image = cv::imread("/home/shensir/Documents/MyPrograming/Cpp/Clions/data/lake.png");
salt(image, 3000);

cv::namedWindow("Image");
cv::imshow("Image", image);
cv::imwrite("salt-lake.jpg", image);
cv::waitKey(0);
return 0;
}

输出:

Mat_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


// for color image
void salt(cv::Mat_<cv::Vec3b> image, int n){
// C++11 random number generator
std::default_random_engine generator;
std::uniform_int_distribution<int>
randomRow(0, image.rows -1);
std::uniform_int_distribution<int>
randomCol(0, image.cols -1);

int i, j;
for(int k=0; k<n; k++){
// random image coordinate
i = randomCol(generator);
j = randomRow(generator);

// color image, 3-channel image
image(j,i)[0] = 255;
image(j,i)[1] = 255;
image(j,i)[2] = 255;
// or in this way
//image(j,i) = cv::Vec3b(255,255,255);

}
}


// overload for gray image
void salt(cv::Mat_<uchar> image, int n){
// C++11 random number generator
std::default_random_engine generator;
std::uniform_int_distribution<int>
randomRow(0, image.rows -1);
std::uniform_int_distribution<int>
randomCol(0, image.cols -1);

int i, j;
for(int k=0; k<n; k++){
// random image coordinate
i = randomCol(generator);
j = randomRow(generator);

// gray image, single-channel 8-bit image
image(j,i) = 255;
}
}


int main(){
// color
cv::Mat_<cv::Vec3b> image = cv::imread("/home/shensir/Documents/MyPrograming/Cpp/Clions/data/lake.png");
cv::imshow("Original Image", image);

salt(image, 3000);
cv::imshow("Salted Image", image);

// gray
cv::Mat_<uchar> gray;
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
cv::imshow("Gray", gray);

salt(gray, 3000);
cv::imshow("Salted Gray", gray);

cv::waitKey(0);
return 0;
}

输出:

本文标题:OpenCV系列笔记五:Accessing pixel values

文章作者:不秩稚童

发布时间:2017年04月28日 - 08:24:51

最后更新:2017年04月28日 - 08:31:33

原始链接:http://datahonor.com/2017/04/28/Accessing-pixel-values/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

击蒙御寇