rex686568

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

 

voidsalt(Mat &image, int n){

for (int k = 0; k < n; k++){

int i = rand() % image.cols;

int j = rand() % image.rows;

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

// uniform distribution

 

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

// 255 means white

image.at<uchar>(j, i) = 255;

}

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

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

image.at<Vec3b>(j, i)[0] = 255;

image.at<Vec3b>(j, i)[1] = 255;

image.at<Vec3b>(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 < nl; i++){

uchar* data = image.ptr<uchar>(i);

// .ptr is more efficient than .at

for (int j = 0; j < 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<<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_<Vec3b>::iteratorit = image.begin<Vec3b>(), itend = image.end<Vec3b>();

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<double>(getTickCount());

colorReduce(image);

duration = static_cast<double>(getTickCount()) - duration;

duration /= getTickFrequency();

 

 

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

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

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

{

const uchar* previous = image.ptr<const uchar>(i - 1);

const uchar* current = image.ptr<const uchar>(i);

const uchar* next = image.ptr< const uchar>(i + 1);

uchar* output = result.ptr<uchar>(i);

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

*output++ =saturate_cast<uchar>( current[j] * 5 - current[j -1] - current[j + 1] - previous[j] - next[j]);

//saturate means 0<= uchar<=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<cv::Mat> 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<double>(getTickCount());

 

//

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

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

 

 

//

duration = static_cast<double>(getTickCount()) - duration;

duration /= getTickFrequency();

cout << duration;

imshow("new",image);

waitKey(0);

 

}

 

 

 

 

 

 

 

posted on 2015-02-04 22:09  rex686568  阅读(451)  评论(0编辑  收藏  举报