OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Efficient scanning of continuous images

來源:互聯網
上載者:User

標籤:

#include<opencv2\opencv.hpp>void colorReduce(cv::Mat &image, int div=64){  int nr= image.rows; // number of rows  int nc= image.cols * image.channels(); // total number of elements per line  if (image.isContinuous())  {  // then no padded pixels  nc= nc*nr;   nr= 1;  // it is now a 1D array  }  int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));  // mask used to round the pixel value  uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0  for (int j=0; j<nr; j++) {  uchar* data= image.ptr<uchar>(j);  for (int i=0; i<nc; i++) {  *data++= *data&mask + div/2;  } // end of row                   }  }  int main(int argc,char* argv[]){cv::Mat pImg;pImg=cv::imread("lena.jpg");cv::imshow("Image",pImg);colorReduce(pImg);cv::imshow("pImg",pImg);cv::waitKey(0);return 0;}



Mat::reshape
在無需複製資料的前提下改變2D矩陣的形狀和通道數或其中之一。
C++: Mat Mat::reshape(int cn, int rows=0) const
參數:
cn – 新的通道數。若cn=0,那麼通道數就保持不變。
rows –新的行數。 若rows = 0, 那麼行數保持不變。

if (image.isContinuous()){image.reshape(1,image.cols*image.rows);}int nr= image.rows; // number of rows  int nc= image.cols * image.channels(); // total number of elements per line  
和上面一樣效果!

OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Efficient scanning of continuous images

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.