---
title: "opencv入门笔记之二 操作图像像素点"
author: "rex686568"
date: "2015-02-04T22:09:00Z"
tags: []
slug: "opencv"
draft: false
lastmod: "2015-02-07T10:59:30Z"
id: 2184277
url: "https://rex686568.iteye.com/blog/2184277"
---

voidsalt(Mat &image, int n){

for (int k = 0; k &lt; n; k++){

int i = rand() % image.cols;

int j = rand() % image.rows;

// rand() ranges from 0 to int.maxif the denominator is large its not

// uniform distribution

if (image.channels() == 1){

// 255 means white

image.at&lt;uchar&gt;(j, i) = 255;

}

else if (image.channels() == 3){

//color image has 3 channels : blue yellow red in scale form

image.at&lt;Vec3b&gt;(j, i)\[0] = 255;

image.at&lt;Vec3b&gt;(j, i)\[1] = 255;

image.at&lt;Vec3b&gt;(j, i)\[2] = 255;

}

}

}

Then

// reducethe quality of each pixel

voidcolorReduce(Mat &image, int div = 64){

int nl = image.rows;

int nc = image.cols\*image.channels();

for (int i = 0; i &lt; nl; i++){

uchar* data = image.ptr&lt;uchar&gt;(i);

// .ptr is more efficient than .at

for (int j = 0; j &lt; nc; j++){

data\[j] = data\[j] / div\*div + div / 2;

//another way is to use bit operator which is a very efficientalternative

// data\[j]=(data\[j]&(0xFF&lt;&lt;n))+div/2;

}

}

}

2/4/2015 11:34 AM - Screen Clipping

If u wanna haveinput and output in the method

It can be wirttenlike void colorReduce(const Mat &image, Mat &result, int div=64);

To a continuous line

Image.reshape(1,rows\*cols);

channel newrows

&image.at(j,i)

Means data =image.data+ j\*image.step+i\*image.elemSize()

Using iterator toaccess pixels

Mat\_&lt;Vec3b&gt;::iteratorit = image.begin&lt;Vec3b&gt;(), itend = image.end&lt;Vec3b&gt;();

It++

(\*it)\[0]

Its convenient but the pointer type is the fastest

In the following waywe can count time is ms;

double duration = static\_cast&lt;double&gt;(getTickCount());

colorReduce(image);

duration = static\_cast&lt;double&gt;(getTickCount()) - duration;

duration /= getTickFrequency();

void sharpen(Mat &image,Mat &result){

result.create(image.size(), image.type());

for (int i = 1; i &lt; image.rows-1;i++)

{

const uchar* previous = image.ptr&lt;const uchar&gt;(i - 1);

const uchar* current = image.ptr&lt;const uchar&gt;(i);

const uchar* next = image.ptr&lt; const uchar&gt;(i + 1);

uchar* output = result.ptr&lt;uchar&gt;(i);

for (int j = 1; j &lt; image.cols - 1; j++){

\*output++ =saturate\_cast&lt;uchar&gt;( current\[j] * 5 - current\[j -1] - current\[j + 1] - previous\[j] - next\[j]);

//saturate means 0&lt;= uchar&lt;=255

}

}

result.row(0).setTo(Scalar(0));

result.row(result.rows-1).setTo(Scalar(0));

result.col(0).setTo(Scalar(0));

result.col(result.cols-1).setTo(Scalar(0));

// deal with the whole row or column

}

Add onepic to another

// c\[i]=a\[i]+b\[i];

cv::add(imageA,imageB,resultC);

// c\[i]= a\[i]+k;

cv::add(imageA,cv::Scalar(k),resultC);

// c\[i]= k1\*a\[1]+k2\*b\[i]+k3;

cv::addWeighted(imageA,k1,imageB,k2,k3,resultC);

// c\[i]= k\*a\[1]+b\[i];

cv::scaleAdd(imageA,k,imageB,resultC);

Add weightis overloaded

result=0.7\*image1+0.9\*image2;

image=(image&cv::Scalar(mask,mask,mask))

+cv::Scalar(div/2,div/2,div/2);

Split andmerge

// create vector of 3 images

std::vector&lt;cv::Mat&gt; planes;

// split 1 3-channel image into 3 1-channelimages

cv::split(image1,planes);

// add to blue channel

planes\[0]+= image2;

// merge the 3 1-channel images into 13-channel image

cv::merge(planes,result);

ROI

Rect(200,250,logoImage.cols,logoImage.rows)

cv::MatimageROI= image(cv::Range(270,270+logo.rows),

cv::Range(385,385+logo.cols))

image.colRange(start,end);

intmain(){

Mat logo= imread("logo.jpg");

Mat image = imread("rain.jpg");

//the same size

Mat ROI;

Mat result;

imshow("old", image);

result.create(image.size(), image.type());

double duration = static\_cast&lt;double&gt;(getTickCount());

//

ROI = image(Rect(0, 0, logo.cols,logo.rows));

addWeighted(logo, 0.3,ROI, 1.0, 0.0,ROI);

//

duration = static\_cast&lt;double&gt;(getTickCount()) - duration;

duration /= getTickFrequency();

cout &lt;&lt; duration;

imshow("new",image);

waitKey(0);

}