楼主: ReneeBK
1036 8

OpenCV Computer Vision Application Programming Cookbook [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

OpenCV Computer Vision Application Programming Cookbook Second Edition.pdf (20.47 MB, 需要: 5 个论坛币)




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Application Cookbook Computer Program Vision

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-12-19 02:58:46 |只看作者 |坛友微信交流群
  1. Accessing pixel values

  2. In order to access each individual element of a matrix, you just need to specify its row and column numbers. The corresponding element, which can be a single numerical value or a vector of values in the case of a multi-channel image, will be returned.

  3. Getting ready

  4. To illustrate the direct access to pixel values, we will create a simple function that adds salt-and-pepper noise to an image. As the name suggests, salt-and-pepper noise is a particular type of noise in which some randomly selected pixels are replaced by a white or a black pixel. This type of noise can occur in faulty communications when the value of some pixels is lost during the transmission. In our case, we will simply randomly select a few pixels and assign them a white color.

  5. How to do it...

  6. We create a function that receives an input image. This is the image that will be modified by our function. The second parameter is the number of pixels on which we want to overwrite white values:

  7. void salt(cv::Mat image, int n) {

  8.   int i,j;
  9.   for (int k=0; k<n; k++) {

  10.     // rand() is the random number generator
  11.     i= std::rand()%image.cols;
  12.     j= std::rand()%image.rows;


  13.     if (image.type() == CV_8UC1) { // gray-level image

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

  15.     } else if (image.type() == CV_8UC3) { // color image

  16.       image.at<cv::Vec3b>(j,i)[0]= 255;
  17.       image.at<cv::Vec3b>(j,i)[1]= 255;
  18.       image.at<cv::Vec3b>(j,i)[2]= 255;
  19.     }
  20.   }
  21. }
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-12-19 03:00:06 |只看作者 |坛友微信交流群

Scanning an image with pointers

  1. How to do it...

  2. The signature of our color reduction function will be as follows:

  3. void colorReduce(cv::Mat image, int div=64);
  4. The user provides an image and the per-channel reduction factor. Here, the processing is done in-place, that is, the pixel values of the input image are modified by the function. See the There's more… section of this recipe for a more general function signature with input and output arguments.

  5. The processing is simply done by creating a double loop that goes over all pixel values as follows:

  6. void colorReduce(cv::Mat image, int div=64) {

  7.      int nl= image.rows; // number of lines
  8.      // total number of elements per line
  9.      int nc= image.cols * image.channels();
  10.               
  11.      for (int j=0; j<nl; j++) {

  12.         // get the address of row j
  13.         uchar* data= image.ptr<uchar>(j);

  14.         for (int i=0; i<nc; i++) {

  15.             // process each pixel ---------------------

  16.             data[i]=    data[i]/div*div + div/2;

  17.             // end of pixel processing ----------------

  18.         } // end of line
  19.      }
  20. }
复制代码

使用道具

板凳
ReneeBK 发表于 2016-12-19 03:01:06 |只看作者 |坛友微信交流群

Scanning an image with iterators

  1. How to do it...

  2. An iterator object for a cv::Mat instance can be obtained by first creating a cv::MatIterator_ object. As is the case with cv::Mat_, the underscore indicates that this is a template subclass. Indeed, since image iterators are used to access the image elements, the return type must be known at the time of compilation. The iterator is then declared as follows:

  3.      cv::MatIterator_<cv::Vec3b> it;
  4. Alternatively, you can also use the iterator type defined inside the Mat_ template class as follows:

  5.      cv::Mat_<cv::Vec3b>::iterator it;
  6. You then loop over the pixels using the usual begin and end iterator methods, except that these ones are, again, template methods. Consequently, our color reduction function is now written as follows:

  7. void colorReduce(cv::Mat &image, int div=64) {

  8.      // obtain iterator at initial position
  9.      cv::Mat_<cv::Vec3b>::iterator it=
  10.                image.begin<cv::Vec3b>();
  11.      // obtain end position
  12.      cv::Mat_<cv::Vec3b>::iterator itend=
  13.                image.end<cv::Vec3b>();

  14.      // loop over all pixels
  15.      for ( ; it!= itend; ++it) {
  16.         // process each pixel ---------------------

  17.        (*it)[0]= (*it)[0]/div*div + div/2;
  18.        (*it)[1]= (*it)[1]/div*div + div/2;
  19.        (*it)[2]= (*it)[2]/div*div + div/2;

  20.         // end of pixel processing ----------------
  21.      }
  22. }
复制代码

使用道具

报纸
ReneeBK 发表于 2016-12-19 03:02:26 |只看作者 |坛友微信交流群

Writing efficient image-scanning loops

  1. How to do it...

  2. In order to measure the execution time of a function or a portion of code, there exists a very convenient OpenCV function called cv::getTickCount(). This function gives you the number of clock cycles that have occurred since the last time you started your computer. Since we want the execution time of a code portion given in seconds, we use another method, cv::getTickFrequency(). This gives us the number of cycles per second. The usual pattern to be used in order to obtain the computational time of a given function (or portion of code) would then be as follows:

  3. const int64 start = cv::getTickCount();
  4. colorReduce(image); // a function call
  5. // elapsed time in seconds
  6. double duration = (cv::getTickCount()-start)/
  7.                                cv::getTickFrequency();
复制代码

使用道具

地板
ReneeBK 发表于 2016-12-19 03:03:21 |只看作者 |坛友微信交流群

Scanning an image with neighbor access

  1. How to do it...

  2. This time, the processing cannot be accomplished in-place. Users need to provide an output image. The image scanning is done using three pointers, one for the current line, one for the line above, and another one for the line below. Also, since each pixel computation requires access to the neighbors, it is not possible to compute a value for the pixels of the first and last row of the image as well as the pixels of the first and last column. The loop can then be written as follows:

  3. void sharpen(const cv::Mat &image, cv::Mat &result) {

  4.    // allocate if necessary
  5.   result.create(image.size(), image.type());
  6.   int nchannels= image.channels(); // get number of channels

  7.    // for all rows (except first and last)
  8.   for (int j= 1; j<image.rows-1; j++) {

  9.     const uchar* previous=
  10.         image.ptr<const uchar>(j-1);     // previous row
  11.     const uchar* current=
  12.         image.ptr<const uchar>(j);       // current row
  13.     const uchar* next=
  14.         image.ptr<const uchar>(j+1);     // next row

  15.     uchar* output= result.ptr<uchar>(j); // output row

  16.     for (int i=nchannels; i<(image.cols-1)*nchannels; i++) {

  17.        *output++= cv::saturate_cast<uchar>(
  18.                   5*current[i]-current[i-nchannels]-
  19.                   current[i+nchannels]-previous[i]-next[i]);
  20.     }
  21.   }

  22.   // Set the unprocessed pixels to 0
  23.   result.row(0).setTo(cv::Scalar(0));
  24.   result.row(result.rows-1).setTo(cv::Scalar(0));
  25.   result.col(0).setTo(cv::Scalar(0));
  26.   result.col(result.cols-1).setTo(cv::Scalar(0));
  27. }
复制代码

使用道具

7
ReneeBK 发表于 2016-12-19 03:04:52 |只看作者 |坛友微信交流群

Performing simple image arithmetic

  1. Performing simple image arithmetic

  2. Images can be combined in different ways. Since they are regular matrices, they can be added, subtracted, multiplied, or divided. OpenCV offers various image arithmetic operators, and their use is discussed in this recipe.

  3. Getting ready

  4. Let's work with a second image that we will combine to our input image using an arithmetic operator. The following represents this second image:

  5. Getting ready
  6. How to do it...

  7. Here, we add two images. This is useful when we want to create some special effects or to overlay information over an image. We do this by calling the cv::add function, or more precisely here, the cv::addWeighted function, since we want a weighted sum as follows:

  8.    cv::addWeighted(image1,0.7,image2,0.9,0.,result);
复制代码

使用道具

8
ReneeBK 发表于 2016-12-19 03:06:12 |只看作者 |坛友微信交流群

Remapping an image

  1. Remapping an image

  2. In the recipes of this chapter, you learned how to read and modify the pixel values of an image. The last recipe will teach you how to modify the appearance of an image by moving its pixels. The pixel values are not changed by this process; it is rather the position of each pixel that is remapped to a new location. This is useful in order to create special effects on an image or to correct image distortions caused, for example, by a lens.

  3. How to do it...

  4. In order to use the OpenCV remap function, you simply have to first define the map to be used in the remapping process. Second, you have to apply this map on an input image. Obviously, it is the way you define your map that will determine the effect that will be produced. In our example, we define a transformation function that will create a wavy effect on the image:

  5. // remapping an image by creating wave effects
  6. void wave(const cv::Mat &image, cv::Mat &result) {

  7.   // the map functions
  8.   cv::Mat srcX(image.rows,image.cols,CV_32F);
  9.   cv::Mat srcY(image.rows,image.cols,CV_32F);

  10.   // creating the mapping
  11.   for (int i=0; i<image.rows; i++) {
  12.     for (int j=0; j<image.cols; j++) {

  13.       // new location of pixel at (i,j)
  14.       srcX.at<float>(i,j)= j; // remain on same column
  15.                 // pixels originally on row i are now
  16.                 // moved following a sinusoid
  17.       srcY.at<float>(i,j)= i+5*sin(j/10.0);
  18.     }
  19.   }

  20.   // applying the mapping
  21.   cv::remap(image, result, srcX, srcY, cv::INTER_LINEAR);
复制代码

使用道具

9
franky_sas 发表于 2016-12-19 09:27:47 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 20:10