In the previous chapter,we learned how to read Image.In the chapter,we will discuss the most important machine vision.There are three step:
- read the Image into the computer.
- change the Image pixels value.
- save the Image.
In the previous chapter,we have learned read Image.Hence,we will learn last two steps.
Let us to see the code:
#include<iostream>
#include"opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char* argv)
{
Mat src = imread("7258//gun.jpg");
if (src.empty())
{
cout << "open the Image failure!" << endl;
return -1;
}
imshow("src", src);
int height = src.rows;
int width = src.cols;
cout << "height:" << height << endl;
cout << "weidth:" << width << endl;
double t = getTickCount();
if (src.channels() == 1)
{
for (int i = 0;i < height;i++)
{
uchar* p;
p = src.ptr<uchar>(i);
for (int j = 0;j < width;j++)
{
p[j] = saturate_cast<uchar>(p[j] * 2);
}
}
}
else if (src.channels() == 3)
{
for (int i = 0;i < height;i++)
{
Vec3b* p;
Vec3b piexl;
p = src.ptr<Vec3b>(i);
for (int j = 0;j < width;j++)
{
piexl[0] = saturate_cast<uchar>(p[j][0] * 2);
piexl[1] = saturate_cast<uchar>(p[j][1] * 2);
piexl[2] = saturate_cast<uchar>(p[j][2] * 2);
p[j] = piexl;
}
}
}
imshow("new src", src);
imwrite("new_src.jpg",src);
double timesum = (getTickCount() - t) / getTickFrequency();
cout << "time:" << timesum << endl;
for (int i = 0;i < height;i++)
{
for (int j = 0;j < width;j++)
{
if(src.channels() == 1)
{
src.at<uchar>(i, j) = 255 - src.at<uchar>(i, j);
}
else if (src.channels() == 3)
{
src.at<Vec3b>(i, j)[0] = 255 - src.at<Vec3b>(i, j)[0];
src.at<Vec3b>(i, j)[1] = 255 - src.at<Vec3b>(i, j)[1];
src.at<Vec3b>(i, j)[2] = 255 - src.at<Vec3b>(i, j)[2];
}
}
}
imshow("new src1", src);
imwrite("new src1",src);
waitKey(0);
return 0;
}
This is all of the code.Next I will to discuss the code by block.
if (src.empty())
{
cout << "open the Image failure!" << endl;
return -1;
}
this block is used to make sure read Image is success.If read the iamge is success,src.empty() will return Flase ,else it will return true.
int height = src.rows;
int width = src.cols;
this two lines will get row number and column number of the Image.
There are two ways to change the pixel,therefore,i will talk about it one by one.
The first way,we use the point to change the pixel of image:
if (src.channels() == 1)
{
for (int i = 0;i < height;i++)
{
uchar* p;
p = src.ptr<uchar>(i);
for (int j = 0;j < width;j++)
{
p[j] = saturate_cast<uchar>(p[j] * 2);
}
}
}
else if (src.channels() == 3)
{
for (int i = 0;i < height;i++)
{
Vec3b* p;
Vec3b piexl;
p = src.ptr<Vec3b>(i);
for (int j = 0;j < width;j++)
{
piexl[0] = saturate_cast<uchar>(p[j][0] * 2);
piexl[1] = saturate_cast<uchar>(p[j][1] * 2);
piexl[2] = saturate_cast<uchar>(p[j][2] * 2);
p[j] = piexl;
}
}
}
First,we need to get the channels.use the src.channels() to get the channels.when we read a gray image,we will get 1,when we read a rgb image,we will get 3.
When I read a gray Image,use two circulations to get all of the pixel.first,in the first circulation we get point of the row,then in the second circulation we can change the pixel value.yet,sometimes we got the value is over the 255 or below the 0,hence,we need to make sure the value in the between 0 and 255.you can see the saturate_cast<uchar>(),it can help us to solve this question.
When I read a rgb image,use tow circulations to get all of the pixel.In contrast to gray Image,its a pixel has three channels,red,green,blue.yet,it need a special Funfamental Data Structures.it is called Vec3b.we need define a Vec3b point and Vec3b variable.Vec3b variable is a data group,it has three number.
The second way,use the .at<uchar>() to change the pixel of Image:
for (int i = 0;i < height;i++)
{
for (int j = 0;j < width;j++)
{
if(src.channels() == 1)
{
src.at<uchar>(i, j) = 255 - src.at<uchar>(i, j);
}
else if (src.channels() == 3)
{
src.at<Vec3b>(i, j)[0] = 255 - src.at<Vec3b>(i, j)[0];
src.at<Vec3b>(i, j)[1] = 255 - src.at<Vec3b>(i, j)[1];
src.at<Vec3b>(i, j)[2] = 255 - src.at<Vec3b>(i, j)[2];
}
}
}
Beause of it is more easy than previous way,so everyone can get it.
Finally,the save image is easiest.you can learn from the code.