MV(二)----modify and save Image

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:

  1. read the Image into the computer.
  2. change the Image pixels value.
  3. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值